《.NET应用架构设计:原则、模式与实践》新书博客--试读-2.1.2 设计原则实战
2.1.2 设计原则实战
using System;
usingSystem.Collections.Generic;
usingSystem.Web;
using AgileSharp.Chapter2.Repository;
using AgileSharp.Chapter2.Domain;
namespace AgileSharp.Chapter2.Service
{
public
class ProductService
{
private ProductRepository productRepository =
null;
public ProductService()
{
productRepository =
new ProductRepository();
}
public List<Product> GetAllProductsFrom(intcategoryId)
{
List<Product> result =
new List<Product>();
try
{
string cacheKey =
string.Format(
"products_in_category_{0}",categoryId);
result = HttpContext.Current.Cache.Get(cacheKey)
as List<Product>;
if (result ==
null)
{
result = productRepository.GetProductsFrom(categoryId);
if (result !=
null &&result.Count> 0)
{
HttpContext.Current.Cache.Insert(cacheKey, result);
}
}
}
catch (Exception ex)
{
//Log will add here later
}
return result;
}
}
}
using System.Collections.Generic;
using AgileSharp.Chapter2.Domain;
namespace AgileSharp.Chapter2.Repository
{
public
interface IProductRepository
{
List<Product> GetProductsFrom(intcategoryId);
}
}
using System;
usingSystem.Collections.Generic;
using AgileSharp.Chapter2.Domain;
namespace AgileSharp.Chapter2.Repository
{
public
class ProductRepository:IProductRepository
{
//…
}
}
using System;
usingSystem.Collections.Generic;
usingSystem.Web;
using AgileSharp.Chapter2.Repository;
using AgileSharp.Chapter2.Domain;
namespace AgileSharp.Chapter2.Service
{
public
class ProductService
{
privateIProductRepositoryproductRepository =
null;
publicProductService()
{
productRepository =
new ProductRepository();
}
public List<Product> GetAllProductsFrom(intcategoryId)
{
//...
}
}
}

