摘要工厂使用依赖注入框架时, [英] Abstract factories when using dependency injection frameworks

查看:92
本文介绍了摘要工厂使用依赖注入框架时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道使用的是DI框架,并在工厂的一个参数时,如何正确地使用抽象工厂是应该由DI框架内加以处理的依赖。

I'm wondering how to properly use abstract factories when using a DI framework and one of the parameters in that factory is a dependency that should be handled by the DI framework.

我不知道是否让我的抽象工厂省略该参数完全然后用我的DI容器接线它还是我应该通过依赖的对象。

I am not sure whether to make my abstract factory omit the parameter completely then use my DI container to wire it up or whether I should pass the dependency to the object.

例如,我有一个TCPSERVER,它使用一个Session.Factory创建插槽。 Session对象,其实需要在其构造函数的处理器。我应该通过处理器到TCPSERVER然后有它它传递到Session.Factory或有我的DI容器做配线?

For example, I have a TcpServer and it uses a Session.Factory to create sockets. The Session object actually takes a Processor in its constructor. Should I pass the Processor to the TcpServer then have it pass it onto the Session.Factory or have my DI container do the wiring?

如果我是有在DI容器做配线它是这样的:

class Session : ISession
{
    public delegate ISession Factory(string name);

    ...
    public Session(string name, Processor processor)
    {
      ...
    }
}

class TcpServer : ITcpServer
{
    private readonly Session.Factory _sessionFactory;

    public TcpServer(Session.Factory sessionFactory)
    {
        this._sessionFactory = socketFactory;
    }

    ...

    public void OnConnectionReceived()
    {
       ...
       var session= _sessionFactory(ip.LocalEndPoint());
       ...

    }
}



然后,使用像Ninject一个DI容器我就能配置容器时,要做到这一点:

Then using a DI container like Ninject I'd be able to do this when configuring the container:

Bind<Session.Factory>().ToMethod(c =>
{
    var processor = Kernel.Get<Processor>();
    return (name) => new Session(name, processor);
}).InSingletonScope();



我的这种方法的主要问题是,它假定谁创造了Session.Factory知道的处理器。对我来说,因为我使用的是DI容器,其实这是很方便的,但它似乎不可思议有一个工厂有它自己的依赖。我总是想象一个厂不是真的永远不必任何成员。

My main issue with this approach is that it assumes whoever creates the Session.Factory knows about the processor. In my case, since I am using a DI container, this is actually very convenient but it seems weird to have a factory have its own dependencies. I always imagined a factory not really ever having any members.

如果我是通过

class Session : ISession
{
    public delegate ISession Factory(string name, Processor processor);

    ...
    public Session(string name, Processor processor)
    {
      ...
    }
}

class TcpServer : ITcpServer
{
    private readonly Session.Factory _sessionFactory;
    private readonly Processor _processor;

    public TcpServer(Session.Factory sessionFactory, Processor processor)
    {
        this._processor = processor;
    }

    ...

    public void OnConnectionReceived()
    {
       ...
       var session = _sessionFactory(ip.LocalEndPoint(), _processor);
       ...

    }
}     



我有两个问题与第二个办法:

I have two issues with the second approach:


  1. 的TCPSERVER实际上并没有做的任何处理器。它只是将它传递。看起来这是穷人的DI工作差不多。

  2. 在这段代码背后的真正程序后,处理器实际上已经到TCPSERVER的参考。因此,使用这种方法的时候,我得到一个循环引用。当我拆开打破它通过使用第一种情形那么它是不是一个问题。

你认为什么是最好的方法?我愿意接受新的想法为好。

What do you think is the best approach? I am open to new ideas as well.

谢谢!

推荐答案

许多容器支持工厂一种或另一种方式,这是你应该走的路。

Many containers support factories in one or another way and this is the way you should go.

例如:以你的例子像这样定义

E.g. Taking your example define a ISessionFactory interface like this

public interface ISessionFactory
{
    ISession CreateSession(string name);
}

有关Ninject 2.3看的 https://github.com/ninject/ninject.extensions.factory ,让它由Ninject实施

For Ninject 2.3 see https://github.com/ninject/ninject.extensions.factory and let it be implemented by Ninject

Bind<ISessionFactory>().AsFactory();

有关2.2做执行自己

public class SessionFactory : ISessionFactory
{
    private IKernel kernel;
    public SessionFactory(IKernel kernel)
    {
        this.kernel = kernel;
    }

    public ISession CreateSession(string name)
    {
        return this.kernel.Get<ISession>(new ConstructorArgument("name", name));
    }
}

这篇关于摘要工厂使用依赖注入框架时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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