反射 + 配置文件 实现IOC容器

来源:互联网 发布:微商城分销php开发手册 编辑:程序博客网 时间:2024/06/01 21:53

IOC实现:


IOC容器我们仅仅停留在知道上是不行的,我们要动手做印象对更深刻,那么我给大家看一个代码,看看代码中IOC容器的实现。


代码实现:


创建一个类库:


解决方案的类库建立:




创建一个实体类:User:


[csharp] view plaincopyprint?
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.   
  5. namespace Spring.Demo.Model  
  6. {  
  7.     /// <summary>  
  8.     /// 用户类  
  9.     /// </summary>  
  10.     public class Users  
  11.     {  
  12.         /// <summary>  
  13.         /// 编号  
  14.         /// </summary>  
  15.         private int _oid;  
  16.         public int Oid  
  17.         {  
  18.             get { return _oid; }  
  19.             set { _oid = value; }  
  20.         }  
  21.   
  22.         /// <summary>  
  23.         /// 姓名  
  24.         /// </summary>  
  25.         private string _name;  
  26.         public string Name  
  27.         {  
  28.             get { return _name; }  
  29.             set { _name = value; }  
  30.         }  
  31.   
  32.         /// <summary>  
  33.         /// 性别  
  34.         /// </summary>  
  35.         private string _sex;  
  36.         public string Sex  
  37.         {  
  38.             get { return _sex; }  
  39.             set { _sex = value; }  
  40.         }  
  41.   
  42.         /// <summary>  
  43.         /// 年龄  
  44.         /// </summary>  
  45.         private int _age;  
  46.         public int Age  
  47.         {  
  48.             get { return _age; }  
  49.             set { _age = value; }  
  50.         }  
  51.     }  
  52. }</span>  

创建IUsers的接口:


[csharp] view plaincopyprint?
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. namespace Spring.Demo.Service  
  5. {  
  6.     public interface IUsers  
  7.     {  
  8.         /// <summary>  
  9.         /// 返回用户的详细信息的方法  
  10.         /// </summary>  
  11.         /// <returns></returns>  
  12.         string GetUserInfo();  
  13.     }  
  14. }  
  15. </span>  

创建一个实现IUsers接口的实现类:


[csharp] view plaincopyprint?
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using Spring.Demo.Service;  
  5. using Spring.Demo.Model;  
  6.   
  7. namespace Spring.Demo.Compontext  
  8. {  
  9.     public class UsersCompontents : IUsers  
  10.     {  
  11.         public UsersCompontents()  
  12.         { }  
  13.  
  14.         #region 获取用户信息  
  15.         public string GetUserInfo()  
  16.         {  
  17.             Users user = new Users();  
  18.             user.Oid = 1;  
  19.             user.Name = "Beniao";  
  20.             user.Sex = "Boy";  
  21.             user.Age = 25;  
  22.   
  23.             return string.Format("编号:{0}--->姓名:{1}--->性别:{2}--->年龄:{3}",  
  24.                 user.Oid,  
  25.                 user.Name,  
  26.                 user.Sex,  
  27.                 user.Age);  
  28.         }  
  29.         #endregion  
  30.     }  
  31. }</span>  

创建测试类:


[csharp] view plaincopyprint?
  1. <span style="font-size:18px;">using ITOO.Library.Core.AOP;  
  2. using Spring.Context;  
  3. using Spring.Demo.Service;  
  4. using System;  
  5. using System.Collections.Generic;  
  6. using System.Configuration;  
  7. using System.Linq;  
  8. using System.Text;  
  9.   
  10. namespace Sping.Demo.SimpleTest  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.   
  17.             IUsers studentChangeBll = SpringHelper.GetObject<IUsers>("Users");  
  18.   
  19.             Console.WriteLine(studentChangeBll.GetUserInfo());  
  20.             Console.Read();  
  21.         }  
  22.     }  
  23. }</span>  

