鸟随凤鸾,人伴贤良,得以共之,我之幸也。说的是鸟随着鸾凤可以飞的更高远,人和比自己境界高的相处,自己也会得到熏染进步。

分享出来简单的心得,望探讨

依赖倒置

依赖注入

Adapter模式

Null模式

二、快速示例

从一个简单的示例开始,业务流程Client通过Service查询产品,基础大概框架如下

 设计模式备忘录(1):适配器模式、依赖注入依赖倒置、空对象模式

关键代码:

 1 class ProductService
 2     {
 3 
 4         private ProductRepository _productRepository;
 5 
 6         public ProductService()
 7         {
 8             _productRepository=new ProductRepository();
 9         }
10 
11         public IList<Product> GerAllProductsIn(int category)
12         {
13             IList<Product> products;
14             string storagekey = string.Format("product_category_id_{0}",category);
15             products =(List<Product>) HttpContext.Current.Cache.Get(storagekey);
16 
17             if (products==null)
18             {
19                 products=_productRepository.GetAllProductsIn(category);
20                 HttpContext.Current.Cache.Insert(storagekey,products);
21             }
22             return products;
23         } 
24     }
ProductService

相关文章:

  • 2021-11-29
  • 2022-12-23
  • 2021-07-18
  • 2022-01-11
  • 2021-11-24
猜你喜欢
  • 2021-12-06
  • 2021-08-20
  • 2022-12-23
  • 2021-11-14
相关资源
相似解决方案