这几个工具的站点

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. iOS 提交应用过程出现的错误及#解决方案#images can't contain alpha channels or transparencies

        本文永久地址为http://www.cnblogs.com/ChenYilong/p/3977542.html ,转载请注明出处.    当你试图通过<预览>"导出&qu ...

  2. Swagger文档化restful接口

    1.注解 @Api:用在类上,说明该类的作用. @ApiOperation:注解来给API增加方法说明. @ApiImplicitParams : 用在方法上包含一组参数说明. @ApiImplici ...

  3. HDU 3787 A+B 模拟题

    解题报告:就是输入两个用逗号隔开的数字,求出这两个数字的和,并且用正常的方式输出来.直接写一个函数将一个包含逗号的数字转换成十进制的数返回就行了.这里推荐一个函数atoi(),参数是char*型的,然 ...

  4. HDU 1501 Zipper 字符串

    题目大意:输入有一个T,表示有T组测试数据,然后输入三个字符串,问第三个字符串能否由第一个和第二个字符串拼接而来,拼接的规则是第一个和第二个字符串在新的字符串中的前后的相对的顺序不能改变,问第三个字符 ...

  5. Strange Queries(莫队)

    题目 You are given an array with n integers a1, a2, ..., an, and q queries to answer. Each query consi ...

  6. TrID文件类型识别linux版

    读取文件头根据特征码进行文件类型匹配. 官方:http://mark0.net/soft-trid-e.html windows版本小工具:FileAnalysis 以下是linux版本 wget h ...

  7. 如何提升mysql replication的性能&amp;多线程传输二进制日志

    1,最好使用内网或者专线链路传输binlog数据 (千兆网卡.还不够的话,bounding 技术,扩展带宽) 在my.cnf中强制使用内网ip传输数据bind-address=ip2,将二进制保存在独 ...

  8. redis 配置文件翻译

    2014年6月24日 17:29:11 include  如果有其它配置文件,可以使用 include 指令 ####通用配置 daemonize  默认的redis不会以守护进程运行,需要这样的话可 ...

  9. maven:多个镜像配置

    <mirrors> <mirror> <id>nexus-aliyun</id> <mirrorOf>nexus-aliyun</mi ...

  10. Java中final关键字概述

    使用final修饰过的变量都不可以改变: 1.final修饰变量 恒定不变的属性,可以用final关键字来修饰: 变量名建议全部使用大写 final修饰的变量不能改变,如果程序中重新赋值,编译报错 例 ...