Microsoft实现的IOC DI之 Unity 、Service Locator、MEF

栏目: 后端 · 发布时间: 8年前

内容简介:Microsoft实现的IOC DI之 Unity 、Service Locator、MEF

这几个 工具 的站点

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

Microsoft实现的IOC DI之 Unity 、Service Locator、MEF

例子

/// <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 .

Microsoft实现的IOC DI之 Unity 、Service Locator、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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

玻璃笼子

玻璃笼子

[美]尼古拉斯·卡尔 / 杨柳 / 中信出版社 / 2015-11 / 49.00元

这是一本关于自动化的书,它提醒我们自动化对人类的影响,人们心安理得享受技术带来的便利却忽视了,它已经渗透进了生活和工作改变了我们的思维和认知方式。商家在设计程序和应用时,早就把他们的想法埋入了编程和APP中。 卡尔的作品无疑是给我们这个时代灌入了的一剂清醒药。他独特的思考问题角度,犀利甚至略为偏激 的言论再加上丰富的*前沿的科技案例会让人读起来畅快淋漓,且醍醐灌顶,意识到自动化等高科技潜移默......一起来看看 《玻璃笼子》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具