简单的注射器:工厂类,需要创建班级依赖 [英] Simple Injector:Factory classes that need to create classes with dependencies

查看:170
本文介绍了简单的注射器:工厂类,需要创建班级依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建了几个不同类型的类的工厂类。工厂注册到容器。什么是工厂里面创建类,因为他们也有相关性的推荐方式。我显然希望避免在容器上的依赖,但如果我新的类,然后他们将不会被使用的容器。 例如,

I have a factory class that creates a couple of different types of class. The factory is registered with the container. What is the recommended way of creating the classes inside the factory, given that they also have dependencies. I clearly want to avoid a dependency on the container but if I new those classes then they won't be using the container. e.g.

public class MyFactory
{
    public IMyWorker CreateInstance(WorkerType workerType)
    {
        if (workerType == WorkerType.A)
              return new WorkerA(dependency1, dependency2);

        return new WorkerB(dependency1);

    }
}

所以,问题是我在哪里可以得到这些依赖关系。

So the question is where do I get those dependencies from.

一种选择是让他们工厂的依赖关系。 例如,

One option could be to make them dependencies of the factory. e.g.

public class MyFactory
{
    private Dependency1 dependency1;
    private Dependency2 dependency2;

    public MyFactory(Dependency1 dependency1, Dependency2, dependency2)
    {
        this.dependency1 = dependency1; this.dependency2 = dependency2;
    }

    public IMyWorker CreateInstance(WorkerType workerType)
    {
        if (workerType == WorkerType.A)
              return new WorkerA(dependency1, dependency2);

        return new WorkerB(dependency1);

    }
}

另一种可能是注册工人类型,使工厂的那些依赖关系 例如,

Another could be to register the worker types and make those dependencies of the factory e.g.

public class MyFactory
{
    private IWorkerA workerA;
    private IWorkerB workerB;

    public MyFactory(IWorkerA workerA, IWorkerB, workerB)
    {
        this.workerA = workerA; this.workerB = workerB;
    }

    public IMyWorker CreateInstance(WorkerType workerType)
    {
        if (workerType == WorkerType.A)
              return workerA;

        return workerB;

    }
}

使用我感觉我榨取工人的依赖进厂的第一个选项。随着第二个选项是在创建工厂时创建的工人。

With the first option I feel like I am leeching the dependencies of the workers into the factory. With the second option the workers are created when the factory is created.

思考?

推荐答案

这是什么的一个经典问题。

This is something of a classic question.

两者的解决方案可能是有问题的,如果不同的实现的数量增加,特别是如果依赖关系对每个实现的有很多方差。你可以结束了一个构造函数20个参数。

Both of your solutions could be problematic if the number of different implementations increases, especially if the dependencies for each of the implementations have a lot of variance. You could end up with a constructor that takes 20 parameters.

我的preferred实施只是有工厂类引用的容器,并解决所需的实例方法。

My preferred implementation is to just have the factory class reference the container, and resolve the needed instance that way.

有些人可能会认为这是不超过服务定位器反模式更好,但我不觉得自己有一个完美的解决了这个问题,并做这种方式似乎是最自然的我。

Some might argue that this is no better than the Service Locator anti-pattern, but I don't feel like there is a perfect solution to this problem, and doing it this way seems the most natural to me.

这篇关于简单的注射器:工厂类,需要创建班级依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