依赖注入 概念演示示例

简介:

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

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

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

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

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

代码 

以下是代码清单:

接口层 

复制代码
1 namespace  IoCTest.Interface
2 ExpandedBlockStart.gif {
3    public interface IWeatherReader
4ExpandedSubBlockStart.gif    {
5ExpandedSubBlockStart.gif        string Current getset; }
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 ExpandedBlockStart.gif {
 9    public class WeatherReaderImpl : IWeatherReader
10ExpandedSubBlockStart.gif    {
11        private string _weather;
12        public WeatherReaderImpl(string weather)
13ExpandedSubBlockStart.gif        {
14            this._weather = weather;
15        }

16
17        public string Current
18ExpandedSubBlockStart.gif        {
19ExpandedSubBlockStart.gif            get return _weather; }
20ExpandedSubBlockStart.gif            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 ExpandedBlockStart.gif {
 8    public interface ITypeMap
 9ExpandedSubBlockStart.gif    {
10ExpandedSubBlockStart.gif        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 ExpandedBlockStart.gif {
10    public class MemoryTypeMap : ITypeMap
11ExpandedSubBlockStart.gif    {
12        private Dictionary<Type, TypeConstructor> _dicType = new Dictionary<Type, TypeConstructor>();
13        private static ITypeMap _typeMapInstance;
14
15ExpandedSubBlockStart.gif        private MemoryTypeMap() { }
16
17        static MemoryTypeMap()
18ExpandedSubBlockStart.gif        {
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
25ExpandedSubBlockStart.gif        {
26ExpandedSubBlockStart.gif            get return _typeMapInstance; }
27        }

28
29        public TypeConstructor this[Type type]
30ExpandedSubBlockStart.gif        {
31            get
32ExpandedSubBlockStart.gif            {
33                TypeConstructor constructor;
34
35                if (!_dicType.TryGetValue(type, out constructor))
36ExpandedSubBlockStart.gif                {
37                    return null;
38                }

39                else
40ExpandedSubBlockStart.gif                {
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 ExpandedBlockStart.gif {
 8    public class TypeConstructor
 9ExpandedSubBlockStart.gif    {
10        private Type _type;
11        private object[] _parameters;
12
13        public Type GetType
14ExpandedSubBlockStart.gif        {
15ExpandedSubBlockStart.gif            get return _type; }
16        }

17
18        public object[] Parameters
19ExpandedSubBlockStart.gif        {
20ExpandedSubBlockStart.gif            get return _parameters; }
21        }

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

28
29        public TypeConstructor(Type type):this(type,null)
30ExpandedSubBlockStart.gif        {
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 ExpandedBlockStart.gif {
 8    public class Assembler<T> where T : class
 9ExpandedSubBlockStart.gif    {
10        private static ITypeMap _typeMap = MemoryTypeMap.Instance;
11
12        public T Create()
13ExpandedSubBlockStart.gif        {
14            TypeConstructor constructor = _typeMap[typeof(T)];
15
16            if (constructor != null)
17ExpandedSubBlockStart.gif            {
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
24ExpandedSubBlockStart.gif            {
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 ExpandedBlockStart.gif {
 9    public class Client
10ExpandedSubBlockStart.gif    {
11        private IWeatherReader _reader;
12        public Client(IWeatherReader reader)
13ExpandedSubBlockStart.gif        {
14            this._reader = reader;
15        }

16
17        public string Weather
18ExpandedSubBlockStart.gif        {
19ExpandedSubBlockStart.gif            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 ExpandedBlockStart.gif {
10    class Program
11ExpandedSubBlockStart.gif    {
12        static void Main(string[] args)
13ExpandedSubBlockStart.gif        {
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 ExpandedBlockStart.gif {
13    [TestFixture]
14    public class UnitTest
15ExpandedSubBlockStart.gif    {
16        [SetUp]
17        public void Init()
18ExpandedSubBlockStart.gif        {
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()
32ExpandedSubBlockStart.gif        {
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()
40ExpandedSubBlockStart.gif        {
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
相关文章
|
5月前
|
C++
【C++】bind绑定包装器全解(代码演示,例题演示)
【C++】bind绑定包装器全解(代码演示,例题演示)
|
4月前
|
XML Java 数据格式
Spring5系列学习文章分享---第三篇(AOP概念+原理+动态代理+术语+Aspect+操作案例(注解与配置方式))
Spring5系列学习文章分享---第三篇(AOP概念+原理+动态代理+术语+Aspect+操作案例(注解与配置方式))
37 0
Threejs入门进阶实战案例(6):对象的通用属性/函数
Threejs入门进阶实战案例(6):对象的通用属性/函数
46 0
|
5月前
|
存储 C++
【C++】function包装器全解(代码演示,例题演示)
【C++】function包装器全解(代码演示,例题演示)
|
5月前
|
编译器 C++
【C++】lambda表达式语法详细解读(代码演示,要点解析)
【C++】lambda表达式语法详细解读(代码演示,要点解析)
|
设计模式 NoSQL Java
如何用最简单的方式解释依赖注入?
如何用最简单的方式解释依赖注入?
73 0
|
NoSQL Redis
如何用最简单的方式解释依赖注入?依赖注入是如何实现解耦的?
如何用最简单的方式解释依赖注入?依赖注入是如何实现解耦的?
58 0
|
前端开发
前端学习案例3-Aop切面编程3 原
前端学习案例3-Aop切面编程3 原
56 0
前端学习案例3-Aop切面编程3 原
|
XML Java 程序员
【如何让代码变“高级”(一)】-Spring组合注解提升代码维度(这么有趣)
在定义某个类或接口时,使用了Spring自带的注解(@Controller、@Service,@Conditional),同时又要使用公司特定的注解标注公司的业务,接着就出现了以下处理方式的那一幕。
248 0