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

问题描述

我刚刚开始学习DI(我正在使用WPF / Silverlight,但我有计划移动到ASP.NET)。从互联网上阅读了一些DI文章后,我有两个框架,我感兴趣,MEF和Unity。我想知道他们之间的真实世界是不同的,哪一个是好的。

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();

p>

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将使用 [Export(typeof(IPlugin))] [/ code]属性,并将这些实例传递给程序构造函数:

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.

,但它也使它。因此,如果您不需要动态发现零件和稳定组合,我将使用常规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.

也可以通过使用DI容器获得最佳的两个世界它与MEF集成,如。使用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和Unity有什么不同和目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 18:10