Magic Studio

专心做有逼格的APP!

依赖注入 概念演示示例

最近开始研究依赖注入。刚刚开始研究这个东西,遇到不少问题。首先,概念就理解的不是很清楚,更不要说代码实现点什么了。

Google之下找到一片好文:http://tech.it168.com/w/d/2007-07-10/200707100933943.shtml

看了下,对依赖注入理解清晰了不少。

      不过,感觉作者为了简单,把所有的代码文件都放在了一个dll中。我觉得这样并不会

对读者理解依赖注入有帮助,所以重新整理了下,放在这里和大家分享。欢迎提出批评意见!

代码 

以下是代码清单:

接口层

1namespace IoCTest.Interface
2{
3    public interface IWeatherReader
4    {
5        string Current getset; }
6    }

7}

 

接口的实现部分

 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using IoCTest.Interface;
 6
 7namespace 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}


依赖注入容器

类型字典接口

 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5
 6namespace IoCTest.Container
 7{
 8    public interface ITypeMap
 9    {
10        TypeConstructor this[Type type] get; }
11    }

12}

 

类型字典接口的实现


 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using IoCTest.Implementation;
 6using IoCTest.Interface;
 7
 8namespace 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}

类型示例创建相关类

 

 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5
 6namespace 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}

 

实例生成类

 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5
 6namespace 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


调用模块

 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using IoCTest.Interface;
 6
 7namespace 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}

 

 Console调用部分

 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using IoCTest.Container;
 6using IoCTest.Interface;
 7
 8namespace 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}

 

 NUnit测试用代码

 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using IoCTest.Client;
 6using IoCTest.Container;
 7using IoCTest.Implementation;
 8using IoCTest.Interface;
 9using NUnit.Framework;
10
11namespace 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}

 

 

 

posted on 2009-02-06 16:57  Mr 布鲁斯  阅读(273)  评论(0编辑  收藏  举报

导航