这几个工具的站点

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 判断相册相机是否允许

    1 判断是否允许使用相机: NSString *mediaType = AVMediaTypeVideo; AVAuthorizationStatus authStatus = [AVCaptureD ...

  2. waven 常用构建命令

    常用命令 mvn compile : 编译maven项目 mvn test : 运行项目测试用例 mvn package : 将项目打成jar包 mvn clean : 删除target目录下生成的文 ...

  3. 记webpack下提取公共js代码的方法

    环境: webpack4.6 + html-webpack-plugin 多页面多入口 经多次研究,稍微靠谱可用的配置 optimization: { splitChunks: { minSize: ...

  4. RabbitMQ Queue一些常见模式

    懒队列:lazy Queue,即用到的时候才会加载,3.6.0及之后新添加的.当新添加数据后,不会将其放入到内存中,而是将其放入到磁盘中. 普通队列:1).in-memory,数据直接放入到内存中. ...

  5. 001_fpm打包命令详解

    使用fpm来制作rpm包 2017/2/22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  6. 二、vue中组件的使用

    1.组件拆分 1.组件实质上也是一个vue实例,因此组件中也可以使用vue的对象属性,反过来每一个vue实例也是一个vue组件(注:1.唯一不同的是el是根实例的特有选项,2.组件中的data必须是一 ...

  7. Nuxt学习笔记

    参考地址:https://zh.nuxtjs.org/guide/installation  官网 http://jspang.com/2018/02/26/nuxt/ 1.目录结构 2.Nuxt常用 ...

  8. Network Principle Course Summary 001

    1.物理层 物理层 协议:RJ45.CLOCK.IEEE802.3 (中继器,集线器) 作用:通过媒介传输比特,确定机械及电气规范(比特Bit) 1.1 通信基础 数据 (data) —— 运送消息的 ...

  9. hdu 2065(泰勒展式)

    比赛的时候遇到这种题,只能怪自己高数学得不好,看着别人秒.... 由4种字母组成,A和C只能出现偶数次. 构造指数级生成函数:(1+x/1!+x^2/2!+x^3/3!……)^2*(1+x^2/2!+ ...

  10. java 完全解耦

    只要有一个方法操作的是类而非接口,那么你就只能使用这个类及其子类,如果你想要将这个方法应用于不在此继承结构中的某个类,那么你就会触霉头,接口可以在很大程度上放宽这种限制,因此,我们可以编写可服用性更好 ...