﻿# Unity Application Block 與 ASP.NET MVC 學習資源整理

[Inversion of control](http://en.wikipedia.org/wiki/Inversion_of_control) (IoC) 與 [Dependency injection](http://en.wikipedia.org/wiki/Dependency_injection) (DI) 是我接觸 [ASP.NET MVC](http://www.asp.net/mvc/) 之後才注意到的一門學問，對其概念十分著迷，但由於中文資訊非常少且概念也有點抽象，所以遲遲沒有上手。上個週末花了不少時間研讀相關資料，並著手寫 Code 練習套用在 ASP.NET MVC 專案中，也算小有心得，特別將一些我看過的文章、資料整理一下，方便有心學習的人。

[Unity Application Block](http://www.codeplex.com/unity) 是微軟 [Enterprise Library](http://www.codeplex.com/entlib) 中多個 Application Block 中的一部份，專注於實做 [Dependency injection](http://en.wikipedia.org/wiki/Dependency_injection) 容器，並支援 constructor, property, and method call 注入，目前實做 DI 的函示庫很多，但由於微軟的文件資源較為豐富，所以我就以 [Unity](http://www.codeplex.com/unity) 為入門學習的套件。

由於 DI ( IoC ) 算是個設計樣式 (Design Pattern)，所以如果日後想要切換不同的 DI Framework 的話其實也不會太困難，因為觀念都是相通的，只要能理解運作的基本觀念，並且至少熟悉一套 DI Framework 就不會有太大問題，本文最後面有 **各家實做的 Dependency Injection 框架比較** 供各位參考。

以下是我研讀過的資料，或許對想學習 Unity / DI / IoC 的人來說會有幫助：

**觀念建立**

-   [Inversion of control - Wikipedia, the free encyclopedia](http://en.wikipedia.org/wiki/Inversion_of_control) /  [控制反转](http://zh.wikipedia.org/wiki/%E6%8E%A7%E5%88%B6%E5%8F%8D%E8%BD%AC)
-   [Dependency injection - Wikipedia, the free encyclopedia](http://en.wikipedia.org/wiki/Dependency_injection)
-   [Inversion of Control Containers and the Dependency Injection pattern](http://martinfowler.com/articles/injection.html)

-   此文作者是 [Martin Fowler](http://martinfowler.com) ( 應該是最早想出這個詞彙的人 )

-   [Introduction to Unity](http://msdn.microsoft.com/en-us/library/dd203206.aspx)
-   [Dependency Injection](http://msdn.microsoft.com/en-us/magazine/cc163739.aspx) \[ MSDN Magazine, 2005-09 \]

**入門教學**

-   [Unity Dependency Injection IoC Screencast](http://www.pnpguidance.net/Screencast/UnityDependencyInjectionIoCScreencast.aspx) ( 影片教學，看完後自己實做一遍應該就會了 )

-   [Unity IoC and ASP.NET MVC Framework - Dependency Injection of Controllers](http://www.pnpguidance.net/Post/UnityIoCASPNETMVCFrameworkDependencyInjectionControllers.aspx) ( 實用
-   [Unity Nested Containers - IUnityContainer and CreateChildContainer](http://www.pnpguidance.net/Post/UnityNestedContainersIUnityParentContainerCreateChildContainer.aspx)
-   [UnityContainer and UnityConfigurationSection - Populating Unity Container via App.config or Web.config](http://www.pnpguidance.net/Post/UnityContainerUnityConfigurationSectionAppConfigWebConfig.aspx)
-   [Unity Configuration from Separate Configuration File](http://www.pnpguidance.net/Post/UnityConfigurationSeparateConfigurationFile.aspx)

-   [Unity framework with ASP.Net MVC Framework](http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/unityinaspmvc07102009032406AM/unityinaspmvc.aspx) ( 詳細的入門教學 )
-   [ASP.NET MVC Tip: Dependency Injection with Unity Application Block](https://weblogs.asp.net/shijuvarghese/asp-net-mvc-tip-dependency-injection-with-unity-application-block) ( 入門教學 )

**官方網站**

-   [Unity Application Block](http://msdn.microsoft.com/en-us/library/dd203101.aspx) ( MSDN )
-   [patterns & practices – Unity](https://github.com/unitycontainer/unity) ( CodePlex )

**相關下載**

-   [下載 Unity Application Block 1.2 - October 2008 ( 目前的穩定版 )](https://github.com/unitycontainer/unity)

-   預設安裝目錄為 C:\\Program Files\\Microsoft Unity Application Block 1.2
-   包括: CHM 說明文件、QuickStart 範例程式、Unity 完整原始碼、編譯好的組件

-   [下載 Unity Application Block 1.2 Documentation for Visual Studio 2008](https://github.com/unitycontainer/unity)
-   [下載 Unity Application Block 1.2 Documentation for Visual Studio 2005](https://github.com/unitycontainer/unity)
-   [下載 Unity Application Block 1.2 for Silverlight - December 2008](https://github.com/unitycontainer/unity)

**範例程式**

-   [Unity Application Block (1.2) Hands On Labs](https://github.com/unitycontainer/unity) ( 好物，照著練習一遍就對了 )
-   [lekker](https://web.archive.org/web/20180122044357/http://lekker.codeplex.com:80/) ( 管理食譜的 MVC 網站 )

-   採用技術：ASP.NET MVC, **Unity**, Entity Framework, **VS2008**

-   [P-Blog](https://web.archive.org/web/20171223190835/http://pblog.codeplex.com/) ( 部落格引擎 )

-   採用技術：ASP.NET MVC, NHibernate, Firebird, **Unity**/Spring.NET IoC, NUnit

-   [KiGG](https://github.com/writeameer/Kigg) ( 推文網站 )

-   採用技術：ASP.NET MVC, jQuery, **Unity**, Linq To SQL or Entity Framework,  
    Enterprise Library (Logging & Caching), xUnit.net, … 非常多 …

-   [Shrinkr](https://web.archive.org/web/20180115234932/http://shrinkr.codeplex.com:80/) ( 縮網址服務的 MVC 網站 )

-   採用技術：ASP.NET MVC, jQuery, **Unity**, Entity Framework **4.0**, jQuery, **VS2010**
-   [Shrinkr - Url Shrinking Service Developed with Entity Framework 4.0, Unity, ASP.NET MVC And jQuery (Part 1)](https://web.archive.org/web/20130630090109/http://weblogs.asp.net/rashid/archive/2009/09/10/shrinkr-url-shrinking-service-developed-with-entity-framework-4-0-unity-asp-net-mvc-and-jquery-part-1.aspx)
-   [Shrinkr - Url Shrinking Service Developed with Entity Framework 4.0, Unity, ASP.NET MVC And jQuery (Part 2)](https://web.archive.org/web/20130813104438/http://weblogs.asp.net/rashid/archive/2009/09/13/shrinkr-url-shrinking-service-developed-with-entity-framework-4-0-unity-asp-net-mvc-and-jquery-part-2.aspx)
-   [Shrinkr - Url Shrinking Service Developed with Entity Framework 4.0, Unity, ASP.NET MVC And jQuery (Part 3)](https://web.archive.org/web/20130630085841/http://weblogs.asp.net/rashid/archive/2009/09/15/shrinkr-url-shrinking-service-developed-with-entity-framework-4-0-unity-asp-net-mvc-and-jquery-part-3.aspx)

**相關文章**

-   [Inject Some Life into Your Applications—Getting to Know the Unity Application Block](http://msdn.microsoft.com/en-us/library/cc816062.aspx)

-   文章的範例專案下載：[UnityGettingStartedCode.zip](https://github.com/unitycontainer/unity/releases)
-   [Tame Your Software Dependencies for More Flexible Apps](http://msdn.microsoft.com/en-us/magazine/cc337885.aspx) \[ MSDN Magazine, 2008-03 \]

-   [Applying Dependency Injection in ASP.NET MVC NerdDinner.com Application](https://weblogs.asp.net/shijuvarghese/applying-dependency-injection-in-asp-net-mvc-nerddinner-com-application)
-   [ASP.NET MVC, UNITY AND COMMON SERVICE LOCATOR](https://web.archive.org/web/20130620222739/http://weblogs.asp.net/rashid/archive/2009/02/15/asp-net-mvc-unity-and-common-service-locator.aspx)
-   [How To Use Unity Container In ASP.NET](https://web.archive.org/web/20131023072218/http://blogs.microsoft.co.il/blogs/gilf/archive/2008/07/01/how-to-use-unity-container-in-asp-net.aspx)
-   How To Use Unity Container In ASP.NET MVC Framework

**部落格**

-   [Patterns and Practices Guidance – Unity](http://www.pnpguidance.net/Category/Unity.aspx)
-   [Unity - Kazi Manzur Rashid's Blog](https://web.archive.org/web/20130514000023/http://weblogs.asp.net/rashid/archive/tags/Unity/default.aspx)
-   [Brad Wilson](http://bradwilson.typepad.com/blog/)
-   [Being Scott Densmore – Unity](https://learn.microsoft.com/en-us/archive/blogs/scottdensmore/)
-   [I Think It’s Interesting - Dependency Injection](http://blog.ashmind.com/index.php/category/agile/dependency-injection/)
-   [Martin Fowler’s Bliki](http://martinfowler.com/bliki/)

**各家實做的 Dependency Injection 框架比較**

-   [Comparing .NET DI (IoC) Frameworks, Part 1](http://blog.ashmind.com/index.php/2008/08/19/comparing-net-di-ioc-frameworks-part-1/)
-   [Comparing .NET DI (IoC) Frameworks, Part 2](http://blog.ashmind.com/index.php/2008/09/08/comparing-net-di-ioc-frameworks-part-2/)
-   [List of .NET Dependency Injection Containers (IOC)](http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx)
-   [Which single IoC/DI container would you recommend using and why?](http://stackoverflow.com/questions/71041/which-single-ioc-di-container-would-you-recommend-using-and-why)
-   [Which Dependency Injection Tool Should I Use?](http://stackoverflow.com/questions/148908/which-dependency-injection-tool-should-i-use)
-   [IoC Container Benchmark - Unity, Windsor, StructureMap and Spring.NET](http://www.codinginstinct.com/2008/04/ioc-container-benchmark-unity-windsor.html)
