依赖注入 概念演示示例

简介:

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

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 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 {
 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}
复制代码


依赖注入容器

类型字典接口

复制代码
 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}
复制代码

 

类型字典接口的实现


复制代码
 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}
复制代码

类型示例创建相关类

 

复制代码
 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}
复制代码

 

实例生成类

复制代码
 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
复制代码


调用模块

复制代码
 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}
复制代码

 

 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}
复制代码

 

 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}
复制代码

 

 

 

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!

















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2009/02/06/1385437.html ,如需转载请自行联系原作者

相关文章
|
机器学习/深度学习 人工智能 算法
【AI超级美发师】深度学习算法打造染发特效(附代码)
如今,在类似天天P图、美图秀秀等手机APP中,给指定照片或视频中的人物更换头发颜色已经是再正常不过的事情了。那么本文便介绍了该功能背后如AI头发分割模块、头发换色、颜色增强与修正模块等技术原理(附代码)。
3788 0
|
存储 弹性计算 运维
自动回复邮件脚本
【4月更文挑战第30天】
208 1
|
存储 消息中间件 传感器
物联网平台(Link Platform)介绍|学习笔记
快速学习物联网平台(Link Platform)介绍
962 15
物联网平台(Link Platform)介绍|学习笔记
|
算法 Java Windows
Guava-RateLimiter详解
常用的限流算法有漏桶算法和令牌桶算法,guava的RateLimiter使用的是令牌桶算法,也就是以固定的频率向桶中放入令牌,例如一秒钟10枚令牌,实际业务在每次响应请求之前都从桶中获取令牌,只有取到令牌的请求才会被成功响应,获取的方式有两种:阻塞等待令牌或者取不到立即返回失败,下图来自网上: ratelimite原理图 本次实战,我们用的是guava的RateLimiter,场景是spring mvc在处理请求时候,从桶中申请令牌,申请到了就成功响应,申请不到时直接返回失败。
3131 0
|
存储 弹性计算 固态存储
阿里云服务器价格表新鲜出炉来看看吧!
阿里云服务器一年多少钱?阿里云服务器租用费用?阿里云服务器CPU内存怎么收费?阿里云服务器公网带宽收费标准价格表
1677 0
阿里云服务器价格表新鲜出炉来看看吧!
在openstack云平台中,使用命令行创建云主机操作步骤
在openstack云平台中,使用命令行创建云主机操作步骤
712 0
在openstack云平台中,使用命令行创建云主机操作步骤
|
编解码 人工智能 物联网
|
存储 分布式计算 搜索推荐
1.1 云计算的产生背景|学习笔记
快速学习1.1 云计算的产生背景
|
数据采集 运维 监控
SRE技术保障平台-盯屏中心TAC在混合云项目中的应用实践
SRE技术保障平台-盯屏中心TAC在混合云项目中的应用实践
SRE技术保障平台-盯屏中心TAC在混合云项目中的应用实践