C# 对轻量级(IoC Container)依赖注入Unity的使用
概述
Unity是一個輕量級的可擴展的依賴注入容器,支持構造函數,屬性和方法調用注入。Unity可以處理那些從事基于組件的軟件工程的開發人員所面對的問題。構建一個成功應用程序的關鍵是實現非常松散的耦合設計。松散耦合的應用程序更靈活,更易于維護。這樣的程序也更容易在開發期間進行測試。你可以模擬對象,具有較強的具體依賴關系的墊片(輕量級模擬實現),如數據庫連接,網絡連接,ERP連接,和豐富的用戶界面組件。例如,處理客戶信息的對象可能依賴于其他對象訪問的數據存儲,驗證信息,并檢查該用戶是否被授權執行更新。依賴注入技術,可確??蛻纛愓_實例化和填充所有這些對象,尤其是在依賴可能是抽象的 。
?
Unity 配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration><configSections><section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/></configSections><unity xmlns="http://schemas.microsoft.com/practices/2010/unity"><container><!--register type="full class name,namespace"--><register type="UnityTest.ISqlHelper,UnityTest" mapTo="UnityTest.MysqlHelper,UnityTest"><lifetime type="singleton"/></register></container></unity> </configuration>需要注意的是type和mapTo的值,用逗號隔開兩部分,一是類的全部,包括命名空間,二是命名空間。
那么,也有其他的方法,先設置好命名空間,那就直接寫類名即可,這個就不說了。
這里是簡單的配置,詳細的的配置自行搜索。
?
下載與引用
到官方下載:http://unity.codeplex.com/
項目里引用dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Configuration.dll
?
程序
假設對數據庫操作類進行更換,那先建立一個操作類的接口,具體實現留著派生的類。
操作類接口
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace UnityTest {public interface ISqlHelper{string SqlConnection();}public interface IOtherHelper{string GetSqlConnection();} }?
派生類一:Ms SQL Server
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace UnityTest {public class MssqlHelper : ISqlHelper{public string SqlConnection(){return "this mssql.";}} }派生類二:MySQL
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace UnityTest {public class MysqlHelper : ISqlHelper{public string SqlConnection(){return "this mysql.";}} }其他類
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace UnityTest {public class MyOtherHelper : IOtherHelper{ISqlHelper sql;public MyOtherHelper(ISqlHelper sql){this.sql = sql;}public string GetSqlConnection(){return this.sql.SqlConnection();}} }?
主程序調用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using Microsoft.Practices.Unity; using Microsoft.Practices.Unity.Configuration;namespace UnityTest {class Program{static void Main(string[] args){IUnityContainer mycontainer = new UnityContainer();//已有對象實例的配置容器注冊// MysqlHelper d = new MysqlHelper();//mycontainer.RegisterInstance<ISqlHelper>(d);//類型的配置容器注冊//mycontainer.RegisterType<ISqlHelper, MysqlHelper>();//配置文件注冊UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");section.Configure(mycontainer);//mycontainer.LoadConfiguration(); //調用依賴ISqlHelper mysql = mycontainer.Resolve<ISqlHelper>();Console.WriteLine(mysql.SqlConnection());//構造函數注入mycontainer.RegisterType<IOtherHelper, MyOtherHelper>();IOtherHelper other = mycontainer.Resolve<IOtherHelper>();Console.WriteLine(other.GetSqlConnection());Console.ReadKey();}} }?
到這里,算結束了。
自己去復制代碼運行一次,相信你一定能更深刻地理解。
?
?
轉載于:https://www.cnblogs.com/xusion/archive/2013/05/08/3067274.html
總結
以上是生活随笔為你收集整理的C# 对轻量级(IoC Container)依赖注入Unity的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为何不精通C? 03 深入剖析声明
- 下一篇: 我的收藏