黄聪:Microsoft Enterprise Library 5.0 系列教程(八) Unity Dependency Injection and Interception...

原文: 黄聪:Microsoft Enterprise Library 5.0 系列教程(八) Unity Dependency Injection and Interception

依赖注入容器Unity:

Unity的构造类似于Castle中的IOC(控制反转 或者叫依赖注入)容器,我们使用抽象接口来隔离使用者和具体实现之间的依赖关系,但是不管再怎么抽象,最终还是要创建具体实现类的实例,这种创建具体实现类的实例对象就会造成对于具体实现的依赖,为了消除这 种创建依赖性,需要把依赖移出到程序的外部(比如配置文件)。使用依赖注入后,这些类完全是基于抽象接口编写而成的,所以可以最大限度地适应需求的变化。 依赖注入的形式有三种,分别为构造子注入(Constructor Injection)、设值方法注入(Setter Injection)和接口注入(Interface Injection)。

还是和以前一样,我们用实际的需求来做演示,说明这个模块到底能解决什么实际的问题吧(以下的例子是仿照TerryLee的文章: Castle IOC容器快速入门,在此我使用Unity来恢复现场,给大家演示如何用Unity解决相同的问题 ):

现在假如我们有这样一个需求,开发一个日志组件,把日志信息输出到文本文件,同时对输出的信息进行格式化,以示意性的代码来实现.

 

1.创建一个新的控制台应用程序,加入需要的Dll文件,并添加需要的引用:

 

 

添加引用:

using Microsoft.Practices.Unity;

2.测试:

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;

namespace test
{
class Program
{
static void Main( string [] args)
{
Console.WriteLine(
" 测试一: " );
test1();

Console.WriteLine(
" 测试二: " );
test2();
}

private static void test1()
{
// 1. 日志格式化只是日志服务中的一个组件.(注:在此实例化该组件没有多大意义,唯一用处只是用来给服务提供一个参数)
ILogFormatter logFormatter = new TextFormatter( " / " );

// 2.创建服务
ILog logServer = new TextFileLog( @" C:\test.log " ,logFormatter);

// 3.使用服务
logServer.Write( " 日志正文 " );
}

private static void test2()
{
// 1.注册了一个容器;
IUnityContainer container = new UnityContainer();

// 2.向容器中注册ILog服务,并告诉容器用TextFileLog实现这个服务
container.RegisterType < ILog, TextFileLog > ();

// 3.向容器中注册ILogFormatter,并告知容器用TextFormatter实现它
// 容器发现类的构造函数还需要另外一个参数Target,这里用InjectionProperty注入该参数
container
.RegisterType
< ILogFormatter, TextFormatter > ( new InjectionProperty( " Target " , " / " ))
.RegisterInstance
< string > ( @" C:\test.log " );

// 4.获取服务
ILog log = container.Resolve < ILog > ();

// 5.使用服务,到此为止我们都没有使用new关键字创建一个具体类的实例,
// 完全屏蔽了组件和服务的实例化,而由Unity自动装配,使得程序更加的灵活.
log.Write( " 日志正文 " );
}
}

}

运行结果:

 

 

到此为止,我们从中要了解到几个知识点:

1.服务

服务是一个个的接口, 接口约定了服务,从而使随意替换服务的实现对使用接口服务的代码没有任何的影响。像我们上面例子中的ILog,ILogFormatter都是一个个服务,我们在这个例子中支实现了一个文本文件的日志记录,如果你要是实 现数据库记录的日志记录,都必须要遵守ILog这个接口。

2.组件

简单来说组件是一个可 重用的程序单元,它实现了某个接口,并仅仅只实现了这一个良好的接口。也就是说,组件是实现了某个服务接口的类。像上例中的TextFileLog,TextFormatter都 是组件

3.自动装配

