(转)prism的MEF UNITY容器区别

http://akashkava.com/blog/391/mef-vs-unity-in-composite-application-prism/

This article describes differences between MEF and Unity which may help you in deciding which technology you must use while making composite application.

Both technologies are useful and easy to understand, and offer a rich set of features that can make integration of various software components very easy. However, both being little different in functioning they offer exact same level of features but choosing wrong one can lead to chaos.

So we will drill down to differences between their operations.


Unity


MEF


Unity creates new instance of type by default.


MEF creates singleton instance of type by default.


Unity does not require registration for classes. This means you can instantiate any other third party type.


MEF requires Export attribute to be specified on the class. MEF cannot create instance of third party type unless it defines Export.


Unity requires type registration in code for interface types.


No registration, simple Export attribute does it all.


Unity container can compose IUnityContainer property in the composed class where you can further access it easily in your own scope. This behavior is not useful for Plugin architecture as getting access to IUnityContainer may result in full access to your application.


MEF does not allow composition of CompositionContainer, which blocks access to MEF in your own scope if you do not have CompositionContainer. This is useful in Plugin architecture where third party code has limited access to your application.


Unity offers injection method mechanism, that can define IUnityContainer parameter and get the unity container object, with which you can compose other private properties manually.


MEF can only compose public properties, this is dangerous as anyone can change public property and make application crash easily.


Inside your application’s framework code, Unity serves the best as it gives you full control over type hierarchy, lifecycle and allows you to utilize third party components easily without writing much of code.


Inside your application, MEF will put lots of restrictions in which your framework can operate as it cannot easily compose third party components and it will force you to write numerous attributes in your code without which it will fail drastically.


Mostly, User Interface objects like your View, or UserControl or any UIElement can never be shared as no UIElement can have two parents at same time. So default behavior of Unity to create a new instance of type is very helpful.


Default behavior of MEF will create only one single instance of UI object, that will lead to trouble, not only that, if UI object is third party tool, MEF will not compose it. You can create multiple copies of exported type by specifying one more attribute called [PartCreationPolicy(Shared)], however it is very time comsuming and tedious to define this one every UI related type we create.


Unity does allow singleton objects, but for that you have to register an instance to the container.


MEF by default creates singleton object only.


Unity does not allow multiple registrations for same instance in same scope sharing same interface but different type.


MEF allows multiple singleton objects of different type sharing same interface.


Unity works best for MVVM, as composing user interface parts can be very easy with unity.


MEF does not work great with MVVM as it will create singleton objects that will behave strangely in runtime and lead to UI failure.


Unity is not good for Modularity, as composing IUnityContainer will offer more control of unity lifecycle to third party modules.


MEF is good for Modularity, as it will not allow modification of composition thus offering great deal of security between modules.


So, to develop a framework, MVVM Crud application and a UI framework, Unity is the best.


To expose some functionality of your application for third party modules to register and access limited functionality, MEF is the best.

Following is outline of how your application should be,

Unity Container must compose and manage your application’s framework, UI and MEF modules.

MEF will only expose limited functionality from Unity Container to third party modules.

No module, through MEF should have access to unity container at all.

Thank you for reading this article, please put your suggestions below in the comment. I do not intend to make any specific rules and regulations here but I am suggesting the architecture based on the differences I have outlined.

时间: 2024-11-06 22:42:02

(转)prism的MEF UNITY容器区别的相关文章

【Prism】MEF版HelloWorld

引言 Pirsm框架是由微软P & P小组设计的,用于构建组合式的WPF企业级应用,支持两个IOC容器,分别为Unity和MEF.官方地址为http://compositewpf.codeplex.com/,在上面可以有最新的源码和Demo,其中多数Demo都是用Unity容器构建的,而本人比较喜欢MEF,打算把Unity的Demo全部改成MEF的,先从HelloWorld开始吧. 模块HelloWorldModule 我们需要将HelloWorld.xaml先导出,,如下 [Export(&qu

Prism 5 + MEF中的ModuleCatalog.CreateFromXaml问题

protected override IModuleCatalog CreateModuleCatalog() { return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("ModuleCatalog.xaml", UriKind.Relative)); } .net的背景,就不说了.用这个代码作为开头吧.在Bootstrapper载入部件时需要用到. ModuleCatalog有

【中英对照】【EntLib6】【Unity】实验1:使用一个Unity容器

Lab 1: Using a Unity Container 实验1:使用一个Unity容器 Estimated time to complete this lab: 15 minutes 估计完成时间:15分钟 Introduction 介绍 In this lab, you will practice using a Unity container to create application objects and wire them together. You will update a

数组与泛型容器区别

一.基本 class Shape{ void draw(){ System.out.println(this+".draw()"); } } class Circle extends Shape{ @Override public String toString() { return "Circle"; } } class Rect extends Shape{ @Override public String toString() { return "Re

(转)MEF+Unity

http://www.dotnetspark.com/kb/4659-mef--unity-interception.aspx MEF + Unity Interception Posted By:Mahadesh Mahalingappa       Posted Date: August 06, 2011    Points: 200    Category: C#    URL: In this article I try to use MEF with Unity Framework .

Unity容器在asp.net mvc中的IOC应用及AOP应用

<asp.net-mvc框架揭秘>一书中,有个示例,是使用unity容器来注入自定义的控制器工厂.代码示例可以自己去下载源码,在这里我就不说了.IOC容器的本质是解耦的实例化接口类,而如何做到解耦就是通过第三方容器来实例化,在这里是unity容器,而不是在项目中实例化接口类.实例化的方法无非就是反射,Emit,表达式树,委托等四个方法.Unity容器的IOC使用主要是三个个方法:Register,Resolver,Dispose.前者注册接口和接口类,后者将接口类的实例化转移到第三方容器中实现

Unity容器中AOP应用示例程序

转发请注明出处:https://www.cnblogs.com/zhiyong-ITNote/p/9127001.html 实在没有找到Unity容器的AOP应用程序示例的说明,在微软官网找到了教程(https://docs.microsoft.com/zh-cn/previous-versions/msp-n-p/dn507492(v%3dpandp.30))看的眼睛疼,而且说得也不是很详细.我自己根据一些资料做了个demo.关键代码: /// unity container 的AOP可以完成

Unity容器实现自动注册

如何创建Unity容器? 首先NuGet搜索Unity, 该示例中使用的版本为4.0.1 新建控制台程序 示例中使用常规操作, 创建一个IPay接口, 分别有两个实现类: ApplePay.HuaweiPay, 它们分别实现了IPay接口, 如下所示: public interface IPay { void Send(); } public class ApplePay : IPay { public void Send() { Console.WriteLine("ApplePay!&quo

3、手写Unity容器--第N层依赖注入

这个场景跟<手写Unity容器--极致简陋版Unity容器>不同,这里构造AndroidPhone的时候,AndroidPhone依赖于1个IPad 1.IPhone接口 namespace SimplestUnity_nLayer { interface IPhone { void Call(); } } 2.AndroidPhone实现 namespace SimplestUnity_nLayer { public class AndroidPhone : IPhone { public