如何使用与关系是不注射液工厂,而不诉诸使用Service Locator模式 [英] How to use a factory with Dependecy Injection without resorting to using Service Locator pattern

查看:130
本文介绍了如何使用与关系是不注射液工厂,而不诉诸使用Service Locator模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GUI应用程序。在它我允许用户从算法一个容器提供的列表中选择。每个算法将拉开序幕作为另一种观点后台任务。我需要支持这一观点的多个实例,并支持相同的算法的多个实例。这一观点也将由容器提供。该算法也有状态

I have a GUI application. In it I allow a user to select from a container-provided list of algorithms. Each algorithm will be kicked off as a background task in another view. I need to support multiple instances of this view, and support multiple instances of the same algorithm. That view will also be provided by the container. The algorithm is also stateful.

所以,我有我需要创建我的观点和算法实例,并结合在一起,在运行时的情况。我没有静态绑定点,这种情况下,这样我就可以正常使用的注射设备(构造函数或财产注射)。我不想叫,我不希望使用容器像一个的服务定位器

So I have a case where I need to create instances of my view and algorithm and bind them together at runtime. I don't have static binding points for these instances, so I can't use the normal injection facilities (constructor or property injection). I don't want to call new, and I don't want to use the container like a Service Locator.

我解决了这个在Castle.Windsor与<一个href="http://docs.castleproject.org/Default.aspx?Page=Typed-Factory-Facility&NS=Windsor&AspxAutoDetectCookieSupport=1"相对=nofollow>键入的工厂设施,但我不得不处理的工厂都在我的应用程序。工厂的设计也有些奇怪,因为我有,当我跟他们做回我的实例工厂。

I solved this in Castle.Windsor with the Typed Factory Facility, but I had to deal with factories all over my app. The factory design was also a little strange because I had to return my instances to the factory when I was done with them.

我现在正在研究使用NInject因为到目前为止学习曲线并介绍文档都好多了,我想提出一个容器为我的团队使用。但是,对于这样的情况下,我想我不得不写我自己的工厂,并直接调用内核来解决新的实例(服务定位器嵌入在一个工厂),以及添加工厂方法在我的注册code

I am now looking into using NInject because so far the learning curve and intro docs were much better, and I'd like to propose a container for my team to use. But for a scenario like this I think I'd have to write my own factories and call the kernel directly to resolve new instances (Service Locator embedded in a factory), as well as adding factory methods in my registration code.

有没有一种通用的方法来解决这个问题,或者这仅仅是依赖注入并不是用来解决自己的问题吗?

Is there a generic way to solve this, or is this simply a problem that Dependency Injection wasn't designed to solve on its own?

澄清:

我说的,我想为Ninject一个明确的答案的评论,而我已经得到了。而且非常感谢:)在现实生活中在这里我可能就使用已经提出了务实的解决办法。

I said in the comments that I'd like a specific answer for Ninject, and I've gotten that. And thanks very much :) In real life here I'll probably just use the pragmatic solutions that have been proposed.

不过,我提供我的基础是一个具体的问题,拙劣的我的问题。我希望的是更纯粹的根本答案,在我的标题问题。

But I botched my question by providing my basis as a concrete problem. I was hoping for a more purely fundamental answer to the question in my title.

是否有一个纯的DI技术,允许用户触发组件的新的实例在运行时?或者,将所有这些实现使用容器作为服务定位器,或者需要一个特定的怪癖到容器(例如内置的工厂支持,ALA Castle.Windsor或即将被释放Ninject厂容厂貌),而比使用纯DI仅有方面?

Is there a pure-DI technique that allows the user to trigger new instances of components at runtime? Or would all such implementations use the container as a Service Locator, or require a specific "quirk" to the container (e.g. built-in factory support, ala Castle.Windsor or the soon-to-be-release Ninject factory feature), rather than utilize only aspects of "pure" DI?

我只听到这个词从Java世界,我没有意味着什么太多的想法 - 所以请原谅我:)是我所期待的某种注出的

I've only heard this word from the Java world, and I don't have much idea of what it means - so forgive me :) Is what I am looking for some kind of "outjection"?

推荐答案

最好的,你创建一个这样的工厂接口

Best you create a factory interface like this

public interface IFooFactory
{
    IFoo CreateFoo(int someParameter);
}

有关Ninject 2.3参见<一href="https://github.com/ninject/ninject.extensions.factory">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 by adding the following configuration.

Bind<IFooFactory>().AsFactory();

有关2.2做执行自己。这个实现是容器配置的一部分,你的实现的一部分。

For 2.2 do the implementation yourself. This implementation is part of the container configuration and not part of your implementations.

public class FooFactory: IFooFactory
{
    private IKernel kernel;
    public FooFactory(IKernel kernel)
    {
        this.kernel = kernel;
    }

    public ISession CreateFoo(int someParameter)
    {
        return this.kernel.Get<IFoo>(
            new ConstructorArgument("someParameter", someParameter));
    }
}

这篇关于如何使用与关系是不注射液工厂,而不诉诸使用Service Locator模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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