在上面的例子中,大家 可能都已经注意到了,TextFileLog依赖于TextFormatter, 我们却没有在配置文件中指定它们之间的依赖关系,这就是Castle IOC聪明的一个地方,它能 够自动管理组件之间的依赖关系,而无需编写特定的xml config来配置,即自动装配的意思。

 

这里还有篇从别的网站Copy过来的IOC介绍,很形象生动,希望对大家的理解有所帮助:

      Spring框架中也有IoC(Inversion of Control,控制倒转)。并且是spring的核心,贯穿始终。所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命周期和 对象间的关系。这是什么意思呢,举个简单的例子,我们是如何找女朋友的?常见的情况是,我们到处去看哪里有长得漂亮身材又好的mm,然后打听她们的兴趣爱好、qq号、电话号、ip号、iq号………,想办法认识她们,投其所好送其所要,然后嘿嘿……这个过程是复杂深奥的,我们必须自己设计和面对每个环节。传 统的程序开发也是如此,在一个对象中,如果要使用另外的对象,就必须得到它(自己new一个,或者从JNDI中查询一个),使用完之后还要将对象销毁(比如Connection等),对象始终会和其他的接口或类藕合起来。
  那么IoC是如何做的呢?有点像通过婚介找女朋友,在我和女朋友之间引入了一个第三者:婚姻介绍所。婚介管理了很多男男女女的资料,我可以向婚介提出一个列表,告诉它我想找个什么样的女朋友,比如长得像李嘉欣,身材像林熙雷,唱歌像周杰伦,速度像卡洛斯,技术像齐达内之类的,然后婚介就会按照我们的要求,提供一个mm,我们只需要去和她谈恋爱、结婚就行了。简单明了,如果婚介给我们的人选不符合要求,我们就会抛出异常。整个过程不再由我自己控制,而是有婚介这样一个类似容器的机构来控制。Spring所倡导的开发方式就是如此,所有的类都会在spring容器中登记,告诉spring你是个什么东西,你需要什么东西,然后spring会在系统运行到适当的时候,把你要的东西主动给你,同时也把你交给其他需要你的东西。所有的类的创建、销毁都由 spring来控制,也就是说控制对象生存周期的不再是引用它的对象,而是spring。对于某个具体的对象而言,以前是它控制其他对象,现在是所有对象都被spring控制,所以这叫控制反转。

 

 文中示例程序下载:

(> ' []' )>  点击下载  <( '[] '< ) 

 

posted on 2014-03-05 11:22 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/3582039.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Over the years software systems have evolutionarily become more and more complex. One of the techniques for dealing with this inherent complexity of software systems is dependency injection – a design pattern that allows the removal of hard-coded dependencies and makes it possible to assemble a service by changing dependencies easily, whether at run-time or compile-time. It promotes code reuse and loosely-coupled design which leads to more easily maintainable and flexible code. The guide you are holding in your hands is a primer on using dependency injection with Unity – a lightweight extensible dependency injection container built by the Microsoft patterns & practices team. It covers various styles of dependency injection and also additional capabilities of Unity container, such as object lifetime management, interception, and registration by convention. It also discusses the advanced topics of enhancing Unity with your custom extensions. The guide contains plenty of trade-off discussions and tips and tricks for managing your application cross-cutting concerns and making the most out of both dependency injection and Unity. These are accompanied by a real world example that will help you master the techniques. Keep in mind that Unity can be used in a wide range of application types such as desktop, web, services, and cloud. We encourage you to experiment with the sample code and think beyond the scenarios discussed in the guide. In addition, the guide includes the Tales from the Trenches – a collection of case studies that offer a different perspective through the eyes of developers working on the real world projects and sharing their experiences. These chapters make clear the range of scenarios in which you can use Unity, and also highlight its ease of use and flexibility. Whether you are a seasoned developer or just starting your development journey, we hope this guide will be worth your time studying it. We hope you discover that Unity container adds significant benefits to your applications and helps you to achieve the goals of maintainability, testability, flexibility, and extensibility in your own projects.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值