Leanpub Header

Skip to main content

.NET 相依性注入

使用 Unity

本書內容是關於 .NET 相依性注入(dependency injection;簡稱 DI)的相關議題。透過本書,您將會了解 DI 的基本概念以及相關的實務應用技術,並協助您設計出更容易維護的應用程式架構。

Minimum price

$14.00

$18.00

You pay

$18.00
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
391
Readers
308
Pages
About

About

About the Book

邁向專業開發人員之路...

本書內容是關於.NET相依性注入(dependency injection;簡稱DI)的相關議題。透過本書,您將會瞭解:

  • 什麼是 DI、它有什麼優點、何時使用 DI、以及可能碰到的陷阱。
  • 如何運用 DI 應付容易變動的軟體需求,設計出更彈性、更好維護的程式碼。
  • 與 DI 有關的設計模式。
  • DI 於 .NET 應用程式中的實務應用,包括 ASP.NET MVC、ASP.NET Web API等等。
  • 如何使用 DI 容器(例如 Unity)來協助開發應用程式。

本書的範例皆以C#撰寫,讀者無須具備任何 DI 背景知識。

這不是 .NET 程式設計的入門書。為了避免讀者花錢買了不適合自己的書,容筆者斗膽列舉閱讀本書的基本條件:

  • 熟悉 C# 語法,包括擴充方法(extrension methods)、泛型(generics)、委派(delegates)等等。如果精通的是 VB(或其他 .NET 語言)但不排斥 C# 語法,也是 OK 的。 
  • 具備物件導向的基礎概念,知道何謂封裝、繼承、多型(polymorphism)。

倒不是說,不符合以上條件的讀者就無法從本書汲取有用的東西;只是就一般的情況而言,讀起來會比較辛苦一些。

如果您曾經接觸、研究過設計模式(design patterns),有些章節閱讀起來會輕鬆一些。然而這並非基本要求,因為本書也會一併介紹相關的設計模式與原則,例如 Decorator 模式、Factory 模式、開放/封閉原則(Open/Closed Principle)、單一責任原則(Single Responsibility Principle)等等。

此外,如果下列描述有一些符合您現在的想法,那麼本書也許對您有幫助:

  • 我的日常開發工作需要設計共用的類別庫或框架,供他人使用。
  • 我經常使用第三方(third-party)元件或框架,而且它們都提供了某種程度的 DI 機制。為了充分運用這些機制,我必須了解 DI 的各種用法。
  • 我希望能夠寫出寬鬆耦合、容易維護的程式碼。
  • 我已經開始運用寬鬆耦合的設計原則來寫程式,我想知道更多有關 DI 的細節,以了解有哪些陷阱和迷思,避免設計時犯了同樣的毛病。

無論別人怎麼說,自己讀過的感覺最準。強烈建議您先讀完本書的試閱章節(包含本書前言和第 1 章完整內容),以評估這本書是否適合你。 

Share this book

Categories

Author

About the Author

Michael Tsai

Hi there! I'm a technical writer with 10+ years of experience in software development and 16 awards of Microsoft .NET MVP.

著作:《C# 本事》、《.NET 本事:非同步程式設計》、《.NET 相依性注入》(簡體中文版:.NET 依賴注入)

譯作: 軟體構築美學 (Brownfield Application Development in .NET)、物件導向分析設計與應用(Object-Oriented Analysis and Design with Applications 3rd Edition)、軟體工程與 Microsoft Visual Studio Team System.

部落格: https://www.huanlintalk.com

Translations

Translations

Contents

Table of Contents

致謝

關於本書

  1. 誰適合閱讀本書
  2. 如何閱讀本書
  3. 書寫慣例
  4. 此區塊會提示當前範例原始碼的所在位置。
  5. 注意事項。
  6. 相關資訊。
  7. 通常是筆者的碎碎念、個人觀點(不見得完全正確或放諸四海皆準),或額外的補充說明。
  8. 需要準備的工具
  9. 範例程式與補充資料
  10. 版本更新紀錄

關於作者

  1. Part I:基礎篇

第 1 章:導論

  1. 本章範例原始碼位置:
  2. 為什麼需要 DI?
  3. 動態繫結
  4. 可維護性
  5. 寬鬆耦合
  6. 可測試性
  7. 平行開發
  8. 什麼是 DI?
  9. 入門範例—非 DI 版本
  10. 開放/封閉原則
  11. 入門範例—DI 版本
  12. 提煉介面(Extract Interface)
  13. 控制反轉(IoC)
  14. 何時該用 DI?
  15. 本章回顧

