ASP.NET设计模式(一)、适配器模式、依赖注入依赖倒置、空对象模式
鸟随凤鸾,人伴贤良,得以共之,我之幸也。说的是鸟随着鸾凤可以飞的更高远,人和比自己境界高的相处,自己也会得到熏染进步。
一、概述
分享出来简单的心得,望探讨
依赖倒置
依赖注入
Adapter模式
Null模式
二、快速示例
从一个简单的示例开始,业务流程Client通过Service查询产品,基础大概框架如下

关键代码:
class ProductService
{ private ProductRepository _productRepository; public ProductService()
{
_productRepository=new ProductRepository();
} public IList<Product> GerAllProductsIn(int category)
{
IList<Product> products;
string storagekey = string.Format("product_category_id_{0}",category);
products =(List<Product>) HttpContext.Current.Cache.Get(storagekey); if (products==null)
{
products=_productRepository.GetAllProductsIn(category);
HttpContext.Current.Cache.Insert(storagekey,products);
}
return products;
}
}
ProductService
ProductService直接依赖于ProductRepostory属于强耦合。如何理解这种强耦合呢?现在假设客户新产品上架,需要将ProductRepository替换为NewProductRepository,那么就势必要修改ProductService以及所有引用类ProductRepository,也就是高层模块依赖于低层模块。
三、重构
1、依赖倒置
那么究竟何为依赖倒置,它又如何解耦呢。
依赖倒置实际就是就是要依赖于抽象,不要依赖于具体。意思就是面对抽象进行,而不是面对实现进行编程,通过抽象解决客户与实现模块间的耦合关系。依赖倒置两大重要的原则:一是高层模块不应依赖于低层模块,都应依赖于抽象,二是具体实现也应依赖于抽象。
来看怎么做,定义IProductRepostory接口,使ProductRepostory依赖于接口 ,对ProductService重构也依赖于接口
class ProductService
{ private IProductRepository _productRepository; public ProductService()
{
_productRepository=new ProductRepository();
} public IList<Product> GerAllProductsIn(int category)
{
IList<Product> products;
string storagekey = string.Format("product_category_id_{0}",category);
products =(List<Product>) HttpContext.Current.Cache.Get(storagekey); if (products==null)
{
products=_productRepository.GetAllProductsIn(category);
HttpContext.Current.Cache.Insert(storagekey,products);
}
return products;
}
}
ProductService
依赖倒置通过面对抽象从数据类型降低了耦合度,依赖倒置使高层模块依赖于抽象,即只关心new出来的是否是IProductRepository接口的对象而不关心new出来是谁。这样现在需要解决的就是具体实现的创建问题。
2、依赖注入
依赖注入从其初衷的角度解释就是:当类A完成某个工作需要创建B类的实例来协助的时候,把创建协助类B的工作提取出来从外部完成。
依赖注入三种形式:构造器,方法,属性。本文用构造器注入的方式介绍依赖注入
ProductService重构:
public class ProductService
{
private IProductRepository _productRepository; public ProductService(IProductRepository productRepository)
{
_productRepository = productRepository; } .................. }
ProductService
测试代码(即外部):
namespace Patterns.QuickDemp.Tests
{
[TestFixture]
public class ProductServiceTests
{
[Test]
public void Test_Client()
{
//外部创建对象,任何继承抽象的对象皆可注入
AnyProductRepository anyproductrepository = new AnyProductRepository();
//注入
ProductService productService = new ProductService(anyproductrepository);
...........
.......... } }
}
Tests
可以大胆的作如下设想:客户,商家,第三方支付的关系拿来类比,客户直接与商家交易是一种模式。现在依赖反转,银行卡,支付宝,现金券等支付方式全部继承某个接口,支付动作抽象出来,客户选择支付方式然后外部注入,第三方根据注入指定方式完成支付动作。
3、Adapter模式
adapter模式意图描述:将一个类的接口转换成客户希望的接口
问题描述:用户自定义接口ICacheStorage.面向抽象过程中要让HttpContext成为一个实现用户自定义接口的实现。HttpContext是非用户编写的类,所以用到适配器模式

public interface ICacheStorage
{
void Remove(string key);
void Store(string key, object data);
T Retrieve<T>(string storageKey);
}
ICacheStorage
public class HttpContextCacheAdapter : ICacheStorage
{
public void Remove(string key)
{
HttpContext.Current.Cache.Remove(key);
} public void Store(string key, object data)
{
HttpContext.Current.Cache.Insert(key, data);
} public T Retrieve<T>(string key)
{
T itemStored = (T)HttpContext.Current.Cache.Get(key);
if (itemStored == null)
itemStored = default(T); return itemStored;
}
}
HttpContextCacheAdapter
仍然依赖倒置依赖注入对Service重构如下:
public class ProductService
{
private IProductRepository _productRepository;
private ICacheStorage _cacheStorage; public ProductService(IProductRepository productRepository, ICacheStorage cacheStorage)
{
_productRepository = productRepository;
_cacheStorage = cacheStorage;
} public IList<Product> GetAllProductsIn(int categoryId)
{
IList<Product> products;
string storageKey = string.Format("products_in_category_id_{0}", categoryId); products = _cacheStorage.Retrieve<List<Product>>(storageKey); if (products == null)
{
products = _productRepository.GetAllProductsIn(categoryId);
_cacheStorage.Store(storageKey, products);
} return products;
}
}
ProductService
4、Null Object 模式
空对象模式:在一些特殊的情况下用户不希望连带的执行某个动作,也不希望传递null在框架里做判断,就用到空对象模式。这个很容易理解,只是让附带动作变为do nothing
public class NullObjectCachingAdapter : ICacheStorage
{
public void Remove(string key)
{
// Do nothing
} public void Store(string key, object data)
{
// Do nothing
} public T Retrieve<T>(string storageKey)
{
return default(T);
}
}
NullObjectCachingAdapter
四、小结
通过依赖注入和依赖倒置,把创建什么样对象的工作交给客户端(Client),降低Service、Repository、Cache之间的耦合度。假如有新的产品,只有实现了IRepositiry接口,在客户端创建对象,注入给service的构造函数就可以了。

