gpt4 book ai didi

c# - 简单注入(inject)器和 SolrNet

转载 作者:太空狗 更新时间:2023-10-30 01:04:46 28 4
gpt4 key购买 nike

我正在从简单的 Ninject 世界转移到快速的 Simple Injector 世界,但我被 SolrNet 困住了。许多流行的 IoC 已经集成了 SolrNet,但没有集成 SimpleInjector。

我已经开始通过转换在 https://github.com/mausch/SolrNet/tree/master/Unity.SolrNetIntegration

有谁知道以前是否有人这样做过(由比我知识渊博的人完成)或者任何人都可以将以下内容转换为使用简单的注入(inject)器:

private static void RegisterSolrOperations(SolrCore core, IUnityContainer container, bool isNamed = true) {
var ISolrReadOnlyOperations = typeof (ISolrReadOnlyOperations<>).MakeGenericType(core.DocumentType);
var ISolrBasicOperations = typeof (ISolrBasicOperations<>).MakeGenericType(core.DocumentType);
var ISolrOperations = typeof (ISolrOperations<>).MakeGenericType(core.DocumentType);
var SolrServer = typeof (SolrServer<>).MakeGenericType(core.DocumentType);
var registrationId = isNamed ? core.Id : null;
var injectionConstructor = new InjectionConstructor(
new ResolvedParameter(ISolrBasicOperations, registrationId),
new ResolvedParameter(typeof (IReadOnlyMappingManager)),
new ResolvedParameter(typeof (IMappingValidator)));
container.RegisterType(ISolrOperations, SolrServer, registrationId, injectionConstructor);
container.RegisterType(ISolrReadOnlyOperations, SolrServer, registrationId, injectionConstructor);
}

到目前为止我有:

private static void RegisterSolrOperations(SolrCore core, Container container, bool isNamed = true)
{
var ISolrReadOnlyOperations = typeof(ISolrReadOnlyOperations<>).MakeGenericType(core.DocumentType);
var ISolrBasicOperations = typeof(ISolrBasicOperations<>).MakeGenericType(core.DocumentType);
var ISolrOperations = typeof(ISolrOperations<>).MakeGenericType(core.DocumentType);
var SolrServer = typeof(SolrServer<>).MakeGenericType(core.DocumentType);
var registrationId = isNamed ? core.Id : null;

var injectionConstructor = new InjectionConstructor(
new ResolvedParameter(ISolrBasicOperations, registrationId),
new ResolvedParameter(typeof(IReadOnlyMappingManager)),
new ResolvedParameter(typeof(IMappingValidator)));

container.Register(ISolrOperations, SolrServer, registrationId, injectionConstructor);
container.Register(ISolrReadOnlyOperations, SolrServer, registrationId, injectionConstructor);
}

但是 injectionConstructor 行显然不适用于简单的注入(inject)器。

谢谢

更新我应该提到我使用的是 solrNet 0.4.x,所以配置会略有不同。在查看史蒂文的回答后,我现在有:

container.RegisterSingle<IReadOnlyMappingManager>(
new MemoizingMappingManager(new AttributesMappingManager()));

container.Register<ISolrDocumentPropertyVisitor, DefaultDocumentVisitor>();
container.Register<ISolrFieldParser, DefaultFieldParser>();
container.RegisterAllOpenGeneric(typeof(ISolrDocumentActivator<>), typeof(SolrDocumentActivator<>));
container.RegisterAllOpenGeneric(typeof(ISolrDocumentResponseParser<>), typeof(SolrDocumentResponseParser<>));
container.Register<ISolrFieldSerializer, DefaultFieldSerializer>();
container.Register<ISolrQuerySerializer, DefaultQuerySerializer>();
container.Register<ISolrFacetQuerySerializer, DefaultFacetQuerySerializer>();

container.RegisterAllOpenGeneric(typeof(ISolrAbstractResponseParser<>), typeof(DefaultResponseParser<>));
container.Register<ISolrHeaderResponseParser, HeaderResponseParser<string>>();
container.Register<ISolrExtractResponseParser, ExtractResponseParser>();

container.RegisterAll<IValidationRule>(new[] {
typeof(MappedPropertiesIsInSolrSchemaRule),
typeof(RequiredFieldsAreMappedRule),
typeof(UniqueKeyMatchesMappingRule)
});

