Leanpub Header

Skip to main content

.NET 依赖注入

本书内容是关于 .NET 依賴注入(dependency injection;简称 DI)的相关议题。透过本书,您将会了解 DI 的基本概念以及相关的实务应用技术,并协助您设计出更容易维护的应用程序架构。

This book is a translation into Chinese (Simplified) of .NET 相依性注入 which was originally written in Chinese (Traditional)

Minimum price

$13.00

$17.00

You pay

$17.00
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
299
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 背景知识。

建议您先读完本书的试阅章节(包含本书第一章完整内容),以评估这本书是否适合你。

 

Share this book

Categories

This book is a translation into Chinese (Simplified) of .NET 相依性注入 which was originally written in Chinese (Traditional)

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

Contents

Table of Contents

致谢

关于本书

  1. 谁适合阅读本书
  2. 如何阅读本书
  3. 书写惯例
  4. 此区块会提示当前范例源代码的所在位置。
  5. 注意事项。
  6. 相关信息。
  7. 通常是笔者的碎碎念、个人观点(不见得完全正确或放诸四海皆准),或额外的补充说明。
  8. 需要准备的工具
  9. 范例程序与补充材料

关于作者

  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. 本练习的源代码项目为 WebFormsDemo.Autofac.csproj。
  21. Step 1:建立新项目
  22. Step 2:注册类型
  23. Step 3:编写 HTTP Handler
  24. Step 4:注册 HTTP Handler
  25. Step 5:编写测试页面
  26. DI 与 WCF
  27. 问题描述
  28. 解法
  29. 练习:使用 Unity
  30. Step 1:建立 WCF 服务
  31. Step 2:编写自定义的 ServiceHostFactory
  32. Step 3:编写自定义的 ServiceHost
  33. Step 4:实现 IContractBehavior 接口
  34. Step 5:实现 IInstanceProvider 接口
  35. Step 6:设定 Unity 容器
  36. Step 7:修改 Web.config
  37. Step 8:编写客户端程序
  38. 练习:使用 Autofac.Wcf 套件
  39. Step 1:建立 WCF 服务
  40. Step 2:编写自定义的 ServiceHostFactory
  41. Step 3:设定 Autofac 容器
  42. Step 4:修改 Web.config
  43. Step 5:编写客户端程序
  44. 本章回顾
  45. 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. 方便记忆的比喻——如果你能接受的话
  42. Per-Request 生命周期
  43. 阶层式容器
  44. 选择生命周期管理员
  45. 拦截
  46. 使用 Unity 容器实现拦截
  47. Step 1:加入 Unity 的拦截扩展包
  48. Step 2:实现拦截行为
  49. Step 3:注册拦截行为
  50. 结语

附录一: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. 结语

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