ASP.NET设计模式(一)、适配器模式、依赖注入依赖倒置、空对象模式的更多相关文章
- Spring.NET依赖注入框架学习-- 泛型对象的创建和使用
Spring.NET依赖注入框架学习-- 泛型对象的创建和使用 泛型对象的创建方法和普通对象是一样的. 通过构造器创建泛型对象 下面是一个泛型类的代码: namespace GenericsPlay ...
- Java进阶篇设计模式之十三 ---- 观察者模式和空对象模式
前言 在上一篇中我们学习了行为型模式的备忘录模式(Memento Pattern)和状态模式(Memento Pattern).本篇则来学习下行为型模式的最后两个模式,观察者模式(Observer P ...
- Java设计模式之十三 ---- 观察者模式和空对象模式
前言 在上一篇中我们学习了行为型模式的备忘录模式(Memento Pattern)和状态模式(Memento Pattern).本篇则来学习下行为型模式的最后两个模式,观察者模式(Observer P ...
- 设计模式:空对象模式(Null Object Pattern)
设计模式:空对象模式(Null Object Pattern) 背景 群里聊到<ASP.NET设计模式>,这本书里有一个“Null Object Pattern”,大家就闲聊了一下这个模式 ...
- C# 设计模式之空对象模式
最近看了不少的书籍和视频等相关资料,决定自己边学习边写一下个人对设计模式的理解,如果有不对的请大家多多指正. 今天先说说我个人觉得最简单的设计模式 -- [空对象模式] 空对象模式可以减少客户端对对象 ...
- 被遗忘的设计模式——空对象模式(Null Object Pattern)
GoF(四人帮)那本<设计模式 可复用面向对象软件的基础>可谓是设计模式方面的经典之作,其中介绍的23种设计模式, 也可谓是经典中的经典.但是,设计模式的种类绝不仅仅是这23种,除此之外还 ...
- GoLang设计模式12 - 空对象模式
空对象设计模式是一种行为型设计模式,主要用于应对空对象的检查.使用这种设计模式可以避免对空对象进行检查.也就是说,在这种模式下,使用空对象不会造成异常. 空对象模式的组件包括: Entity:接口,定 ...
- ASP.NET Core依赖注入——依赖注入最佳实践
在这篇文章中,我们将深入研究.NET Core和ASP.NET Core MVC中的依赖注入,将介绍几乎所有可能的选项,依赖注入是ASP.Net Core的核心,我将分享在ASP.Net Core应用 ...
- Spring IOC - 控制反转(依赖注入) - 入门案例 - 获取对象的方式 - 别名标签
1. IOC - 控制反转(依赖注入) 所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管理过程交 由Spring框架来处理,从此在开发过程中不再需要关注对象的创建和生命周 ...
随机推荐
- ASP.NET--ListBox初始化时设置多个选中项
public void SetSelectedListItem(ListBox lst, List<DBServerIPBind> source) { ; i < source.Co ...
- 使用正则表达式匹配JS函数代码
使用正则表达式匹配JS函数代码 String someFunction="init"; Pattern regex = Pattern.compile("function ...
- SSDT – Error SQL70001 This statement is not recognized in this context-摘自网络
March 28, 2013 — arcanecode One of the most common errors I get asked about when using SQL Server Da ...
- 展开/收缩 ul
了一个展开收缩的东东,留着以后万一用到 后台递归生成的函数(这里是一个反射参数展示,支持多层级展开显示,后台反射如何多层级解析的方法有时间再补上吧) /// <summary> /// 递 ...
- [SCOI2005]互不侵犯King
题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. ——by洛谷 https://www. ...
- background-position也许你没考虑到
设置背景图片时不知你有没有遇到过背景位置的困扰,有没有深入思考过,background-position到底是什么,下面请各位看看我的理解. 简而言之就一句话,默认图片左上角居元素左上角的坐标,例如: ...
- 错误: 找不到或无法加载主类 / Class not found
Java Resources文件上有红色感叹号存在 说明引入jar包存在错误,把引用错误的jar包去掉即可. 右键项目,Properties,Java Build Path,选中jar包remove, ...
- java 5 Lock
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public clas ...
- 标准I/O之实现细节
在UNIX系统中,标准I/O库最终都要调用文件I/O(read.write等).每个标准I/O流都有一个与其相关联的文件描述符,可以对一个流调用fileno函数以获得其描述符. 注意,fileno不是 ...
- Android读取Manifest文件下Application等节点下的metadata自定义数据
介绍 Android在 提供了meta-date使用键值对的形式实现自定义配置.一般使用中作为渠道标识.可以用 在<application>,<activity>,<ac ...