第 2 章:DI 用法與模式

  1. 本章範例原始碼位置:
  2. 設計模式梗概
  3. 小引-電器與介面
  4. Null Object 模式
  5. Decorator 模式
  6. Composite 模式
  7. Adapter 模式
  8. Factory 模式
  9. 注入方式
  10. 建構式注入
  11. 已知應用例
  12. 用法
  13. 範例程式
  14. 屬性注入
  15. 已知應用例
  16. 用法
  17. 範例程式
  18. Strategy 模式
  19. 方法注入
  20. 已知應用例
  21. 用法
  22. 範例
  23. Ambient Context 模式
  24. Context 的中文術語
  25. 已知應用例
  26. 範例程式(一)
  27. 範例程式(二)
  28. Service Locator 模式
  29. 過猶不及-再談建構式注入
  30. 半吊子注入
  31. 阻止相依蔓延
  32. 解決「半吊子注入」
  33. 過度注入
  34. 重構成參數物件
  35. 多載建構函式
  36. 重構成 Façade 模式
  37. 單一責任原則
  38. 本章回顧

第 3 章:DI 容器

  1. 本章範例原始碼位置:
  2. DI 容器簡介
  3. 物件組合
  4. 自製 DI 容器
  5. DI 容器與 Factory 模式
  6. 自製 DI 容器—2.0 版
  7. 現成的 DI 容器
  8. MEF 是 DI 容器嗎?
  9. 物件組合
  10. 使用 XML
  11. 使用程式碼
  12. 自動註冊
  13. 自動或手動?隱含或明確?
  14. 自動匹配
  15. 深層解析
  16. 物件生命週期管理
  17. 記憶體洩漏問題
  18. 生命週期選項
  19. 攔截
  20. AOP 與攔截
  21. 使用 Decorator 模式實現攔截
  22. 本章回顧
  23. Part II:實戰篇

第 4 章:DI 與 ASP.NET MVC 分層架構

  1. 本章範例原始碼位置:
  2. 分層架構概述
  3. 關於洋蔥架構
  4. Repository 模式
  5. Repository 參考資料
  6. MVC 分層架構範例 V1-緊密耦合
  7. 本節範例的原始碼位置:
  8. 領域模型
  9. 資料存取層
  10. 應用程式層
  11. 展現層
  12. 審視目前設計
  13. MVC 分層架構範例 V2-寬鬆耦合
  14. 真實案例:為了方便測試而切換資料來源
  15. 此範例的原始碼位置:
  16. 領域模型
  17. 資料存取層
  18. 應用程式層
  19. 展現層
  20. 組合物件
  21. 切換 Controller 工廠
  22. 審視目前設計
  23. 避免過度設計
  24. YAGNI
  25. MVC 分層架構範例 V3-簡化一些
  26. 此範例的原始碼位置:
  27. 資料存取層
  28. 應用程式層
  29. 展現層
  30. 審視目前設計
  31. 一個 HTTP 請求搭配一個 DbContext
  32. 本節範例的原始碼位置:
  33. ASP.NET MVC 5 的 IDependencyResolver
  34. 實作自訂的 IDependencyResolver 元件
  35. 此範例的原始碼位置:
  36. ASP.NET MVC 5 應用程式的生命週期
  37. 本章回顧

第 5 章:DI 與 ASP.NET Web API

  1. 本章範例原始碼位置:
  2. ASP.NET Web API 管線
  3. 訊息處理常式
  4. Controller 是怎樣建成的?
  5. Controller 建立完成後
  6. 注入物件至 Web API Controller
  7. 抽換 IHttpControllerActivator 服務
  8. 純手工打造
  9. 使用 DI 容器:Unity
  10. 抽換 IDependencyResolver 服務
  11. IDependencyResolver 與 IDependencyScope
  12. 純手工 DI 範例
  13. 步驟 1:實作 IDependencyResolver 介面
  14. 步驟 2:替換預設的型別解析器
  15. 使用 DI 容器:Unity
  16. 使用 DI 容器:Autofac
  17. 本章回顧

