最近开始研究依赖注入。刚刚开始研究这个东西,遇到不少问题。首先,概念就理解的不是很清楚,更不要说代码实现点什么了。
Google之下找到一片好文:http://tech.it168.com/w/d/2007-07-10/200707100933943.shtml
看了下,对依赖注入理解清晰了不少。
不过,感觉作者为了简单,把所有的代码文件都放在了一个dll中。我觉得这样并不会
对读者理解依赖注入有帮助,所以重新整理了下,放在这里和大家分享。欢迎提出批评意见!
以下是代码清单:
接口层
1
namespace
IoCTest.Interface
2
{
3
public interface IWeatherReader
4
{
5
string Current { get; set; }
6
}
7
}
namespace
IoCTest.Interface2
{3
public interface IWeatherReader4
{5
string Current { get; set; }6
}7
}
接口的实现部分
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Text;
5
using
IoCTest.Interface;
6
7
namespace
IoCTest.Implementation
8
{
9
public class WeatherReaderImpl : IWeatherReader
10
{
11
private string _weather;
12
public WeatherReaderImpl(string weather)
13
{
14
this._weather = weather;
15
}
16
17
public string Current
18
{
19
get { return _weather; }
20
set { _weather = value; }
21
}
22
}
23
}
using
System;2
using
System.Collections.Generic;3
using
System.Linq;4
using
System.Text;5
using
IoCTest.Interface;6

7
namespace
IoCTest.Implementation8
{9
public class WeatherReaderImpl : IWeatherReader10
{11
private string _weather;12
public WeatherReaderImpl(string weather)13
{14
this._weather = weather;15
}16

17
public string Current18
{19
get { return _weather; }20
set { _weather = value; }21
}22
}23
}
依赖注入容器
类型字典接口
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Text;
5
6
namespace
IoCTest.Container
7
{
8
public interface ITypeMap
9
{
10
TypeConstructor this[Type type] { get; }
11
}
12
}
using
System;2
using
System.Collections.Generic;3
using
System.Linq;4
using
System.Text;5

6
namespace
IoCTest.Container7
{8
public interface ITypeMap9
{10
TypeConstructor this[Type type] { get; }11
}12
}
类型字典接口的实现
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Text;
5
using
IoCTest.Implementation;
6
using
IoCTest.Interface;
7
8
namespace
IoCTest.Container
9
{
10
public class MemoryTypeMap : ITypeMap
11
{
12
private Dictionary<Type, TypeConstructor> _dicType = new Dictionary<Type, TypeConstructor>();
13
private static ITypeMap _typeMapInstance;
14
15
private MemoryTypeMap() { }
16
17
static MemoryTypeMap()
18
{
19
MemoryTypeMap singleton = new MemoryTypeMap();
20
singleton._dicType.Add(typeof(IWeatherReader), new TypeConstructor(typeof(WeatherReaderImpl), "s"));
21
_typeMapInstance = singleton;
22
}
23
24
public static ITypeMap Instance
25
{
26
get { return _typeMapInstance; }
27
}
28
29
public TypeConstructor this[Type type]
30
{
31
get
32
{
33
TypeConstructor constructor;
34
35
if (!_dicType.TryGetValue(type, out constructor))
36
{
37
return null;
38
}
39
else
40
{
41
return constructor;
42
}
43
}
44
}
45
}
46
}
using
System;2
using
System.Collections.Generic;3
using
System.Linq;4
using
System.Text;5
using
IoCTest.Implementation;6
using
IoCTest.Interface;7

8
namespace
IoCTest.Container9
{10
public class MemoryTypeMap : ITypeMap11
{12
private Dictionary<Type, TypeConstructor> _dicType = new Dictionary<Type, TypeConstructor>();13
private static ITypeMap _typeMapInstance;14

15
private MemoryTypeMap() { }16

17
static MemoryTypeMap()18
{19
MemoryTypeMap singleton = new MemoryTypeMap();20
singleton._dicType.Add(typeof(IWeatherReader), new TypeConstructor(typeof(WeatherReaderImpl), "s"));21
_typeMapInstance = singleton;22
}23

24
public static ITypeMap Instance25
{26
get { return _typeMapInstance; }27
}28

29
public TypeConstructor this[Type type]30
{31
get32
{33
TypeConstructor constructor;34

35
if (!_dicType.TryGetValue(type, out constructor))36
{37
return null;38
}39
else40
{41
return constructor;42
}43
}44
}45
}46
}
类型示例创建相关类
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Text;
5
6
namespace
IoCTest.Container
7
{
8
public class TypeConstructor
9
{
10
private Type _type;
11
private object[] _parameters;
12
13
public Type GetType
14
{
15
get { return _type; }
16
}
17
18
public object[] Parameters
19
{
20
get { return _parameters; }
21
}
22
23
public TypeConstructor(Type type, params object[] parameters)
24
{
25
_type = type;
26
_parameters = parameters;
27
}
28
29
public TypeConstructor(Type type):this(type,null)
30
{
31
32
}
33
}
34
}
using
System;2
using
System.Collections.Generic;3
using
System.Linq;4
using
System.Text;5