container.Register<ISolrConnection>(() => new SolrConnection(url));
container.RegisterAllOpenGeneric(typeof(ISolrMoreLikeThisHandlerQueryResultsParser<>), typeof(SolrMoreLikeThisHandlerQueryResultsParser<>));
container.RegisterAllOpenGeneric(typeof(ISolrQueryExecuter<>), typeof(SolrQueryExecuter<>));
container.RegisterAllOpenGeneric(typeof(ISolrDocumentSerializer<>), typeof(SolrDocumentSerializer<>));
container.RegisterAllOpenGeneric(typeof(ISolrBasicOperations<>), typeof(SolrBasicServer<>));
container.RegisterAllOpenGeneric(typeof(ISolrBasicReadOnlyOperations<>), typeof(SolrBasicServer<>));
container.RegisterAllOpenGeneric(typeof(ISolrOperations<>), typeof(SolrServer<>));
container.RegisterAllOpenGeneric(typeof(ISolrReadOnlyOperations<>), typeof(SolrServer<>));
container.Register<ISolrSchemaParser, SolrSchemaParser>();
container.Register<ISolrDIHStatusParser, SolrDIHStatusParser>();
container.Register<IMappingValidator, MappingValidator>();

但是有了这个我得到了错误:

The constructor of the type MappingValidator contains the parameter of type IValidationRule[] with name 'rules' that is not registered. Please ensure IValidationRule[] is registered in the container, or change the constructor of MappingValidator.

最佳答案

Unity 配置看起来过于复杂,Ninject 配置要简单得多。我不确定为什么 Unity 配置如此复杂。

我抓取了 Ninject 配置并转换为 Simple Injector。由于我不熟悉 Solr,Solr 可能依赖于简单注入(inject)器配置中的一些额外调整,但我认为这会让你继续:

// using SimpleInjector;

public static void Configure(Container container, string serverURL)
{
container.RegisterInstance<IReadOnlyMappingManager>(
new MemoizingMappingManager(new AttributesMappingManager()));
container.Register<ISolrDocumentPropertyVisitor, DefaultDocumentVisitor>();
container.Register<ISolrFieldParser, DefaultFieldParser>();
container.Register(typeof(ISolrDocumentActivator<>), typeof(SolrDocumentActivator<>));
container.Register(typeof(ISolrDocumentResponseParser<>), typeof(SolrDocumentResponseParser<>));
container.Register<ISolrFieldSerializer, DefaultFieldSerializer>();
container.Register<ISolrQuerySerializer, DefaultQuerySerializer>();
container.Register<ISolrFacetQuerySerializer, DefaultFacetQuerySerializer>();

container.Collection.Register(typeof(ISolrResponseParser<>), new[] {
typeof(ResultsResponseParser<>),
typeof(HeaderResponseParser<>),
typeof(FacetsResponseParser<>),
typeof(HighlightingResponseParser<>),
typeof(MoreLikeThisResponseParser<>),
typeof(SpellCheckResponseParser<>),
typeof(StatsResponseParser<>),
typeof(CollapseResponseParser<>)
});

container.Register<ISolrHeaderResponseParser, HeaderResponseParser<string>>();

container.Collection.Register<IValidationRule>(new[] {
typeof(MappedPropertiesIsInSolrSchemaRule),
typeof(RequiredFieldsAreMappedRule),
typeof(UniqueKeyMatchesMappingRule)
});

container.RegisterInstance<ISolrConnection>(new SolrConnection(this.serverURL));
container.Register(typeof(ISolrQueryResultParser<>), typeof(SolrQueryResultParser<>));
container.Register(typeof(ISolrQueryExecuter<>), typeof(SolrQueryExecuter<>));
container.Register(typeof(ISolrDocumentSerializer<>), typeof(SolrDocumentSerializer<>));
container.Register(typeof(ISolrBasicOperations<>), typeof(SolrBasicServer<>));
container.Register(typeof(ISolrBasicReadOnlyOperations<>), typeof(SolrBasicServer<>));
container.Register(typeof(ISolrOperations<>), typeof(SolrServer<>));
container.Register(typeof(ISolrReadOnlyOperations<>), typeof(SolrServer<>));
container.Register<ISolrSchemaParser, SolrSchemaParser>();
container.Register<IMappingValidator, MappingValidator>();
}

关于c# - 简单注入(inject)器和 SolrNet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21784507/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com