这几个工具的站点

Microsoft Unity  http://unity.codeplex.com

Service Locator http://commonservicelocator.codeplex.com

MEF  .net4.0内含,3.x前在codeplex上开源

Utility

The main reasons to use Unity (or any other IoC container) are if:

Ø You have dependencies between your objects .

Ø You need to manage the lifetime of an object .

Ø You want to manage dependencies at runtime, such as cache, constructors, and properties .

Ø You need to intercept the creation of an object .

Unity is a lightweight, extensible dependency injection container that supports interception, constructor injection, property injection, and method call injection. You can use Unity in a variety of different ways to help decouple the components of your applications, to maximize coherence in components, and to simplify design, implementation, testing, and administration of these applications.

Unity is a general-purpose container for use in any type of Microsoft® .NET Framework-based application. It provides all of the features commonly found in dependency injection mechanisms, including methods to register type mappings and object instances, resolve objects, manage object lifetimes, and inject dependent objects into the parameters of constructors and methods and as the value of properties of objects it resolves.

例子

[InjectionConstructor] 
public Writer(ILogger logger) 

    this.logger = logger; 
}

//Prepare the container  
var container = new UnityContainer();  
//We specify that the logger to be used is the FileLogger  
container.RegisterType<ILogger, FileLogger>();  
//and how to instantiate a new Writer  
container.RegisterType<Writer>();  
//Here Unity knows how to create the new constructor  
var writer = container.Resolve<Writer>();  
writer.Write("Some Text.");

通过UnityContainer实现注入

Service Locator

例子

/// <summary> 
/// Utility to configure the container 
/// </summary> 
public sealed class UnityContainerConfigurator 

    /// <summary> 
    /// Configures this instance. 
    /// </summary> 
    /// <returns></returns> 
    public static IUnityContainer Configure() 
    { 
        var container = new UnityContainer() 
        .RegisterType<ILogger, FileLogger>() 
        .RegisterType<Writer>(); 
        return container; 
    } 
}

// create a new instance of Microsoft Unity container 
  var provider = new UnityServiceLocator(UnityContainerConfigurator.Configure()); 
  // assign the container to the Service Locator provider

ServiceLocator.SetLocatorProvider(() => provider);

// resolve objects using the service locator 
  var writer = ServiceLocator.Current.GetInstance<Writer>(); 
  writer.Write("Some Text.");

ServiceLocator的实现非常简单,而且代码也很少

MEF

The main reasons to use MEF are if:

Ø You need to implement external and reusable extensions in your client application, but you might have different implementations in different hosts .

Ø You need to auto-discover the available extensions at runtime .

Ø You need a more powerful and extensible framework than a normal Dependency Injection framework, and you want to get rid of the various boot-strapper and initializer objects .

Ø You need to implement extensibility and/or modularity in your components .

If your application doesn’t require any of the items in these lists, you probably should not implement the IoC pattern, and you might not need to use Unity and MEF .

例子

/// <summary> 
/// Logger customized for MEF 
/// </summary> 
[Export(typeof(ILogger))] 
public class MefLogger : ILogger 

    /// <summary> 
    /// Writes the log. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    public void WriteLog(string message) 
    { 
        Console.WriteLine("String built from MEF: {0}.", message); 
    } 
}

/// <summary> 
/// Gets or sets the writer. 
/// </summary> 
/// <value>The writer.</value> 
[Import] 
public ILogger Writer { get; set; } 
public void Run() 

    // first we build the catalog 
    var catalog = new AssemblyCatalog(Assembly.GetExecutingAssemb 
    //create the container using the catalog 
    var container = new CompositionContainer(catalog); 
    container.ComposeParts(this); 
    //use the resolved property 
    Writer.WriteLog("Mef message"); 
}

简单比较

Service Locator: 最简单的实现形式,对于比较简单的应用合适,本身的实现代码也很简单

Utility:复杂度中等,介于Service Locator和MEF之间

MEF:以一个完整的框架形式展现, .net 4内置支持,提供生命期等各种管理

