使用通过DI容器注入的抽象工厂

22d*_*b05 6 .net c# dependency-injection service-locator abstract-factory

我在一个具体的例子中对依赖注入实现感到困惑.

假设我们有一个SomeClass类,它具有IClassX类型的依赖关系.

public class SomeClass
{
     public SomeClass(IClassX dependency){...}
}
Run Code Online (Sandbox Code Playgroud)

创建IClassX接口的具体实现取决于运行时参数N.

使用给定的构造函数,我无法配置DI容器(使用Unity),因为我不知道将在运行时使用什么IClassX实现.Mark Seemann在他的着作Dependency Injection In .Net中建议我们应该使用Abstract Factory作为注入参数.

现在我们有SomeAbstractFactory,它基于运行时参数runTimeParam返回IClassX的实现.

public class SomeAbstractFactory
{
     public SomeAbstractFactory(){ }

     public IClassX GetStrategyFor(int runTimeParam)
     {
         switch(runTimeParam)
         {
             case 1: return new ClassX1();
             case 2: return new ClassX2();
               default : return new ClassDefault();
         }
     }
}
Run Code Online (Sandbox Code Playgroud)

SomeClass现在接受ISomeAbstractFactory作为注入参数:

public class SomeClass
{
     public SomeClass(ISomeAbstractFactory someAbstractfactory){...}
}
Run Code Online (Sandbox Code Playgroud)

那没关系.我们只有一个组合根,我们在其中创建对象图.我们配置Unity容器以将SomeAbstractFactory注入SomeClass.

但是,我们假设类ClassX1和ClassX2有自己的依赖项:

public class ClassX1 : IClassX
{
    public ClassX1(IClassA, IClassB) {...}
}

public class ClassX2 : IClassX
{
    public ClassX2(IClassA, IClassC, IClassD) {...}
}
Run Code Online (Sandbox Code Playgroud)

如何解决IClassA,IClassB,IClassC和IClassD依赖关系?

1.通过SomeAbstractFactory构造函数注入

我们可以像这样向SomeAbstractFactory注入IClassA,IClassB,IClassC和IClassD的具体实现:

public class SomeAbstractFactory
{
     public SomeAbstractFactory(IClassA classA, IClassB classB, IClassC classC, IClassD classD)
     {...}
     ...
}
Run Code Online (Sandbox Code Playgroud)

Unity容器将在初始组合根中使用,然后使用穷人的DI根据参数runTimeParam返回具体的ClassX1或ClassX2

public class SomeAbstractFactory
{
     public SomeAbstractFactory(IClassA classA, IClassB classB, IClassC classC, IClassD classD){...}

     public IClassX GetStrategyFor(int runTimeParam)
     {
         switch(runTimeParam)
         {
             case 1: return new ClassX1(classA, classB);
             case 2: return new ClassX2(classA, classC, classD);
               default : return new ClassDefault();
         }
     }
}
Run Code Online (Sandbox Code Playgroud)

这种方法的问题:

  • SomeAbstractFactory知道不属于它的依赖项.
  • 更深的对象图需要更改SomeAbstractFactory构造函数和类实现
  • DI容器不会用于解决依赖关系,必须使用穷人的DI

2.明确调用DI容器

我们不是"新建"ClassX1或ClassX2,而是使用DI容器解决它们.

public class SomeAbstractFactory
{
     public SomeAbstractFactory(IUnityContainer container){...}

     public IClassX GetStrategyFor(int runTimeParam)
     {
         switch(runTimeParam)
         {
             case 1: return container.Resolve<IClassX>("x1");
             case 2: return container.Resolve<IClassX>("x2");
               default : return container.Resolve<IClassX>("xdefault");
         }
     }
}
Run Code Online (Sandbox Code Playgroud)

这种方法的问题:

  • DI容器被传递到SomeAbstractFactory
  • DI Resolve方法不仅用于组合根(ServiceLocator反模式)

还有其他更合适的方法吗?

Sco*_*nen 3

下面的示例展示了如何使用 Unity 执行此操作。这篇博文使用 Windsor 更好地解释了这一点。每个的基本概念完全相同,只是实现略有不同。

我宁愿允许我的抽象工厂访问容器。我将抽象工厂视为防止对容器依赖的一种方法 - 我的类仅依赖于IFactory,因此它只是使用容器的工厂的实现。Castle Windsor 更进一步 - 您定义工厂的接口,但 Windsor 提供实际的实现。但这是一个好兆头,即相同的方法在这两种情况下都适用,并且您不必更改工厂接口。

