title: Implementing caching of services in Domain project being used by a Web API
body: <p><strong>My question is</strong>: how do I implement caching in my domain project, which is working like a normal stack with the repository pattern.</p>
  
  <p>I have a setup that looks like the following:</p>
  
  <ul>
  <li>ASP.NET MVC website</li>
  <li>Web API </li>
  <li>Domain project (using IoC, with Windsor)</li>
  </ul>
  
  <p>My domain project for instance have:</p>
  
  <ul>
  <li>IOrderRepository.cs</li>
  <li>OrderRepository.cs</li>
  <li>Order.cs</li>
  </ul>
  
  <p>My ASP.NET MVC website calls the Web API and gets back some DTO classes. My Web API then maps these objects to business objects in my domain project, and makes the application work.</p>
  
  <p>Nowhere in my application have I implemented caching.</p>
  
  <p><strong>Where should be caching be implemented?</strong> </p>
  
  <p>I thought about doing it inside the methods in the OrderRepository, so my <code>Get</code>, <code>GetBySpecification</code> and <code>Update</code> methods has to call some generic cache handler injected by the OrderRepository. </p>
  
  <p>This obviously gives some very ugly code, and isn't very generic.</p>
  
  <p><strong>How to maintain the cache?</strong></p>
  
  <p>Let's say we have a cache key like "OrderRepostory_123". When I call the Update method, should I call cacheHandler.Delete("OrderRepository_123") ? Because that seems very ugly as well</p>
  
  <p><strong>My own thoughts...</strong></p>
  
  <p>I can't really see a decent way to do it besides some of the messy methods I have described. Maybe I could make some cache layer, but I guess that would mean my WebAPI wouldn't call my OrderRepository anymore, but my CacheOrderRepository-something?</p>