日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2023/12/18 C# 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。