在控制台程序中创建一个配置文件:


[csharp] view plaincopyprint?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <configSections>  
  4.     <sectionGroup name="spring">  
  5.       <section name="context"  
  6.                type="Spring.Context.Support.ContextHandler, Spring.Core"/>  
  7.       <section name="objects"  
  8.                type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />  
  9.     </sectionGroup>  
  10.   </configSections>  
  11.   <spring>  
  12.     <context>  
  13.       <resource uri="config://spring/objects"/>  
  14.     </context>  
  15.     <objects xmlns="http://www.springframework.net">  
  16.       <!--这的配置根据实际的程序来的,UsersCompontents是程序集Spring.Demo.Compontext下的一个类-->  
  17.       <object name="Users"  
  18.               type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontext"  singleton="false" >  
  19.       </object>  
  20.     </objects>  
  21.   </spring>  
  22. </configuration></span>  

运行后,发现SpringHelper却小引用。我们一般写代码中我们是这样写的:


[csharp] view plaincopyprint?
  1. <span style="font-size:18px;">//从config文件中取得程序集信息  
  2. IApplicationContext context = ConfigurationManager.GetSection("spring/context")  
  3.                                as IApplicationContext;  
  4. //调用方法  
  5. //Users为config文件里的配置节  
  6. //<object name="Users"         
  7. //        type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontent">  
  8. //</object>  
  9. IUsers user = context.GetObject("Users"as IUsers;</span>  

这样我们就可以从配置文件中将对象取出来,但是我们都不想在代码中有多余的代码,不能每一次new对象的时候,我们都要写一遍这句话:IApplicationContext context = ConfigurationManager.GetSection("spring/context") as IApplicationContext;这样就增加了我们维护代码的成本,因此,我们将这句话封装起来,封装的代码是这样的:


创建一个类:SpringHelper:


[csharp] view plaincopyprint?
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Spring.Context;  
  6. using Spring.Context.Support;  
  7.   
  8. namespace ITOO.Library.Core.AOP  
  9. {  
  10.     public class SpringHelper  
  11.     {  
  12.         /// <summary>  
  13.         /// Spring容器上下文  
  14.         /// </summary>  
  15.         private static IApplicationContext SpringContext  
  16.         {  
  17.             get  
  18.             {  
  19.                 return ContextRegistry.GetContext();  
  20.             }  
  21.         }  
  22.   
  23.   
  24.         /// <summary>  
  25.         /// 获取配置文件 配置的 对象  
  26.         /// </summary>  
  27.         /// <typeparam name="T"></typeparam>  
  28.         /// <param name="objName"></param>  
  29.         /// <returns></returns>  
  30.         public static T GetObject<T>(string objName) where T : class  
  31.         {  
  32.             return (T)SpringContext.GetObject(objName);  
  33.         }  
  34.     }  
  35. }  
  36.   
  37.   
  38. </span>  

以上的代码我们就可以将每次读取配置文件中的那句话去掉了,我们直接就可以写这样一句话就可以了:IUsers studentChangeBll = SpringHelper.GetObject<IUsers>("Users");


这里体现了封装的重要性,先前我在做AOP的时候,我的师傅看到了类似这样的代码的时候,他就跟我讨论过这个问题,我当时懵懵懂懂,没有进行下一步的行动,现在想想,问题出现在我根本没有动手去做,或者知识没有深入到那个层次,认识这个知识的方面没有那么深。所有问题,都要动手去做才行。


总结:


我们从上面的实践到分析之后,我们发现其实我们看似是新的东西,其实我们已经学习过了,就像IOC容器一样,我们学习过了反射和配置文件,我们发现其实IOC容器不就是反射和配置文件来实现的吗,反射和配置文件是我们在大话设计模式中就已经学习到了的东西,这都不是新的东西。一个看似复杂的东西,都是有简单的东西来组装成的,我们知道这个,就不会对新的东西有畏惧感了。

0 0