概述
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(); } } }
到這里,算結束了。
自己去復制代碼運行一次,相信你一定能更深刻地理解。