6
namespace
IoCTest.Container7
{8
public class TypeConstructor9
{10
private Type _type;11
private object[] _parameters;12

13
public Type GetType14
{15
get { return _type; }16
}17

18
public object[] Parameters19
{20
get { return _parameters; }21
}22

23
public TypeConstructor(Type type, params object[] parameters)24
{25
_type = type;26
_parameters = parameters;27
}28

29
public TypeConstructor(Type type):this(type,null)30
{31
32
}33
}34
}
实例生成类
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Text;
5
6
namespace
IoCTest.Container
7
{
8
public class Assembler<T> where T : class
9
{
10
private static ITypeMap _typeMap = MemoryTypeMap.Instance;
11
12
public T Create()
13
{
14
TypeConstructor constructor = _typeMap[typeof(T)];
15
16
if (constructor != null)
17
{
18
if (constructor.Parameters == null)
19
return (T)Activator.CreateInstance(constructor.GetType);
20
else
21
return (T)Activator.CreateInstance(constructor.GetType, constructor.Parameters);
22
}
23
else
24
{
25
return null;
26
}
27
}
28
}
29
}
30
using
System;2
using
System.Collections.Generic;3
using
System.Linq;4
using
System.Text;5

6
namespace
IoCTest.Container7
{8
public class Assembler<T> where T : class9
{10
private static ITypeMap _typeMap = MemoryTypeMap.Instance;11

12
public T Create()13
{14
TypeConstructor constructor = _typeMap[typeof(T)];15

16
if (constructor != null)17
{18
if (constructor.Parameters == null)19
return (T)Activator.CreateInstance(constructor.GetType);20
else21
return (T)Activator.CreateInstance(constructor.GetType, constructor.Parameters); 22
}23
else24
{25
return null;26
}27
}28
}29
}
30
调用模块
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Text;
5
using
IoCTest.Interface;
6
7
namespace
IoCTest.Client
8
{
9
public class Client
10
{
11
private IWeatherReader _reader;
12
public Client(IWeatherReader reader)
13
{
14
this._reader = reader;
15
}
16
17
public string Weather
18
{
19
get { return _reader.Current; }
20
}
21
}
22
}
using
System;2
using
System.Collections.Generic;3
using
System.Linq;4
using
System.Text;5
using
IoCTest.Interface;6

7
namespace
IoCTest.Client8
{9
public class Client10
{11
private IWeatherReader _reader;12
public Client(IWeatherReader reader)13
{14
this._reader = reader;15
}16

17
public string Weather18
{19
get { return _reader.Current; }20
}21
}22
}
Console调用部分
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Text;
5
using
IoCTest.Container;
6
using
IoCTest.Interface;
7
8
namespace
IoCTest
9
{
10
class Program
11
{
12
static void Main(string[] args)
13
{
14
IWeatherReader reader = new Assembler<IWeatherReader>().Create();
15
IoCTest.Client.Client client = new IoCTest.Client.Client(reader);
16
17
Console.WriteLine(reader.Current);
18
}
19
}
20
}
using
System;2
using
System.Collections.Generic;3
using
System.Linq;4
using
System.Text;5
using
IoCTest.Container;6
using
IoCTest.Interface;7

8
namespace
IoCTest9
{10
class Program11
{12
static void Main(string[] args)13
{14
IWeatherReader reader = new Assembler<IWeatherReader>().Create();15
IoCTest.Client.Client client = new IoCTest.Client.Client(reader);16

17
Console.WriteLine(reader.Current);18
}19
}20
}
NUnit测试用代码
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Text;
5
using
IoCTest.Client;
6
using
IoCTest.Container;
7
using
IoCTest.Implementation;
8
using
IoCTest.Interface;
9
using
NUnit.Framework;
10
11
namespace
UnitTest
12
{
13
[TestFixture]
14
public class UnitTest
15
{
16
[SetUp]
17
public void Init()
18
{
19
20
}
21
22
//[Test]
23
//public void TestWhatWeather()
24
//{
25
// IoCTest.Client.Client client = new IoCTest.Client.Client();
26
27
// Assert.AreEqual(client.Weather, "unknown");
28
//}
29
30
[Test]
31
public void TestIsNull()
32
{
33
IWeatherReader reader = new Assembler<IWeatherReader>().Create();
34
Assert.IsNotNull(reader);
35
Assert.AreEqual(typeof(WeatherReaderImpl), reader.GetType());
36
}
37
38
[Test]
39
public void ConstructorTest()
40
{
41
IWeatherReader reader = new Assembler<IWeatherReader>().Create();
42
IoCTest.Client.Client client = new IoCTest.Client.Client(reader);
43
Assert.IsNotNull(client);
44
}
45
}
46
}
using
System;2
using
System.Collections.Generic;3
using
System.Linq;4
using
System.Text;5
using
IoCTest.Client;6
using
IoCTest.Container;7
using
IoCTest.Implementation;8
using
IoCTest.Interface;9
using
NUnit.Framework;10

11
namespace
UnitTest12
{13
[TestFixture]14
public class UnitTest15
{16
[SetUp]17
public void Init()18
{19
20
}21

22
//[Test]23
//public void TestWhatWeather()24
//{25
// IoCTest.Client.Client client = new IoCTest.Client.Client();26

27
// Assert.AreEqual(client.Weather, "unknown");28
//}29

30
[Test]31
public void TestIsNull()32
{33
IWeatherReader reader = new Assembler<IWeatherReader>().Create();34
Assert.IsNotNull(reader);35
Assert.AreEqual(typeof(WeatherReaderImpl), reader.GetType()); 36
}37

38
[Test]39
public void ConstructorTest()40
{41
IWeatherReader reader = new Assembler<IWeatherReader>().Create();42
IoCTest.Client.Client client = new IoCTest.Client.Client(reader);43
Assert.IsNotNull(client); 44
}45
}46
}
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2009/02/06/1385437.html
