2016-12-01 1 views
0

아래와 같이 캐싱 서비스를위한 프록시 디자인 패턴을 구현하려고합니다. 차례가를 필요에 내가 을 등록 할 수 ProductService 또는 CachedProductService하지만 CachedProductService에 IProductService을 실제 서비스와 캐시 서비스 개체를 만들 IoC 컨테이너 (유니티/Autofac)를 사용하려면 어떻게해야IoC를 사용한 프록시 디자인 패턴

public interface IProductService 
{ 
    int ProcessOrder(int orderId); 
} 

public class ProductService : IProductService 
{ 
    public int ProcessOrder(int orderId) 
    { 
     // implementation 
    } 
} 

public class CachedProductService : IProductService 
{ 
    private IProductService _realService; 

    public CachedProductService(IProductService realService) 
    { 
     _realService = realService; 
    } 

    public int ProcessOrder(int orderId) 
    { 
     if (exists-in-cache) 
     return from cache 
     else 
     return _realService.ProcessOrder(orderId); 
    } 
} 

IProductService 개체 (ProductService)를 만듭니다. 나는 이런 식으로 뭔가에 도달하기 위해 노력하고

:

응용 프로그램은 타겟으로 IProductService 및 인스턴스에 대한 IoC 컨테이너를 요청하고 (캐시가 활성화/비활성화되어있는 경우) 응용 프로그램의 구성에 따라 응용 프로그램은 ProductService 또는 CachedProductService 인스턴스와 함께 제공됩니다.

아이디어가 있으십니까? 감사. 그래프가 다음과 같을 것이다 컨테이너없이

답변

0

:

container.Register<IProductService, ProductService>(); 

// Add caching conditionally based on a config switch 
if (ConfigurationManager.AppSettings["usecaching"] == "true") 
    container.RegisterDecorator<IProductService, CachedProductService>(); 
: 여기

new CachedProductService(
    new ProductService()); 

단순 인젝터를 사용하여 예제