将Castle IOC引入项目开发中实现“依赖注入”

      我在上一篇笔记<Castle学习笔记----初探IOC容器 >里讲到过.
      通常IOC实现的步骤为-->建立容器-->加入组件-->获取组件-->使用组件.这篇文章还是以这四个环节来阐述。

一.建立容器
      这里我拿手上的一个现成项目来做分析,首先我们得建立IOC容器.项目中是建立了一个容器类Container来专门负责IOC容器的搭建及组件的加入.代码如下:

  1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.Text;
  4
  5 using  Castle.Windsor;
  6 using  Castle.MicroKernel;
  7 using  Castle.Windsor.Configuration.Interpreters;
  8 using  TMS.Framework.Exceptions;
  9
 10 namespace  TMS.Framework
 11 {
 12    /// <summary>
 13    /// The IoC container which provides interface to find Service. 
 14    /// </summary>

 15    public sealed class Container
 16    {
 17        /// <summary>
 18        /// WindsorContainer object
 19        /// </summary>

 20        private WindsorContainer windsor;
 21        //public WindsorContainer Windsor
 22        //{
 23        //    get { return windsor; }
 24        //}
 25
 26        private IKernel kernel;
 27        public IKernel Kernel
 28        {
 29            get { return kernel; }
 30        }

 31
 32        /// <summary>
 33        /// this
 34        /// </summary>

 35        private static readonly Container instance = new Container();
 36        public static Container Instance
 37        {
 38            get { return Container.instance; }
 39        }

 40
 41        /// <summary>
 42        /// Construction Method.
 43        /// Initialization IOC.
 44        /// </summary>

 45        public Container()
 46        {
 47            try
 48            {
 49                //IOC containers establishment, and through the most dynamic configuration file by adding components.
 50                Castle.Core.Resource.ConfigResource source = new Castle.Core.Resource.ConfigResource();
 51                XmlInterpreter interpreter = new XmlInterpreter(source);
 52                windsor = new WindsorContainer(interpreter);
 53                kernel = windsor.Kernel;
 54            }

 55            catch (BizSystemException bSE)
 56            {
 57                TMSExcetionBase.ProcessBizException(bSE);
 58            }

 59        }

 60
 61        /// <summary>
 62        /// Returns a component instance by the type of service.
 63        /// </summary>
 64        /// <typeparam name="T"></typeparam>
 65        /// <returns></returns>

 66        public T Resolve<T>()
 67        {
 68            return (T)kernel[typeof(T)];
 69        }

 70
 71        /// <summary>
 72        /// Returns a component instance by the service name.
 73        /// </summary>
 74        /// <param name="service"></param>
 75        /// <returns></returns>

 76        private object Resolve(Type service)
 77        {
 78            return kernel[service];
 79        }

 80
 81        /// <summary>
 82        /// Returns a component instance by the service name.
 83        /// </summary>
 84        /// <param name="key"></param>
 85        /// <returns></returns>

 86        private object Resolve(String key)
 87        {
 88            return kernel[key];
 89        }

 90
 91        /// <summary>
 92        /// Release resource that be container used.
 93        /// </summary>

 94        public void Dispose()
 95        {
 96            kernel.Dispose();
 97        }

 98    }

 99}

100
注意:上面代码种的 ProcessBizException()是一个自定义异常处理TMSExcetionBase类的静态方法。

二 . 加入组件
      加入组件的过程在上一部建立容器的时候我们已经指定让他去配置文件中查找。看看下面代码:
1 Castle.Core.Resource.ConfigResource source  =   new  Castle.Core.Resource.ConfigResource();
2 XmlInterpreter interpreter  =   new  XmlInterpreter(source);
3 windsor  =   new  WindsorContainer(interpreter);
     在配置文件的 <configSections>配置节里首先加入下面的配置,关于这点的作用在此就不多解释,详细可以看看我之前写的文章:
< configSections >
     <!--  Specify the castle section handler  -->
     < section name = " castle "  type = " Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor " />

      上面我门定义了castle配置节点,那castle的配置节点该怎么去配置呢,看看下面的配置代码:
1    < castle >
2      < include uri = http://www.cnblogs.com/beniao/admin/file://configuration//TMS.Role.config   />
3      < include uri = http://www.cnblogs.com/beniao/admin/file://configuration//TMS.UserInfo.config   />
4    </ castle >
      这样的配置到底是什么意思呢?在Container类里建立容器的同时就指定了他到配置文件中去找需要加入到容器的组件,组件我们又通过配置文件中配置了castle配置节点,在castle的详细配置中,我们可以看到,使用了include语句加载了指定的组件配置器,<include uri=file://Configuration//TMS.UserInfo.config/>,代表根目录下的Configuration文件下的TMS.UserInfo.config是一个详细的组件配置器。下面我们看看组件配置器的具体配置:
1 <? xml version = " 1.0 "  encoding = " utf-8 "   ?>
2 < configuration >
3    < components >
4      < component id = " UserInfo "  service = " TMS.Service.IUserInfoService,TMS.Service "  
5                type = " TMS.Component.UserInfoComponent,TMS.Component "   />
6    </ components >
7 </ configuration >
8

上面这样的组件配置到底是什么意思呢?在这里我给大家分析下。
service="TMS.Service.IUserInfoService,TMS.Service"----代表服务(既接口,在TMS.Service这个程序集下)
type="TMS.Component.UserInfoComponent,TMS.Component" --代表组件(既实现服务的类,TMS.Component下)
现在大家应该很清楚的知道上面的配置是什么意思了吧,也就是说TMS.Component程序集下的UserInfoComponent这个组件类实现了TMS.Servie程序集下的UserInfoService这个服务(接口).

三 .获取组件及使用组件
    说了半天终于到了最后阶段了。前面的都是为这一步做准备工作,真是享福的在后面,下面来看看怎么获取一个组件并使用这个组件。

通过传入的接口,让容器自动去找实现接口的组件。
IUserInfoService user  =  Container.Instance.Resolve < IUserInfoService > ();

下面是调用代码:
1 protected   void  Page_Load( object  sender, EventArgs e)
2 {
3   this.GridView1.DataSource = user.InquireAll();
4   this.GridView1.DataBind();
5}

运行结果如下:

  
   Castle IOC示例程序下载

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值