简体   繁体   English

具有多个类型的StructureMap开放通用类型

[英]StructureMap Open Generic types with multiple types

so I've been trying to solve this issue for a couple of hours now. 所以我已经尝试解决这个问题了几个小时了。 I searched the internet and looked at many Stackoverflow questions, but none could help me. 我在互联网上搜索并查看了许多Stackoverflow问题,但没有一个可以帮助我。


So I'm building a pipeline. 所以我正在建立一个管道。 The basic conecpt is the following: 基本概念如下:

  • PipelineStore (Singleton) - Contains all registered Pipeline PipelineStore(Singleton)-包含所有已注册的Pipeline
  • Pipeline[TEntity] (Transient) - One Pipeline per Entity (Account, Contact, ...) Pipeline [TEntity](瞬态)-每个实体一条Pipeline (帐户,联系人等)
  • PipelineStep[TOperation, TInteractor, TEntity] (Transient) - Many Steps per Pipeline PipelineStep [TOperation,TInteractor,TEntity](瞬态)-每个Pipeline步骤

[] representing the generic types. []代表通用类型。


I want to use StructureMap to create the Pipeline and the PipelineStep. 我想使用StructureMap创建Pipeline和PipelineStep。 So I created an auto factory called IPipelineFactory which has a single function: 因此,我创建了一个名为IPipelineFactory的自动工厂,它具有一个功能:

IPipeline<TEntity> CreatePipeline<TEntity>() TEntity : EntityDTO, new();

I registered it in the Registry: 我在注册表中注册了它:

For<IPipelineFactory>().CreateFactory();
For(typeof(IPipeline<>))
    .Use(typeof(Pipeline<>))
    .Transient();

Then I get the IPipelineFactory injected and use it like the following: 然后,我注入了IPipelineFactory,并如下所示使用它:

public IPipeline<TEntity> NewPipeline<TEntity>() 
    where TEntity : EntityDTO, new()
{
    var pipeline = _pipelineFactory.CreatePipeline<TEntity>();
    _pipelines.Add(pipeline);
    return pipeline;
}

This works great, but when I try to do the same thing with the PipelineStep it fails: 这很好用,但是当我尝试对PipelineStep做同样的事情时,它失败了:

public interface IPipelineStepFactory
{
    IPipelineStep<TOperation, TInteractor, TEntity> CreatePipelineStep<TOperation, TInteractor, TEntity>() 
        where TOperation : IOperation<TInteractor, TEntity> 
        where TInteractor : IInteractor<TEntity>
        where TEntity : EntityDTO, new();
}

And the registration: 并注册:

For<IPipelineStepFactory>().CreateFactory();
For(typeof(IPipelineStep<,,>))
    .Use(typeof(PipelineStep<,,>));

usage: 用法:

public IAddPipeline<TEntity> Pipe<TOperation, TInteractor>()
    where TOperation : IOperation<TInteractor, TEntity>
    where TInteractor : IInteractor<TEntity>
{
    var step = PipelineStepFactory.CreatePipelineStep<TOperation, TInteractor, TEntity>();
    RegisteredSteps.Add(step);
    return this;
}

When I try to test the code it throws the following exception at runtime: 当我尝试测试代码时,它将在运行时引发以下异常:

No default Instance is registered and cannot be automatically determined for type 'IPipelineStep<TestOperation, ITestInteractor, TestEntity>'

There is no configuration specified for IPipelineStep<TestOperation, ITestInteractor, TestEntity>

I thinks it's probably missing support for open generic types with multiple type arguments. 我认为可能缺少对具有多个类型参数的开放泛型类型的支持。 Please let me know if there's any workarround to fix this issue. 请让我知道是否有任何解决方法。 Thanks for your time! 谢谢你的时间!

So I figured it out myself. 所以我自己弄清楚了。 The problem was, that the PipelineStep had slightly different generic type constraints which caused the issue. 问题是, PipelineStep泛型类型约束稍有不同,从而导致了问题。 I changed 我变了

public class PipelineStep<TOperation, TInteractor, TEntity> : PipelineStepBase, IPipelineStep<TOperation, TInteractor, TEntity> 
    where TOperation : IOperation<TInteractor, TEntity>, IOperation<IInteractor<EntityDTO>, EntityDTO>
    where TInteractor : IInteractor<TEntity>, IInteractor<EntityDTO>
    where TEntity : EntityDTO

to

public class PipelineStep<TOperation, TInteractor, TEntity> : PipelineStepBase, IPipelineStep<TOperation, TInteractor, TEntity> 
        where TOperation : IOperation<TInteractor, TEntity>
        where TInteractor : IInteractor<TEntity>
        where TEntity : EntityDTO

and now it's working! 现在可以了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM