Castle IOC容器构建配置详解(二)
主要内容
1.基本类型配置
2.Array类型配置
3.List类型配置
4.Dictionary类型配置
5.自定义类型转换
一.基本类型配置
在Castle IOC的配置文件中,大家可能都已经注意一个问题了,就是不管组件接收的是什么基本数据类型,我们一律没有在配置文件中指定,也就是说,不管组件接收的类型是int型或者是String类型,我们都可以这样去配置:
1
2
3
4
5
|
< component id = "MyComponent" > < parameters > < port >10</ port > </ parameters > </ component > |
这是因为在Castle IOC中,MicroKernel中的SubSystem中有一个TypeConverter,它专门负责类型的转换。参数的注入一般都是通过构造函数或者公有的属性,基本数据类型在配置文件我们不需要用专门的节点去配置,但是对于一些复杂的数据类型久有些不一样。目前Castle IOC能够支持的数据类型如下。
类型 | 节点 | 示例 |
System.Int32, Int16, Int64 | - | <parameters>
<port>10</port> </parameters> |
System.UInt32, UInt16, UInt64 | - | <parameters>
<port>10</port> </parameters> |
System.Char | - | <parameters>
<letter>a</letter> </parameters> |
System.Single, Double, Decimal | - | <parameters>
<threshold>13.22</threshold> </parameters> |
System.String | - | <parameters>
<server>mail.host.com</server> </parameters> |
System.Byte, SByte | - | <parameters>
<rcolor>144</rcolor> </parameters> |
System.Boolean | - | <parameters>
<enabled>0</enabled> </parameters> |
System.DateTime | - | <parameters>
<initial>11022005</initial> </parameters> |
System.Type | - | <parameters>
<type>Components.MyComponent, Components</type> </parameters> |
System.Array | array | 参见后面 |
System.Collections.IList | list | 参见后面 |
System.Collections.IDictionary | dictionary | 参见后面 |
如果有其它的类型,我们需要编写自定义的TypeConverter。
二.Array类型配置
组件构造函数有一个Array的参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class MyComponent { private int[] orders; public int[]Orders { get{ return this.orders;} } public MyComponent() { } public MyComponent(int[]orders) { this.orders = orders; } } |
这时候我们的配置文件可以如下去写
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? xml version = "1.0" encoding = "utf-8" ?> < configuration > < component id = "e" type = "CastleDemo.MyComponent,CastleDemo" > < parameters > < Orders > < item type = "System.Int32" > < item >1</ item > < item >2</ item > < item >3</ item > </ item > </ Orders > </ parameters > </ component > </ configuration > |
三.List类型配置
组件构造函数有一个IList类型的参数
1
2
3
4
5
6
7
8
9
|
public class MyComponent { private IList _hosts; public MyComponent(IList hosts) { this._hosts = hosts; } public IList Hosts { |
这时候我们的配置文件应该如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<? xml version = "1.0" encoding = "utf-8" ?> < configuration > < component id = "mycomponent" type = "CastleDemo.MyComponent,CastleDemo" > < parameters > < hosts > < list type = "System.String" > < item >server1</ item > < item >server2</ item > < item >server3</ item > < item >server4</ item > </ list > </ hosts > </ parameters > </ component > </ configuration > |
四.Dictionary类型配置
组件构造函数有一个Idictionary类型的参数
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class MyComponent { private IDictionary _dictionary; public MyComponent(IDictionary d) { this._dictionary = d; } public IDictionary Dictionary { get{ return this._dictionary;} } // } |
配置文件应该如下去写:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? xml version = "1.0" encoding = "utf-8" ?> < configuration > < component id = "MyComponent" type = "CastleDemo.MyComponent,CastleDemo" > < parameters > < d > < dictionary > < entry key = "a" >a</ entry > < entry key = "b" >b</ entry > < entry key = "c" >c</ entry > </ dictionary > </ d > </ parameters > </ component > </ configuration > |
或者我们可以在配置文件中分别指定Key和Value的数据类型,分别使用keyType和valueType。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? xml version = "1.0" encoding = "utf-8" ?> < configuration > < component id = "MyComponent" type = "CastleDemo.MyComponent,CastleDemo" > < parameters > < d > < dictionary keyType = "System.String, mscorlib" valueType = "System.String, mscorlib" > < entry key = "a" >a</ entry > < entry key = "b" >b</ entry > < entry key = "c" >c</ entry > </ dictionary > </ d > </ parameters > </ component > </ configuration > |
五.自定义类型转换
要实现我们自定义的类型转换,在这之前我们还是花一点时间来看看Castle IOC中是如何实现类型的转换的。在SubSystems中有一个Conversion,专门负责类型的转换,通过一个类型转换器ConversionManager来实现对类型转换的管理,在DefaultConversionManager初始化的时候,会加载以下几个类型转换:
1
2
3
4
5
6
7
8
9
|
protected virtual void InitDefaultConverters() { Add( new PrimitiveConverter() ); Add( new TypeNameConverter() ); Add( new EnumConverter() ); Add( new ListConverter() ); Add( new DictionaryConverter() ); Add( new ArrayConverter() ); } |
这些类型转换器之间的结构图如下:
图1
PrimitiveConverter:负责基本数据类型的转换
TypeNameConverter:负责把一个类型的名字转换成这个类型的实例
EnumConverter:负责枚举类型的转换
ListConverter:负责Ilist数据类型的转换
DictionaryConverter:负责Idictionary数据类型转换
ArrayConverter:负责Array数据类型转换
以其中的PrimitiveConverter为例来看一下它的实现代码:
1
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
|
public class PrimitiveConverter : AbstractTypeConverter { private Type[] types; public PrimitiveConverter() { types = new Type[] { typeof (Char), typeof (DateTime), typeof (Decimal), typeof (Boolean), typeof (Int16), typeof (Int32), typeof (Int64), typeof (UInt16), typeof (UInt32), typeof (UInt64), typeof (Byte), typeof (SByte), typeof (Single), typeof (Double), typeof (String) }; } public override bool CanHandleType(Type type) { return Array.IndexOf(types, type) != -1; } public override object PerformConversion(String value, Type targetType) { if (targetType == typeof(String)) return value; try { return Convert.ChangeType(value, targetType); } catch(Exception ex) { String message = String.Format( "Could not convert from '{0}' to {1}", value, targetType.FullName); throw new ConverterException(message, ex); } } public override object PerformConversion(IConfiguration configuration, Type targetType) { return PerformConversion(configuration.Value, targetType); } } |
可以看到,Castle IOC会把所有的配置参数都当作String类型接收,如果目标类型是String,则直接返回结果,否则再进行类型转换。由此我们可以分析得出,要实现自己的类型转换,有以下两步:
1.编写的自己的类型转换类,实现接口ITypeConverter
1
2
3
4
|
public class MyTypeConverter : ITypeConverter { // } |
2.添加自己的类型转换到ConversionManager中
1
2
3
4
|
IKernel kernel = new DefaultKernel(); IConversionManager conversionMng = (IConversionManager) kernel.GetSubSystem( SubSystemConstants.ConversionManagerKey ); conversionMng.Add(new MyTypeConverter()); |
关于Castle IOC容器中构建配置信息就到这里了,我总共分为了一,二两部分来讲解。Castle IOC系列的文章后续还有很多,希望大家继续关注!
Castle IOC容器构建配置详解(二)的更多相关文章
- Castle IOC容器构建配置详解(一)
主要内容 1.配置什么 2.几种配置方式 3.Include 介绍 4.Properties介绍 5.条件状态 一.配置什么 Castle IOC中并不像Spring.net那样贯穿着一个思想就是一切 ...
- logback -- 配置详解 -- 二 -- <appender>
附: logback.xml实例 logback -- 配置详解 -- 一 -- <configuration>及子节点 logback -- 配置详解 -- 二 -- <appen ...
- Spring源码解析二:IOC容器初始化过程详解
IOC容器初始化分为三个步骤,分别是: 1.Resource定位,即BeanDefinition的资源定位. 2.BeanDefinition的载入 3.向IOC容器注册BeanDefinition ...
- Spring框架 之IOC容器 和AOP详解
主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置 一.S ...
- logback配置详解(二)
<appender> <appender>: <appender>是<configuration>的子节点,是负责写日志的组件. <appende ...
- (转)Apache2 httpd.conf 配置详解 (二)
转之--http://jafy00.blog.51cto.com/2594646/508205 DocumentRoot "/usr/local/apache-2.2.6/htdocs&qu ...
- logback -- 配置详解 -- 四 -- <filter>
附: logback.xml实例 logback -- 配置详解 -- 一 -- <configuration>及子节点 logback -- 配置详解 -- 二 -- <appen ...
- logback -- 配置详解 -- 三 -- <encoder>
附: logback.xml实例 logback -- 配置详解 -- 一 -- <configuration>及子节点 logback -- 配置详解 -- 二 -- <appen ...
- logback -- 配置详解 -- 一 -- <configuration>及子节点
附: logback.xml实例 logback -- 配置详解 -- 一 -- <configuration>及子节点 logback -- 配置详解 -- 二 -- <appen ...
随机推荐
- 【英语】Bingo口语笔记(61) - mind系列
- 【Python】菜鸟的基本课程继续中
同样的缩进表示这段代码处于同一个层次. 每一个print都自带一个换行. 定义变量一定要在使用函数等之前. abs(-14) ======= 取绝对值函数 内建函数 print abs(-14) == ...
- A. Puzzles CodeForces Round #196 (Div.2)
题目的大意是,给你 m 个数字,让你从中选 n 个,使得选出的数字的极差最小. 好吧,超级大水题.因为要极差最小,所以当然想到要排个序咯,然后去连续的 n 个数字,因为数据不大,所以排完序之后直接暴力 ...
- 【转】 [C/OC的那点事儿]NSMutableArray排序的三种实现(依赖学生成绩管理系统).
原文网址:http://blog.csdn.net/ministarler/article/details/17018839 c语言实现的学生成绩管理系统是面向过程的,而OC实现的学生成绩管理系统则是 ...
- unity, sprite atlas
一, Sprite Packer 可以直接在unity里放碎图,只要将Texture Type选为Sprite(2D and UI),Sprite Mode选为Single,再把想打在一张大图里的碎图 ...
- C#格式化小数位的方法
C#格式化字符串还是很方便的,之前一直没用过,也就没有缘分了解 Double dValue = 95.12345; int iValue = 10000; string strValue = &quo ...
- javascript对象之 selectionStart selectionEnd
<script> function inserttag(){ var text=document.getElementById('con'); text.focus(); var star ...
- Effective c++ 小结
来源:http://www.cnblogs.com/feisky/archive/2009/11/04/1595990.html 最近又重新看了Effective C+,不过到现在还是有好多地方不懂的 ...
- 【LeetCode 236】Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- python的使用环境总结
python在linux上运行,使用的是vim,每次都是敲四个空格进行缩进,真尼玛的蛋疼,书本果然是个好东西,从而根据书本python高级编程中的设置配置而来: 1.进行自动补全的脚本 [root@p ...