第 6 章:更多 DI 實作範例

  1. 本章範例的專案原始碼位置:
  2. 共用程式碼
  3. DI 與 ASP.NET MVC 5
  4. 練習:使用 Unity
  5. Step 1:建立新專案
  6. Step 2:設定 Unity 容器
  7. 在一個 HTTP 請求的範圍內共享同一個物件
  8. Step 3:建立 Controller
  9. DI 與 ASP.NET Web Forms
  10. 問題描述
  11. 解法
  12. 練習:使用 Unity
  13. Step 1:建立新專案
  14. Step 2:註冊型別
  15. Step 3:撰寫 HTTP Handler
  16. Step 4:註冊 HTTP Handler
  17. Step 5:撰寫測試頁面
  18. 練習:使用 Unity 的 BuildUp 方法
  19. 練習:使用 Autofac
  20. Step 1:建立新專案
  21. Step 2:註冊型別
  22. Step 3:撰寫 HTTP Handler
  23. Step 4:註冊 HTTP Handler
  24. Step 5:撰寫測試頁面
  25. DI 與 WCF
  26. 問題描述
  27. 解法
  28. 練習:使用 Unity
  29. Step 1:建立 WCF 服務
  30. Step 2:撰寫自訂的 ServiceHostFactory
  31. Step 3:撰寫自訂的 ServiceHost
  32. Step 4:實作 IContractBehavior 介面
  33. Step 5:實作 IInstanceProvider 介面
  34. Step 6:設定 Unity 容器
  35. Step 7:修改 Web.config
  36. Step 8:撰寫用戶端程式
  37. 練習:使用 Autofac.Wcf 套件
  38. Step 1:建立 WCF 服務
  39. Step 2:撰寫自訂的 ServiceHostFactory
  40. Step 3:設定 Autofac 容器
  41. Step 4:修改 Web.config
  42. Step 5:撰寫用戶端程式
  43. 本章回顧
  44. Part III:工具篇

第 7 章:Unity 學習手冊

  1. 本章範例原始碼位置:
  2. Unity 快速入門
  3. Hello, Unity!
  4. 註冊型別對應
  5. 串接呼叫
  6. 註冊既有物件
  7. 解析
  8. 解析一個物件:Resolve
  9. 具名註冊與解析
  10. 解析多個物件:ResolveAll
  11. 註冊與解析泛型
  12. 檢查註冊
  13. 使用組態檔來設定容器
  14. Unity 組態檔基本格式
  15. 載入組態檔設定
  16. 執行時期組態 vs. 設計時期組態
  17. 註冊與解析-進階篇
  18. 共用的範例程式
  19. 情境
  20. 設計
  21. 程式碼
  22. 自動註冊
  23. 解決重複型別對應的問題
  24. AllClasses 類別
  25. WithMappings 類別
  26. 自動匹配
  27. 自動匹配規則
  28. 手動匹配
  29. 循環參考問題
  30. 注入參數
  31. 解析物件時改寫注入之參數與物件
  32. 注入屬性
  33. 延遲解析
  34. 使用 Lazy<T>
  35. 使用自動工廠
  36. 注入自訂工廠
  37. 物件生命週期管理
  38. 預設的生命週期
  39. 指定生命週期
  40. Transient vs. Per-Resolve
  41. Per-Request 生命週期
  42. 階層式容器
  43. 選擇生命週期管理員
  44. 攔截
  45. InterfaceInterceptor 範例
  46. Step 1:加入 Unity 攔截套件
  47. Step 2:實作攔截行為
  48. Step 3:註冊攔截行為
  49. 本章重點回顧

附錄一:DI 容器實務建議

  1. 容器設定
  2. 避免對同一個組件(DLL)重複掃描兩次或更多次
  3. 使用不同類別來註冊不同用途的元件
  4. 使用非靜態類別來建立與設定 DI 容器
  5. 不要另外建立一個 DLL 專案來集中處理相依關係的解析
  6. 為個別組件加入一個初始化類別來設定相依關係
  7. 掃描組件時,盡量避免指定組件名稱
  8. 生命週期管理
  9. 優先使用 DI 容器來管理物件的生命週期
  10. 考慮使用子容器來管理 Per-Request 類型的物件
  11. 在適當時機呼叫容器的 Dispose 方法
  12. 元件設計相關建議
  13. 避免建立深層的巢狀物件
  14. 考慮使用泛型來封裝抽象概念
  15. 考慮使用 Adapter 或 Façade 來封裝 3rd-party 元件
  16. 不要一律為每個元件定義一個介面
  17. 對於同一層(layer)的元件,可依賴其具象型別
  18. 動態解析
  19. 盡量避免把 DI 容器直接當成 Service Locator 來使用
  20. 考慮使用物件工廠或 Func<T> 來處理晚期繫結

附錄二:初探 ASP.NET 5 的內建 DI 容器

  1. 練習步驟
  2. 步驟 1:建立專案
  3. 步驟 2:加入必要組件
  4. 步驟 3:將 Web API 元件加入 ASP.NET 管線
  5. 步驟 4:加入 API Controller
  6. 步驟 5:撰寫測試用的服務類別
  7. 步驟 6:注入相依物件至 Controller 的建構函式
  8. 結語

版權頁

  1. .NET 相依性注入

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

About the Publisher

About the Publisher

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub