Unity 1.2使用初探(1)

來源:互聯網
上載者:User
Unity 1.2使用初探(1)

最近賦閑在家,突然想看一點東西。

以前都有用Enterprise Library,對於PolicyInject可使項目輔助功Log,Auth等以更優雅的方式,歎其巧妙。現在微軟出了Unity1.2注入容器,但是我在使用這個架構上還是個新手,所以在此學習如何應用。(: http://www.codeplex.com/unity )

下面是我的一些初步使用,主要參照在 UnityDocs,路過的各位高手哪位有比較完好的中午資料,請是否可共用一下~~;

1,第一個Demo;

以下是一個Demo:首先定義1個介面,1個實現:

 

public interface ILogService

{

void Write(string message);

}

 

public class CnsLogService: ILogService

{

#region ILogService 成員

 

public void Write(string message) {

Console.WriteLine(String.Format("Cns-exception msg:{0}", message));

}

 

#endregion

}

 

然後我們可以這樣應用Unity:

public static void Main(string[] args) {

 

IUnityContainer myContainer = new UnityContainer();

myContainer.RegisterType<ILogService, CnsLogService>();

ILogService myServiceInstance = myContainer.Resolve<ILogService>();

myServiceInstance.Write("oh,exception occured!");

}

運行結果如:

 

2, 注入鏈

發現文檔有這樣一段內容

大體意思就是你可以註冊一連串的類型。代碼如上所示:

那麼來實現一下:

先再實現一次ILogService

public class DataLogService:ILogService

{

#region ILogService 成員

 

public void Write(string message) {

DailyPractice.Utility.Log.AddLog(message);

}

 

#endregion

}

 

namespace DailyPractice.Utility

{

public class Log

{

public static void AddLog(string message) {

//insert into Log(message) values(@message)

Console.WriteLine(String.Format("Data-exception msg:{0}", message));

}

}

}

 

然後實現一下註冊:

public static void Main(string[] args) {

IUnityContainer myContainer = new UnityContainer();

myContainer.RegisterType<ILogService, CnsLogService>()

.RegisterType<ILogService, DataLogService>();

ILogService myServiceInstance = myContainer.Resolve<ILogService>();

myServiceInstance.Write("oh,exception occured!");

 

}

結果執行為:

非常意外: CnsLogService並沒有執行

然後把代碼改成

public static void Main(string[] args) {

IUnityContainer myContainer = new UnityContainer();

myContainer.RegisterType<ILogService, CnsLogService>()

.RegisterType<ILogService, DataLogService>();

IEnumerable<ILogService> myServiceInstances = myContainer.ResolveAll<ILogService>();

foreach (ILogService myServiceInstance in myServiceInstances) { myServiceInstance.Write("haha, you have an exception 了吧!"); }

}

執行結果是:

什麼都沒有執行???

看過terrylee的blog:http://www.cnblogs.com/terrylee/archive/2008/02/21/unity-application-block-part1.html

發現還有一種實現方式,先看代碼:

public static void Main(string[] args) {

IUnityContainer myContainer = new UnityContainer();

myContainer.RegisterType<ILogService, CnsLogService>("CnsLogService")

.RegisterType<ILogService, DataLogService>("DataLogService");

IEnumerable<ILogService> myServiceInstances = myContainer.ResolveAll<ILogService>();

foreach (ILogService myServiceInstance in myServiceInstances) { myServiceInstance.Write("haha, you have an exception 了吧!"); }

}

執行結果是:

執行結果正常,

所以現在就來找原因:terrylee's blog有這麼一段

    除了可以擷取單個對象執行個體之外,我們還可以一次擷取容器中所有與某一介面映射的所有對象執行個體,但是需要依賴於在註冊映射時提供的名稱,如果沒有指定名稱,通過GetAll方法不會被擷取到。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.