之间有什么目的MEF和统一不同? [英] What is different between and purpose of MEF and Unity?

查看:119
本文介绍了之间有什么目的MEF和统一不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习DI(我工作的WPF / Silverlight的,但我有一个计划转移到ASP.NET)。当我阅读网上的一些DI的文章有两个框架,我很感兴趣,MEF与统一。我想知道什么是真正的世界他们之间的不同,哪一个是好去。

I just start study DI (I'm working on WPF/Silverlight but I have a plan to move to ASP.NET). After I read some DI articles from internet there are two Frameworks that I'm interested in, MEF and Unity. I want to know what is the real-world different between them and which one is good to go.

推荐答案

的主要区别是,与统一,你会明确注册要在作文中使用的每个类的:

The main difference is that with unity you will explicitly register each class you want to use in the composition:

var container = new UnityContainer();
container.RegisterType<IFoo,Foo>();
container.RegisterType<IBar,Bar>();
...
var program = container.Resolve<Program>();
program.Run();

在MEF在另一方面,你的属性而不是注册在别的地方纪念类:

In MEF on the other hand, you mark classes with attributes instead of registering them somewhere else:

[Export(typeof(IFoo))]
public Foo
{
   ...
}

乍一看,这看起来像一个小的语法差异,但它实际上是比这更重要。 MEF被设计成允许部件的动态发现。例如,使用 DirectoryCatalog 您可以将应用程序设计以这样一种方式,你可以通过简单的应用程序文件夹下探新的DLL扩展它。

At first sight this looks like a minor syntactic difference, but it is actually more important than that. MEF is designed to allow for the dynamic discovery of parts. For example, with a DirectoryCatalog you can design your application in such a way that you can extend it by simply dropping new DLLs in the application folder.

在这个例子中,MEF会找到并实例化的所有类与 [导出(typeof运算(IPlugin))] 指定目录下的属性,并通过这些实例的计划构造器:

In this example, MEF will find and instantiate all classes with an [Export(typeof(IPlugin))] attribute in the given directory and passes those instances to the Program constructor:

[Export]
public class Program
{
    private readonly IEnumerable<IPlugin> plugins;

    [ImportingConstructor]
    public Program(
       [ImportMany(typeof(IPlugin))] IEnumerable<IPlugin> plugins)
    {
        this.plugins = plugins;
    }

    public void Run()
    {
        // ...
    }
}

切入点:

public static void Main()
{
    using (var catalog = new DirectoryCatalog(".","*"))
    using (var container = new CompositionContainer(catalog))
    {
        var program = container.GetExportedValue<Program>();
        program.Run();
    }
}

要适应这种动态组合方案,MEF有稳定的组合物,一个概念,这意味着当它运行到缺少相关的地方它会简单地标记部分为不可用,并将继续构成反正。

To accommodate such dynamic composition scenarios, MEF has a concept of "stable composition", which means that when it runs into a missing dependency somewhere it will simply mark the part as unavailable and will continue the composition anyway.

<一个href="http://blogs.msdn.com/b/nblumhardt/archive/2009/07/17/light-up-or-mef-optional-exports.aspx">Stable成分是非常有用的,但也使得它很很难调试失败的组成。所以,如果你不需要的部分动态发现和稳定的组合物,我会用一个常规的DI容器,而不是MEF。与MEF,定期DI容器会给你明确的错误信息时,缺少相关。

Stable composition can be quite useful, but it also makes it very difficult to debug a failed composition. So if you don't need dynamic discovery of parts and "stable composition", I would use a regular DI container instead of MEF. Unlike MEF, regular DI containers will give you clear error messages when a dependency is missing.

这也可能可以得到两全其美的使用它集成了MEF,如 DI容器Autofac 的。使用Autofac组成核心应用,和MEF的量需要是动态扩展的部分。

It might also be possible to get the best of both worlds by using a DI container which integrates with MEF, like Autofac. Use Autofac to compose the core application, and MEF for the parts which need to be dynamically extensible.

这篇关于之间有什么目的MEF和统一不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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