如何使用Structuremap定制工厂?

时间:2014-10-12 14:30:36

标签: c# dependency-injection structuremap structuremap3

我有一个场景,我的班级依赖于两个服务。但是,我使用提供程序模式,并有一个自定义工厂,从app.config文件返回这些服务的实例。

以下是我想要实现的示例场景代码

public class ClassA
{
     public ClassA(IServiceA serviceA, IServiceB)
     {
         // ServiceA and ServiceB are instantiated through a factory class
     }

     public ClassA(IServiceFactory factory)
     {
         // I want to use this constructor and use this factory and call the above constructor
     }
}

在上面的代码中,我的ClassA需要ServiceA和ServiceB,但是我想使用我的工厂来创建这些类的实例,因此我创建了另一个依赖于工厂类的构造函数。现在从结构Map我想创建一个配置,使用每个类的这个构造函数并正确解析工厂。我不想手动为每个类定义映射,而是创建一个能够正确解析每个类的配置规则。

我无法弄清楚应该怎么做?请提出一些建议。

1 个答案:

答案 0 :(得分:0)

您可能希望查看使用工厂方法注册以使StructureMap解析IA和IB并返回一个新的ClassA实例直接调用构造函数。