具有多种类型的 StructureMap Open 通用类型
StructureMap Open Generic types with multiple types
所以我已经尝试解决这个问题几个小时了。我在互联网上搜索并查看了许多 Whosebug 问题,但 none 可以帮助我。
所以我正在构建管道。基本概念如下:
- PipelineStore (Singleton) - 包含所有已注册的
Pipeline
- 管道[TEntity](瞬态)- 每个实体(客户、联系人...)一个
Pipeline
- PipelineStep[TOperation, TInteractor, TEntity](瞬态)- 每个
Pipeline
的许多步骤
[]
表示通用类型。
我想使用 StructureMap 创建 Pipeline 和 PipelineStep。所以我创建了一个名为 IPipelineFactory
的汽车工厂,它只有一个功能:
IPipeline<TEntity> CreatePipeline<TEntity>() TEntity : EntityDTO, new();
我在注册表中注册了它:
For<IPipelineFactory>().CreateFactory();
For(typeof(IPipeline<>))
.Use(typeof(Pipeline<>))
.Transient();
然后我注入 IPipelineFactory 并像下面这样使用它:
public IPipeline<TEntity> NewPipeline<TEntity>()
where TEntity : EntityDTO, new()
{
var pipeline = _pipelineFactory.CreatePipeline<TEntity>();
_pipelines.Add(pipeline);
return pipeline;
}
效果很好,但是当我尝试用 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();
}
以及注册:
For<IPipelineStepFactory>().CreateFactory();
For(typeof(IPipelineStep<,,>))
.Use(typeof(PipelineStep<,,>));
用法:
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;
}
当我尝试测试代码时,它在运行时抛出以下异常:
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>
我认为它可能缺少对具有多个类型参数的开放泛型类型的支持。如果有解决此问题的任何解决方法,请告诉我。感谢您的宝贵时间!
所以我自己想通了。问题是 PipelineStep
的泛型类型约束略有不同,这导致了这个问题。我变了
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
至
public class PipelineStep<TOperation, TInteractor, TEntity> : PipelineStepBase, IPipelineStep<TOperation, TInteractor, TEntity>
where TOperation : IOperation<TInteractor, TEntity>
where TInteractor : IInteractor<TEntity>
where TEntity : EntityDTO
现在可以使用了!
所以我已经尝试解决这个问题几个小时了。我在互联网上搜索并查看了许多 Whosebug 问题,但 none 可以帮助我。
所以我正在构建管道。基本概念如下:
- PipelineStore (Singleton) - 包含所有已注册的
Pipeline
- 管道[TEntity](瞬态)- 每个实体(客户、联系人...)一个
Pipeline
- PipelineStep[TOperation, TInteractor, TEntity](瞬态)- 每个
Pipeline
的许多步骤
[]
表示通用类型。
我想使用 StructureMap 创建 Pipeline 和 PipelineStep。所以我创建了一个名为 IPipelineFactory
的汽车工厂,它只有一个功能:
IPipeline<TEntity> CreatePipeline<TEntity>() TEntity : EntityDTO, new();
我在注册表中注册了它:
For<IPipelineFactory>().CreateFactory();
For(typeof(IPipeline<>))
.Use(typeof(Pipeline<>))
.Transient();
然后我注入 IPipelineFactory 并像下面这样使用它:
public IPipeline<TEntity> NewPipeline<TEntity>()
where TEntity : EntityDTO, new()
{
var pipeline = _pipelineFactory.CreatePipeline<TEntity>();
_pipelines.Add(pipeline);
return pipeline;
}
效果很好,但是当我尝试用 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();
}
以及注册:
For<IPipelineStepFactory>().CreateFactory();
For(typeof(IPipelineStep<,,>))
.Use(typeof(PipelineStep<,,>));
用法:
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;
}
当我尝试测试代码时,它在运行时抛出以下异常:
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>
我认为它可能缺少对具有多个类型参数的开放泛型类型的支持。如果有解决此问题的任何解决方法,请告诉我。感谢您的宝贵时间!
所以我自己想通了。问题是 PipelineStep
的泛型类型约束略有不同,这导致了这个问题。我变了
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
至
public class PipelineStep<TOperation, TInteractor, TEntity> : PipelineStepBase, IPipelineStep<TOperation, TInteractor, TEntity>
where TOperation : IOperation<TInteractor, TEntity>
where TInteractor : IInteractor<TEntity>
where TEntity : EntityDTO
现在可以使用了!