IoC在类库中。在哪里引导 [英] IoC in class library. Where to bootstrap

查看:122
本文介绍了IoC在类库中。在哪里引导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用可以被其他组件重用的类库。在这个类库中,我使用unity来进行依赖注入。对于这个类库,我创建一个测试项目。呼叫者也获得一个测试项目。我不确定的一件事是绑定的位置。我应该把它包含在类库中,还是应该从调用应用程序中这样做?

解决方案

这是一个有趣的问题。如何依赖注入不具有入口点的可重用的程序集。我真的很想看别人的答案。



依赖注入是入口点汇编的责任 。然而,如果你有很多班级和大会每个都需要DI,那么他们可能会被暂停一些班级/班组,而且任务变得繁重。



Solution One



使用约定超过配置。你坚持一个类 Foo 执行 IFoo 等的规则。很多DI框架都有办法设置使用约定



解决方案二



以上解决方案无法解决所有问题,因为有时您需要参数化设置注射。这是我如何解决问题(特别是对于由 MEF 加载的那些程序集,这是针对 AutoFac ):



创建界面 IIocInstaller 其中容器(或构建器被传递)

  public interface IIocInstaller 
{
void Setup(ContainerBuilder builder);
}

创建了一个程序集属性来标记需要DI的程序集:

  [AttributeUsage(AttributeTargets.Assembly)] 
public class ExportAssemblyAttribute:Attribute
{
}

在每个程序集中,我创建一个设置DI的类:

  [assembly:ExportAssembly] 
命名空间This.That
{

[Export(typeof(IIocInstaller))]
public class IocInstaller:IIocInstaller
{
public void Setup(ContainerBuilder builder)
{
....
}
}
}

然后在入口点,我有一个通用代码,可以查看具有程序集属性的所有加载程序集(包括MEFed的程序集),然后查找实现 IIocInstaller 的类型,然后在其上调用安装程序。 p>

I'm using a class library that can be reused by other components. In this class library I'm using unity for dependency injection. For this class library I create a test project. The caller also gets a test project. One thing I'm uncertain about is the location of the bindings. Should I incorporate this in the class library or should I do this from the calling application?

解决方案

This is an interesting problem. How can you dependency inject re-usable assemblies that do not have an entry point. I would really like to see other people's answer.

Dependency injection is the responsibility of the entry-point assembly. Yet, if you have a lot of classes and assemblies each needing DI, then it is possible that they are left-off for some classes/assemblies and the task becomes onerous.

Solution One

Using convention over configuration. You stick to a rule of class Foo implementing IFoo, etc. A lot of DI frameworks have the means to set it up using convention.

Solution two

Above solution does not solve all problems since sometimes you need to parameterise the setup of the injection. Here is how I have solved the problem (especially for those assemblies loaded by MEF and this is for AutoFac):

Created an interface IIocInstaller where container (or builder is passed)

public interface IIocInstaller
{
    void Setup(ContainerBuilder builder);
}

Created an assembly attribute that flags assemblies needing DI:

[AttributeUsage(AttributeTargets.Assembly)]
public class ExportAssemblyAttribute : Attribute
{
}

In each assembly, I create a class that sets up DI:

[assembly: ExportAssembly]
namespace This.That
{

    [Export(typeof(IIocInstaller))]
    public class IocInstaller : IIocInstaller
    {
        public void Setup(ContainerBuilder builder)
        {
            ....
        }
    }
}

Then in the entry point, I have a common code which looks through all loaded assemblies (including MEFed ones) that have the assembly attribute and then look for the type implementing the IIocInstaller and then call Setup on them.

这篇关于IoC在类库中。在哪里引导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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