.NET 依赖注入
.NET 依赖注入
關於本書
本书内容是关于 .NET 相依性注入(dependency injection;简称 DI)的相关议题。透过本书,您将会了解:
- 什么是 DI、它有什么优点、何时使用 DI、以及可能碰到的陷阱。
- 如何运用 DI 应付容易变动的软件需求,设计出更弹性、更好维护的程序代码。
- 与 DI 有关的设计模式。
- DI 于 .NET 应用程序中的实务应用,包括 ASP.NET MVC、ASP.NET Web API 等等。
- 如何使用 DI 容器(例如 Unity)来协助开发应用程序。
本书的范例皆以 C# 撰写,读者无须具备任何 DI 背景知识。
建议您先读完本书的试阅章节(包含本书第一章完整内容),以评估这本书是否适合你。
Table of Contents
-
- 序
- 致谢
-
关于本书
- 谁适合阅读本书
- 如何阅读本书
- 书写惯例
- 需要准备的工具
- 范例程序与补充材料
- 关于作者
-
Part I:基础篇
-
第 1 章:导论
-
为什么需要 DI?
- 可维护性
- 宽松耦合
- 可测试性
- 平行开发
-
什么是 DI?
- 入门范例—非 DI 版本
-
入门范例—DI 版本
- 提炼接口(Extract Interface)
- 控制反转(IoC)
- 何时该用 DI?
- 本章回顾
-
为什么需要 DI?
-
第 2 章:DI 用法与模式
-
设计模式梗概
- 小引-电器与接口
- Null Object 模式
- Decorator 模式
- Composite 模式
- Adapter 模式
- Factory 模式
-
注入方式
-
构造函数注入
- 已知应用例
- 用法
- 范例程序
-
属性注入
- 已知应用例
- 用法
- 范例程序
-
方法注入
- 已知应用例
- 用法
- 范例
-
构造函数注入
-
Ambient Context 模式
- 已知应用例
- 范例程序(一)
- 范例程序(二)
- Service Locator 模式
-
过犹不及-再谈构造函数注入
-
半吊子注入
- 阻止相依蔓延
- 解决「半吊子注入」
-
过度注入
- 重构成参数对象
- 重载构造函数
- 重构成 Façade 模式
-
半吊子注入
- 本章回顾
-
设计模式梗概
-
第 3 章:DI 容器
-
DI 容器简介
- 对象组合
-
自制 DI 容器
- 自制 DI 容器—2.0 版
- 现成的 DI 容器
-
对象组合
- 使用 XML
- 使用代码
- 自动注册
- 自动匹配
- 深层解析
-
对象生命周期管理
- 内存泄漏问题
- 生命周期选项
-
拦截
- 使用 Decorator 模式实现拦截
- 本章回顾
-
DI 容器简介
-
第 1 章:导论
-
Part II:实战篇
-
第 4 章:DI 与 ASP.NET MVC 分层架构
-
分层架构概述
- Repository 模式
-
MVC 分层架构范例 V1-紧密耦合
- 领域模型
- 数据访问层
- 应用层
- 表示层
- 审视目前设计
-
MVC 分层架构范例 V2-宽松耦合
- 领域模型
- 数据访问层
- 应用层
- 表示层
-
组合对象
- 切换 Controller 工厂
- 审视目前设计
- 避免过度设计
-
MVC 分层架构范例 V3-简化一些
- 数据访问层
- 应用层
- 表示层
- 审视目前设计
- 一个 HTTP 请求搭配一个 DbContext
-
ASP.NET MVC 5 的
IDependencyResolver
-
实现自定义的
IDependencyResolver
组件
-
实现自定义的
- 本章回顾
-
分层架构概述
-
第 5 章:DI 与 ASP.NET Web API
-
ASP.NET Web API 管线
- Controller 是怎样建成的?
- 注入对象至 Web API Controller
-
抽换 IHttpControllerActivator 服务
- 纯手工打造
- 使用 DI 容器:Unity
-
抽换 IDependencyResolver 服务
- IDependencyResolver 与 IDependencyScope
-
纯手工 DI 范例
-
步骤 1:实现
IDependencyResolver
接口 - 步骤 2:替换默认的类型解析器
-
步骤 1:实现
- 使用 DI 容器:Unity
- 使用 DI 容器:Autofac
- 本章回顾
-
ASP.NET Web API 管线
-
第 6 章:更多 DI 实现范例
- 共享代码
-
DI 与 ASP.NET MVC 5
-
练习:使用 Unity
- Step 1:建立新项目
- Step 2:设定 Unity 容器
- Step 3:建立 Controller
-
练习:使用 Unity
-
DI 与 ASP.NET Web Forms
- 问题描述
- 解法
-
练习:使用 Unity
- Step 1:建立新项目
- Step 2:注册类型
- Step 3:编写 HTTP Handler
- Step 4:注册 HTTP Handler
- Step 5:编写测试页面
- 练习:使用 Unity 的 BuildUp 方法
-
练习:使用 Autofac
- Step 1:建立新项目
- Step 2:注册类型
- Step 3:编写 HTTP Handler
- Step 4:注册 HTTP Handler
- Step 5:编写测试页面
-
DI 与 WCF
- 问题描述
- 解法
-
练习:使用 Unity
- Step 1:建立 WCF 服务
- Step 2:编写自定义的 ServiceHostFactory
- Step 3:编写自定义的 ServiceHost
- Step 4:实现 IContractBehavior 接口
- Step 5:实现 IInstanceProvider 接口
- Step 6:设定 Unity 容器
- Step 7:修改 Web.config
- Step 8:编写客户端程序
-
练习:使用 Autofac.Wcf 套件
- Step 1:建立 WCF 服务
- Step 2:编写自定义的 ServiceHostFactory
- Step 3:设定 Autofac 容器
- Step 4:修改 Web.config
- Step 5:编写客户端程序
- 本章回顾
-
第 4 章:DI 与 ASP.NET MVC 分层架构
-
Part III:工具篇
-
第 7 章:Unity 学习手册
-
Unity 快速入门
- Hello, Unity!
- 注册类型对应
- 注册现有对象
-
解析
-
解析一个对象:
Resolve
- 具名注册与解析
-
解析多个对象:
ResolveAll
-
解析一个对象:
- 注册与解析泛型
- 检查注册
-
使用配置文件来设定容器
- Unity 配置文件基本格式
- 加载配置文件设定
-
注册与解析-进阶篇
-
共享的范例程序
- 情境
- 设计
- 代码
-
自动注册
- 解决重复类型对应的问题
-
AllClasses
类型 -
WithMappings
类型
-
自动匹配
- 自动匹配规则
-
手动匹配
- 循环参考问题
- 注入参数
- 注入属性
-
延迟解析
-
使用
Lazy<T>
- 使用自动工厂
-
使用
- 注入自定义工厂
-
共享的范例程序
-
对象生命周期管理
- 默认的生命周期
-
指定生命周期
- Transient vs. Per-Resolve
- Per-Request 生命周期
- 阶层式容器
- 选择生命周期管理员
-
拦截
-
使用 Unity 容器实现拦截
- Step 1:加入 Unity 的拦截扩展包
- Step 2:实现拦截行为
- Step 3:注册拦截行为
-
使用 Unity 容器实现拦截
- 结语
-
Unity 快速入门
-
附录一:DI 容器实务建议
-
容器设定
- 避免对同一个组件(DLL)重复扫描两次或更多次
- 使用不同类型来注册不同用途的组件
- 使用非静态类型来建立与设定 DI 容器
- 不要另外建立一个 DLL 项目来集中处理依赖关系的解析
- 为个别组件加入一个初始化类型来设定依赖关系
- 扫描组件时,尽量避免指定组件名称
-
生命周期管理
- 优先使用 DI 容器来管理对象的生命周期
- 考虑使用子容器来管理 Per-Request 类型的对象
- 在适当时机调用容器的 Dispose 方法
-
组件设计相关建议
- 避免建立深层的巢状对象
- 考虑使用泛型来封装抽象概念
- 考虑使用 Adapter 或 Façade 来封装 3rd-party 组件
- 不要一律为每个组件定义一个接口
- 对于同一层(layer)的组件,可依赖其具象类型
-
动态解析
- 尽量避免把 DI 容器直接当成 Service Locator 来使用
-
考虑使用对象工厂或
Func<T>
来处理晚期绑定
-
容器设定
-
附录二:初探 ASP.NET 5 的自带 DI 容器
-
练习步骤
- 步骤 1:建立项目
- 步骤 2:加入必要组件
- 步骤 3:将 Web API 组件加入 ASP.NET 管线
- 步骤 4:加入 API Controller
- 步骤 5:编写测试用的服务类型
- 步骤 6:注入相依对象至 Controller 的构造函数
- 结语
-
练习步骤
-
第 7 章:Unity 学习手册
- 備註
關於出版社

