当前位置:编程学习 > asp >>

Asp.Net Design Pattern Studynotes -- Part1

Asp.Net Design Pattern Studynotes -- Part1
 

let's start with an exampleto entry amazing OO world !
 

let's saynow we need to implement a small feature which need :

an entityclass: Product

businesslogic : List<Product>GetProductBy(Function<Product,bool> where);

Service : List<Product> GetProductBy(Function<Product,bool>where);

 

1st Version
 

Entity:


[csharp]

public classProduct{} 

public classProduct{}

 

Logic:


[csharp]

public classProductRepositoryV1 
    { 
  
       public List<Product>GetProductBy(Func<Product,bool> where ) 
       { 
           var products = newList<Product>(); 
           returnproducts.Where(where).ToList(); 
       } 
    } 

public classProductRepositoryV1
    {
 
       public List<Product>GetProductBy(Func<Product,bool> where )
       {
           var products = newList<Product>();
           returnproducts.Where(where).ToList();
       }
    }

 

service:


[csharp]

classProductServiceV1 
    { 
        private ProductRepositoryV1 _productRespository; 
        private List<Product>_cacheProduct ; 
  
        public ProductServiceV1() 
        { 
            _productRespository = newProductRepositoryV1(); //1.instant  
            _cacheProduct = newList<Product>(); 
        } 
  
        public List<Product>GetProductListBy(Func<Product,bool> where) 
        { 
            var productInCache =_cacheProduct.Where(where);//3.in couple with BL  
            if (!productInCache.Any()) 
            { 
                var products =_productRespository.GetProductBy(where); 
               _cacheProduct.AddRange(products); //2.also care about how to cache  
                return products; 
            } 
            return productInCache.ToList(); 
        } 
  
    } 

classProductServiceV1
    {
        private ProductRepositoryV1 _productRespository;
        private List<Product>_cacheProduct ;
 
        public ProductServiceV1()
        {
            _productRespository = newProductRepositoryV1(); //1.instant
            _cacheProduct = newList<Product>();
        }
 
        public List<Product>GetProductListBy(Func<Product,bool> where)
        {
            var productInCache =_cacheProduct.Where(where);//3.in couple with BL
            if (!productInCache.Any())
            {
                var products =_productRespository.GetProductBy(where);
               _cacheProduct.AddRange(products); //2.also care about how to cache
                return products;
            }
            return productInCache.ToList();
        }
 
    }

 

 

But we cansee the deficiencies :

 

For 1stVersion (Original Version):

 

1.productServiceis in couple with ProductRepository .once productRespository changed method signature, service have to change code.

solution: depend onabstract but not on concrete implementation

 

2.code is untestable .which need data base ready , but if data base can not connect ,meaning can not be tested.

solution: decoupleservice from business class

 

3.do multiple things .

a. cache thedata ,b. provice service .c. instance the repository object .

 

solution : do only one thing .

 

 

ok, nowlet's fix it !

2nd version :
 

Entity: same with above .

 

business:

 

[csharp]

inte易做图ce IProductRespository//added inte易做图ce fordependency inversion  
    { 
        List<Product>GetProductBy(Func<Product, bool> where); 
    } 
 class ProductRespositoryV2:IProductRespository 
    { 
        public List<Product>GetProductBy(Func<Product, bool> where) 
        { 
            var products = newList<Product>(); 
            returnproducts.Where(where).ToList(); 
        } 
    } 

inte易做图ce IProductRespository//added inte易做图ce fordependency inversion
    {
        List<Product>GetProductBy(Func<Product, bool> where);
    }
 class ProductRespositoryV2:IProductRespository
    {
        public List<Product>GetProductBy(Func<Product, bool> where)
        {

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,