2012-04-04 39 views
0

的高速缓存的实现我有一个服务接口,ICustomerService,并经两种实现:城堡温莎 - 注册和解析缓存和相同的接口

public class CustomerService : ICustomerService 
{ 
    // stuff 
} 

public class CachedCustomerService : ICustomerService 
{ 
    public CachedCustomerService(CustomerService service) { } 
} 

缓存服务的话,只是缓存和代表正常服务。

对于注册我已将ICustomerService解析为CachedCustomerService,然后CustomerService仅针对其自己的类型注册。

这工作正常。

我想知道的是,如果我可以使CachedCustomerService需要一个接口,而不是具体的CustomerService。原因是我们可能最终得到两种类型的CustomerService,并且我想避免(如果可能)每个特定的缓存版本。

所以CachedCustomerService的构造函数将变为:

public class CachedCustomerService : ICustomerService 
{ 
    // notice the ICustomerService 
    public CachedCustomerService(ICustomerService service) { } 
} 

我有过登记总量控制,但分辨率由asp.net的MVC的碗完成。

谢谢!

回答

3

所有你有温莎城堡做的是像this article建议。 所以你的情况,你只需要以与您的缓存的业务先注册你的客户服务:

var container = new WindsorContainer() 
.Register(
Component.For(typeof(ICustomerService)).ImplementedBy(typeof(CachedCustomerService)), 
Component.For(typeof(ICustomerService)).ImplementedBy(typeof(CustomerService)) 
); 

然后缓存的客户服务可以使用ICustomerService作为内部/包装的对象如下:

public class CachedCustomerService : ICustomerService 
{ 
    private ICustomerService _customerService; 
    public CachedCustomerService(ICustomerService service) 
    { 
     Console.WriteLine("Cached Customer service created"); 
     this._customerService = service; 
    } 

    public void Run() 
    { 
     Console.WriteLine("Cached Customer service running"); 
     this._customerService.Run(); 
    } 
} 

然后分辨率是正常的:

ICustomerService service = container.Resolve<ICustomerService>(); 
service.Run();