简单的注射器和SolrNet

时间:2014-02-14 16:28:39

标签: c# solrnet simple-injector

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

我已经开始通过转换发现的Unity内容来创建自己的集成 https://github.com/mausch/SolrNet/tree/master/Unity.SolrNetIntegration

有没有人知道这是否已经完成(由比我更了解的人),或者任何人都可以将以下内容转换为使用简单的注射器:

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);            
    }

但是injectConstructor行显然不适用于简单的注入器。

由于

更新 我应该提到我使用的是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>();

但是我得到了错误:

  

MappingValidator类型的构造函数包含参数   键入IValidationRule [],其名称为'rules',未注册。   请确保IValidationRule []已在容器中注册,或   更改MappingValidator的构造函数。

1 个答案:

答案 0 :(得分:5)

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

我抓住了Ninject配置并转换为Simple Injector。由于我不熟悉Solr,可能是Solr在Simple Injector配置中依赖于一些额外的调整,但我认为这会让你顺利:

// 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>();
}