Inversion of control (IoC) 與 Dependency injection (DI) 是我接觸 ASP.NET MVC 之後才注意到的一門學問,對其概念十分著迷,但由於中文資訊非常少且概念也有點抽象,所以遲遲沒有上手。上個週末花了不少時間研讀相關資料,並著手寫 Code 練習套用在 ASP.NET MVC 專案中,也算小有心得,特別將一些我看過的文章、資料整理一下,方便有心學習的人。
Unity Application Block 是微軟 Enterprise Library 中多個 Application Block 中的一部份,專注於實做 Dependency injection 容器,並支援 constructor, property, and method call 注入,目前實做 DI 的函示庫很多,但由於微軟的文件資源較為豐富,所以我就以 Unity 為入門學習的套件。
由於 DI ( IoC ) 算是個設計樣式 (Design Pattern),所以如果日後想要切換不同的 DI Framework 的話其實也不會太困難,因為觀念都是相通的,只要能理解運作的基本觀念,並且至少熟悉一套 DI Framework 就不會有太大問題,本文最後面有 各家實做的 Dependency Injection 框架比較 供各位參考。
以下是我研讀過的資料,或許對想學習 Unity / DI / IoC 的人來說會有幫助:
觀念建立
- Inversion of control - Wikipedia, the free encyclopedia / 控制反转
- Dependency injection - Wikipedia, the free encyclopedia
- Inversion of Control Containers and the Dependency Injection pattern
- 此文作者是 Martin Fowler ( 應該是最早想出這個詞彙的人 )
- Introduction to Unity
- Dependency Injection [ MSDN Magazine, 2005-09 ]
入門教學
- Unity Dependency Injection IoC Screencast ( 影片教學,看完後自己實做一遍應該就會了 )
- Unity framework with ASP.Net MVC Framework ( 詳細的入門教學 )
- ASP.NET MVC Tip: Dependency Injection with Unity Application Block ( 入門教學 )
官方網站
- Unity Application Block ( MSDN )
- patterns & practices – Unity ( CodePlex )
相關下載
- 下載 Unity Application Block 1.2 - October 2008 ( 目前的穩定版 )
- 預設安裝目錄為 C:\Program Files\Microsoft Unity Application Block 1.2
- 包括: CHM 說明文件、QuickStart 範例程式、Unity 完整原始碼、編譯好的組件
- 下載 Unity Application Block 1.2 Documentation for Visual Studio 2008
- 下載 Unity Application Block 1.2 Documentation for Visual Studio 2005
- 下載 Unity Application Block 1.2 for Silverlight - December 2008
範例程式
- Unity Application Block (1.2) Hands On Labs ( 好物,照著練習一遍就對了 )
- lekker ( 管理食譜的 MVC 網站 )
- 採用技術:ASP.NET MVC, Unity, Entity Framework, VS2008
- P-Blog ( 部落格引擎 )
- 採用技術:ASP.NET MVC, NHibernate, Firebird, Unity/Spring.NET IoC, NUnit
- KiGG ( 推文網站 )
- 採用技術:ASP.NET MVC, jQuery, Unity, Linq To SQL or Entity Framework,
Enterprise Library (Logging & Caching), xUnit.net, … 非常多 …
- 採用技術:ASP.NET MVC, jQuery, Unity, Linq To SQL or Entity Framework,
- Shrinkr ( 縮網址服務的 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)
- Shrinkr - Url Shrinking Service Developed with Entity Framework 4.0, Unity, ASP.NET MVC And jQuery (Part 2)
- Shrinkr - Url Shrinking Service Developed with Entity Framework 4.0, Unity, ASP.NET MVC And jQuery (Part 3)
相關文章
- Inject Some Life into Your Applications—Getting to Know the Unity Application Block
- 文章的範例專案下載:UnityGettingStartedCode.zip
- Tame Your Software Dependencies for More Flexible Apps [ MSDN Magazine, 2008-03 ]
- Applying Dependency Injection in ASP.NET MVC NerdDinner.com Application
- ASP.NET MVC, UNITY AND COMMON SERVICE LOCATOR
- How To Use Unity Container In ASP.NET
- How To Use Unity Container In ASP.NET MVC Framework
部落格
- Patterns and Practices Guidance – Unity
- Unity - Kazi Manzur Rashid's Blog
- Brad Wilson
- Being Scott Densmore – Unity
- I Think It’s Interesting - Dependency Injection
- Martin Fowler’s Bliki
各家實做的 Dependency Injection 框架比較
- Comparing .NET DI (IoC) Frameworks, Part 1
- Comparing .NET DI (IoC) Frameworks, Part 2
- List of .NET Dependency Injection Containers (IOC)
- Which single IoC/DI container would you recommend using and why?
- Which Dependency Injection Tool Should I Use?
- IoC Container Benchmark - Unity, Windsor, StructureMap and Spring.NET