jeudi 1 décembre 2016

Proxy design pattern with IoC

I am trying to implement proxy design pattern for caching services as below.

public interface IProductService
{
   Task<int> ProcessOrder(int orderId);
}

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

public class CachedProductService : IProductService
{
   private IProductService _realService;

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

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

How do I to use IoC container (Unity/Autofac) to create real service and cached service objects as I can register IProductService to ProductService or CachedProductService but CachedProductService in turn requires a IProductService object (ProductService) during creation.

I am trying to arrive at something like this:

The application will target IProductService and request IoC container for an instance and depending on the configuration of the application (if cache is enabled/disabled), the application will be provided with ProductService or CachedProductService instance.

Any ideas? Thanks.

Aucun commentaire:

Enregistrer un commentaire