Microsoft实现的IOC DI之 Unity 、Service Locator、MEF的更多相关文章

  1. 设计模式之初识IoC/DI(六)

    本篇和大家一起学习IoC和DI即控制反转和依赖注入. 当然听上去这词语非常的专业,真不知道是怎么组出来的,看上去难归看上去难,但稍微理解一下也就这么回事了. 首先我们要明白IoC/DI干嘛用的,不然别 ...

  2. 服务定位器(Service Locator)

    服务定位器(Service Locator) 跟DI容器类似,引入Service Locator目的也在于解耦.有许多成熟的设计模式也可用于解耦,但在Web应用上, Service Locator绝对 ...

  3. 使用Microsoft的IoC框架:Unity来对.NET应用进行解耦

    1.IoC/DI简介 IoC 即 Inversion of Control,DI 即 Dependency Injection,前一个中文含义为控制反转,后一个译为依赖注入,可以理解成一种编程模式,详 ...

  4. Atitit。如何实现dip, di ,ioc ,Service Locator的区别于联系

    Atitit.如何实现dip, di ,ioc  ,Service Locator的区别于联系 1. Dip原则又来自于松耦合思想方向1 2. 要实现dip原则,有以下俩个模式1 3. Ioc和di的 ...

  5. 【IOC--Common Service Locator】不依赖于某个具体的IoC

    你在你的应用程序应用IoC容器了吗,你是否希望不依赖于某个具体的IoC,微软的模式与实践团队在Codeplex上发布的Common Service Locator.Common Service Loc ...

  6. 关于依赖注入IOC/DI的感想

    之前一直不明白依赖注入有什么好处,甚至觉得它是鸡肋,现在想想,当时真是可笑. 这个想法正如同说接口是没有用处一样. 当整个项目非常庞大,各个方法之间的调用非常复杂,那么,可以想象一下,假设说没有任何的 ...

  7. IoC/DI

    From:http://jinnianshilongnian.iteye.com/blog/1471944 我对IoC/DI的理解 博客分类: spring杂谈 IoCDI  IoC IoC: Inv ...

  8. asp.net core 四 IOC&DI Autofac

    其实关于IOC,DI已经有了很多的文章,但是自己在使用中还是有很多困惑,而且相信自己使用下,印象还是会比较深刻的 关于这段时间一直在学习.net core,但是这篇文章是比较重要的,也是我自己觉得学习 ...

  9. 我对IoC/DI的理解

    IoC IoC: Inversion of Control,控制反转, 控制权从应用程序转移到框架(如IoC容器),是框架共有特性 1.为什么需要IoC容器 1.1.应用程序主动控制对象的实例化及依赖 ...

随机推荐

  1. Docker学习笔记二 使用镜像

    本文地址:https://www.cnblogs.com/veinyin/p/10408363.html  Docker运行容器前,需本地存在对应镜像,若没有则Docker从镜像仓库下载该镜像.  镜 ...

  2. 【转】CocoaLumberjack——带颜色的Log

    CHENYILONG Blog [转]CocoaLumberjack--带颜色的Log - 趣味苹果开发 - 博客园 转自:趣味苹果开发   CocoaLumberjack--带颜色的Log Coco ...

  3. js 判断日期大小、是否在时间范围内等处理

    var beginval="2015-09-01";//这个时间可以是日期控件选择的,也可以是其他的任何日期时间 var endval="2015-09-01" ...

  4. 用于阻止缓冲区溢出攻击的 Linux 内核参数与 gcc 编译选项

    先来看看基于 Red Hat 与 Fedora 衍生版(例如 CentOS)系统用于阻止栈溢出攻击的内核参数,主要包含两项: kernel.exec-shield 可执行栈保护,字面含义比较“绕”, ...

  5. select & input的disabled属性及其向后台传值问题

    1.select & input disabled属性 select & input 均具有disabled属性,设置该属性可禁止修改select / input 的文本内容,同时也会 ...

  6. WPF的EventAggregator的发布和订阅

    EventAggregator是Prism中专门处理ViewModel与ViewModel之间事件传递的类对象,它提供了针对事件的发布方法和订阅方法,所以可以非常方便的来管理事件.下面分几步来实现相关 ...

  7. Centos: -bash: unzip: command not found

    安装命令:yum install -y unzip zip 同时安装unzip和zip

  8. JS post提交表单

    js post方式提交表单有两种办法,1:AJAX提交 2:在JS里拼出一个form,然后submit 第二种办法的代码 //这个主要是解决给password MD5 var email = 'ema ...

  9. 解析URL参数

    1.拿到一个完整url后,如何解析该url得到里面的参数. /** * 解析url中参数信息,返回参数数组 */ function convertUrlQuery($query) { $queryPa ...

  10. NodeJS学习:环境变量

    简介 环境变量(environment variables) 不属于 NodeJS 范畴,它是操作系统用于设定执行环境的参数.会在程序运行时传递给应用程序. NodeJS 获取环境变量,是通过 glo ...