Castle学习笔记----将Castle IOC引入项目开发中实现“依赖注入”
我在上一篇笔记<Castle学习笔记----初探IOC容器 >里讲到过.
通常IOC实现的步骤为-->建立容器-->加入组件-->获取组件-->使用组件.这篇文章还是以这四个环节来阐述。
一.建立容器
这里我拿手上的一个现成项目来做分析,首先我们得建立IOC容器.项目中是建立了一个容器类Container来专门负责IOC容器的搭建及组件的加入.代码如下:
二 . 加入组件
加入组件的过程在上一部建立容器的时候我们已经指定让他去配置文件中查找。看看下面代码:
<configSections>
<!-- Specify the castle section handler -->
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
上面我门定义了castle配置节点,那castle的配置节点该怎么去配置呢,看看下面的配置代码:
IUserInfoService user = Container.Instance.Resolve<IUserInfoService>();
下面是调用代码:
通常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类的静态方法。
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

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

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

3




上面我门定义了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是一个详细的组件配置器。下面我们看看组件配置器的具体配置:
2

3

4

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

2

3

4

5

6

7

8

上面这样的组件配置到底是什么意思呢?在这里我给大家分析下。
service="TMS.Service.IUserInfoService,TMS.Service"----代表服务(既接口,在TMS.Service这个程序集下)
type="TMS.Component.UserInfoComponent,TMS.Component" --代表组件(既实现服务的类,TMS.Component下)
现在大家应该很清楚的知道上面的配置是什么意思了吧,也就是说TMS.Component程序集下的UserInfoComponent这个组件类实现了TMS.Servie程序集下的UserInfoService这个服务(接口).
三 .获取组件及使用组件
说了半天终于到了最后阶段了。前面的都是为这一步做准备工作,真是享福的在后面,下面来看看怎么获取一个组件并使用这个组件。

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

2

3

4

5

运行结果如下:
Castle IOC示例程序下载
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述