This book is published on Leanpub by Ministep Books
Authors have earned$9,913,563writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
Leanpub 無條件、無風險的 100% 滿意保證
從付款之後的 45 天內,只要點個兩下,你就可以完成退書並且取回先前支付的全數金額。退書是採用人工作業,因此可能需要花幾天的時間才能完成整個程序。
See full terms.
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), EPUB (for phones and tablets) and MOBI (for 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
Top Books
C++ Best Practices
Jason TurnerLevel up your C++, get the tools working for you, eliminate common problems, and move on to more exciting things!
Continuous Delivery Pipelines
Dave FarleyThis practical handbook provides a step-by-step guide for you to get the best continuous delivery pipeline for your software.
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
C++20
Rainer GrimmC++20 is the next big C++ standard after C++11. As C++11 did it, C++20 changes the way we program modern C++. This change is, in particular, due to the big four of C++20: ranges, coroutines, concepts, and modules.
The book is almost daily updated. These incremental updates ease my interaction with the proofreaders.
Atomic Kotlin
Bruce Eckel and Svetlana IsakovaFor both beginning and experienced programmers! From the author of the multi-award-winning Thinking in C++ and Thinking in Java together with a member of the Kotlin language team comes a book that breaks the concepts into small, easy-to-digest "atoms," along with exercises supported by hints and solutions directly inside IntelliJ IDEA!
Introductory Statistics with Randomization and Simulation
Mine Cetinkaya-Rundel, Christopher Barr, OpenIntro, and David DiezA complete foundation for Statistics, also serving as a foundation for Data Science, that introduces inference using randomization and simulation while covering traditional methods.
Leanpub revenue supports OpenIntro, so we can provide free desk copies to teachers interested in using our books in the classroom.
More resources: openintro.org.
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
Java OOP Done Right
Alan MellorObject Oriented Programming is still a great way to create clean, maintainable code. But only if you use it right.
This book gives you 25 years of OO best practice, ready to use.
You'll learn to design objects behaviour-first, use TDD to help, then confidently apply Design Patterns, SOLID principles and Refactoring to make clean, crafted code.
Introducing EventStorming
Alberto BrandoliniThe deepest tutorial and explanation about EventStorming, straight from the inventor.
Discrete Mathematics for Computer Science
Alexander Shen, Alexander S. Kulikov, Vladimir Podolskii, and Aleksandr GolovnevThis book supplements the DM for CS Specialization at Coursera and contains many interactive puzzles, autograded quizzes, and code snippets. They are intended to help you to discover important ideas in discrete mathematics on your own. By purchasing the book, you will get all updates of the book free of charge when they are released.
Top Bundles
- #1
Software Architecture for Developers: Volumes 1 & 2 - Technical leadership and communication
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #2
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #3
Cisco CCNA 200-301 Complet
4 Books
Ce lot comprend les quatre volumes du guide préparation à l'examen de certification Cisco CCNA 200-301. - #4
Modern C++ by Nicolai Josuttis
2 Books
- #5
CCDE Practical Studies (All labs)
3 Books
CCDE lab - #6
"The C++ Standard Library" and "Concurrency with Modern C++"
2 Books
Get my books "The C++ Standard Library" and "Concurrency with Modern C++" in a bundle. The first book gives you the details you should know about the C++ standard library; the second one dives deeper into concurrency with modern C++. In sum, you get more than 600 pages full of modern C++ and about 250 source files presenting the standard library... - #7
Mastering Containers
2 Books
Docker and Kubernetes are taking the world by storm! These books will get you up-to-speed fast! Docker Deep Dive is over 400 pages long, and covers all objectives on the Docker Certified Associate exam.The Kubernetes Book includes everything you need to get up and running with Kubernetes! - #8
The Future of Digital Health
6 Books
We put together the most popular books from The Medical Futurist to provide a clear picture about the major trends shaping the future of medicine and healthcare. Digital health technologies, artificial intelligence, the future of 20 medical specialties, big pharma, data privacy and how technology giants such as Amazon or Google want to conquer... - #9
Django for Beginners/APIs/Professionals
3 Books
- #10
Linux Administration Complet
4 Books
Ce lot comprend les quatre volumes du Guide Linux Administration :Linux Administration, Volume 1, Administration fondamentale : Guide pratique de préparation aux examens de certification LPIC 1, Linux Essentials, RHCSA et LFCS. Administration fondamentale. Introduction à Linux. Le Shell. Traitement du texte. Arborescence de fichiers. Sécurité...