我在上一篇筆記<Castle學習筆記----初探IOC容器 >裏講到過.
通常IOC實現的步驟爲-->建立容器-->加入組件-->獲取組件-->使用組件.這篇文章還是以這四個環節來闡述。
一.建立容器
這裏我拿手上的一個現成項目來做分析,首先我們得建立IOC容器.項目中是建立了一個容器類Container來專門負責IOC容器的搭建及組件的加入.代碼如下:
2using System.Collections.Generic;
3using System.Text;
4
5using Castle.Windsor;
6using Castle.MicroKernel;
7using Castle.Windsor.Configuration.Interpreters;
8using TMS.Framework.Exceptions;
9
10namespace 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
二 . 加入組件
加入組件的過程在上一部建立容器的時候我們已經指定讓他去配置文件中查找。看看下面代碼:
2XmlInterpreter interpreter = new XmlInterpreter(source);
3windsor = new WindsorContainer(interpreter);
<!-- Specify the castle section handler -->
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
上面我門定義了castle配置節點,那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>
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這個服務(接口).
三 .獲取組件及使用組件
說了半天終於到了最後階段了。前面的都是爲這一步做準備工作,真是享福的在後面,下面來看看怎麼獲取一個組件並使用這個組件。
下面是調用代碼:
2{
3 this.GridView1.DataSource = user.InquireAll();
4 this.GridView1.DataBind();
5}
運行結果如下:
Castle IOC示例程序下載