C# 对轻量级(IoC Container)依赖注入Unity的使用

概述
Unity是一个轻量级的可扩展的依赖注入容器,支持构造函数,属性和方法调用注入。Unity可以处理那些从事基于组件的软件工程的开发人员所面对的问题。构建一个成功应用程序的关键是实现非常松散的耦合设计。松散耦合的应用程序更灵活,更易于维护。这样的程序也更容易在开发期间进行测试。你可以模拟对象,具有较强的具体依赖关系的垫片(轻量级模拟实现),如数据库连接,网络连接,ERP连接,和丰富的用户界面组件。

例如,处理客户信息的对象可能依赖于其他对象访问的数据存储,验证信息,并检查该用户是否被授权执行更新。依赖注入技术,可确保客户类正确实例化和填充所有这些对象,尤其是在依赖可能是抽象的 。

Unity 配置文件

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <configSections>
  4. <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  5. </configSections>

  6. <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  7. <container>
  8. <!--register type="full class name,namespace"-->
  9. <register type="UnityTest.ISqlHelper,UnityTest" mapTo="UnityTest.MysqlHelper,UnityTest">
  10. <lifetime type="singleton"/>
  11. </register>
  12. </container>
  13. </unity>
  14. </configuration>
复制代码

需要注意的是type和mapTo的值,用逗号隔开两部分,一是类的全部,包括命名空间,二是命名空间。
那么,也有其他的方法,先设置好命名空间,那就直接写类名即可,这个就不说了。
这里是简单的配置,详细的的配置自行搜索。

下载与引用
到官方下载: http://unity.codeplex.com/
项目里引用dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Configuration.dll

程序
假设对数据库操作类进行更换,那先建立一个操作类的接口,具体实现留着派生的类。
操作类接口

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace UnityTest
  6. {
  7. public interface ISqlHelper
  8. {
  9. string SqlConnection();
  10. }

  11. public interface IOtherHelper
  12. {
  13. string GetSqlConnection();
  14. }
  15. }
复制代码

派生类一:Ms SQL Server

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace UnityTest
  6. {
  7. public class MssqlHelper : ISqlHelper
  8. {
  9. public string SqlConnection()
  10. {
  11. return "this mssql.";
  12. }
  13. }
  14. }
复制代码

派生类二:MySQL

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace UnityTest
  6. {
  7. public class MysqlHelper : ISqlHelper
  8. {
  9. public string SqlConnection()
  10. {
  11. return "this mysql.";
  12. }
  13. }
  14. }
复制代码

其他类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace UnityTest
  6. {
  7. public class MyOtherHelper : IOtherHelper
  8. {
  9. ISqlHelper sql;
  10. public MyOtherHelper(ISqlHelper sql)
  11. {
  12. this.sql = sql;
  13. }
  14. public string GetSqlConnection()
  15. {
  16. return this.sql.SqlConnection();
  17. }


  18. }
  19. }
复制代码

主程序调用

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Configuration;
  6. using Microsoft.Practices.Unity;
  7. using Microsoft.Practices.Unity.Configuration;


  8. namespace UnityTest
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. IUnityContainer mycontainer = new UnityContainer();


  15. //已有对象实例的配置容器注册
  16. // MysqlHelper d = new MysqlHelper();
  17. //mycontainer.RegisterInstance<ISqlHelper>(d);

  18. //类型的配置容器注册
  19. //mycontainer.RegisterType<ISqlHelper, MysqlHelper>();

  20. //配置文件注册
  21. UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
  22. section.Configure(mycontainer);
  23. //mycontainer.LoadConfiguration(); 

  24. //调用依赖
  25. ISqlHelper mysql = mycontainer.Resolve<ISqlHelper>();
  26. Console.WriteLine(mysql.SqlConnection());

  27. //构造函数注入
  28. mycontainer.RegisterType<IOtherHelper, MyOtherHelper>();
  29. IOtherHelper other = mycontainer.Resolve<IOtherHelper>();
  30. Console.WriteLine(other.GetSqlConnection());

  31. Console.ReadKey();

  32. }
  33. }
  34. }
复制代码

到这里,算结束了。
自己去复制代码运行一次,相信你一定能更深刻地理解。



原文链接: http://www.cnblogs.com/xusion/archive/2013/05/08/3067274.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值