在下面的方法中,必要的是依赖于工厂的类传递一些参数,允许工厂确定要创建哪个实例。工厂会将其转换为字符串,容器会将其与命名实例进行匹配。此方法适用于 Unity 和 Windsor。

通过这种方式,依赖的类IFactory不知道工厂正在使用字符串值来查找正确的类型。在 Windsor 示例中,类将一个Address对象传递给工厂,工厂使用该对象根据地址的国家/地区确定要使用哪个地址验证器。除了工厂之外,没有其他类“知道”如何选择正确的类型。这意味着如果您切换到不同的容器,您唯一需要更改IFactory. 任何依赖的东西都IFactory不必改变。

下面是使用 Unity 的示例代码:

public interface IThingINeed
{}

public class ThingA : IThingINeed { }
public class ThingB : IThingINeed { }
public class ThingC : IThingINeed { }

public interface IThingINeedFactory
{
    IThingINeed Create(ThingTypes thingType);
    void Release(IThingINeed created);
}

public class ThingINeedFactory : IThingINeedFactory
{
    private readonly IUnityContainer _container;

    public ThingINeedFactory(IUnityContainer container)
    {
        _container = container;
    }

    public IThingINeed Create(ThingTypes thingType)
    {
        string dependencyName = "Thing" + thingType;
        if(_container.IsRegistered<IThingINeed>(dependencyName))
        {
            return _container.Resolve<IThingINeed>(dependencyName);
        }
        return _container.Resolve<IThingINeed>();
    }

    public void Release(IThingINeed created)
    {
        _container.Teardown(created);
    }
}

public class NeedsThing
{
    private readonly IThingINeedFactory _factory;

    public NeedsThing(IThingINeedFactory factory)
    {
        _factory = factory;
    }

    public string PerformSomeFunction(ThingTypes valueThatDeterminesTypeOfThing)
    {
        var thingINeed = _factory.Create(valueThatDeterminesTypeOfThing);
        try
        {
            //This is just for demonstration purposes. The method
            //returns the name of the type created by the factory
            //so you can tell that the factory worked.                
            return thingINeed.GetType().Name;
        }
        finally
        {
            _factory.Release(thingINeed);
        }
    }
}

public enum ThingTypes
{
    A, B, C, D
}

public class ContainerConfiguration
{
    public void Configure(IUnityContainer container)
    {
        container.RegisterType<IThingINeedFactory,ThingINeedFactory>(new InjectionConstructor(container));
        container.RegisterType<IThingINeed, ThingA>("ThingA");
        container.RegisterType<IThingINeed, ThingB>("ThingB");
        container.RegisterType<IThingINeed, ThingC>("ThingC");
        container.RegisterType<IThingINeed, ThingC>();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一些单元测试。他们表明工厂IThingINeed在检查传递给其Create()函数的内容后返回正确的类型。

在这种情况下(可能适用也可能不适用)我还指定了一种类型作为默认值。如果容器中没有注册任何与要求完全匹配的内容,那么它可能会返回默认值。该默认值也可以是没有任何行为的空实例。但所有这些选择都是在工厂和容器配置中进行的。

[TestClass]
public class UnitTest1
{
    private IUnityContainer _container;

    [TestInitialize]
    public void InitializeTest()
    {
       _container = new UnityContainer();
       var configurer = new ContainerConfiguration();
       configurer.Configure(_container);
    }

    [TestCleanup]
    public void CleanupTest()
    {
        _container.Dispose();
    }

    [TestMethod]
    public void ThingINeedFactory_CreatesExpectedType()
    {
        var factory = _container.Resolve<IThingINeedFactory>();
        var needsThing = new NeedsThing(factory);
        var output = needsThing.PerformSomeFunction(ThingTypes.B);
        Assert.AreEqual(output, typeof(ThingB).Name);
    }

    [TestMethod]
    public void ThingINeedFactory_CreatesDefaultyTpe()
    {
        var factory = _container.Resolve<IThingINeedFactory>();
        var needsThing = new NeedsThing(factory);
        var output = needsThing.PerformSomeFunction(ThingTypes.D);
        Assert.AreEqual(output, typeof(ThingC).Name);
    }
}
Run Code Online (Sandbox Code Playgroud)

同样的工厂可以使用 Windsor 实现,并且 Windsor 示例中的工厂可以在 Unity 中完成。