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()); 

はシンプルインジェクタを使用した例です

関連する問題