Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which layer implement the cache?

I'm developing a Rest API using Service Stack's framework. All layers are separated so we can make DAL mocks for business logic layer unit testing.

I'm configuring the cache with inversion of control:

container.Register<ICacheClient>(new MemoryCacheClient());

Where MemoryCacheClient is a simple class that implements ICacheClient with a few methods.

And here is the question: What is the best layer in which to include the call to the cache through this inversion of control?

  • It could be in the BLL, but won't it bring problems to unit tests?

  • It could be in DAL, knowing that I would have to lose IOC? And, in this case, I will depend of webserver's cache, that could be wrong.

  • It could be in Web interface, knowing that I can have some logic here and even lose some features?

  • It could be between web interface and BLL, creating a new layer?

I've searched a lot and read some articles, but with no lucky:

  • Help with debate on Separation of concerns (Data Access vs Business Logic)

  • http://www.velocityreviews.com/forums/t639532-3-tier-design-and-cache-for-asp-net-3-5-a.html

  • http://forums.asp.net/t/1795015.aspx/1

Thank's

like image 378
Junior Suzuki Avatar asked Mar 11 '13 13:03

Junior Suzuki


1 Answers

Your caching doesn't need to be in either layer. You can keep it external from your business logic and data access logic by wrapping the calls that would leverage caching in a decorated method and configuring the use of the caching decorator in the IoC container.

I haven't done this using ServiceStack specifically, but the pattern is well documented:

  • Introducing the CachedRepository Pattern (explicit caching decorator)
  • Caching as a Cross-Cutting Concern using Castle Windsor (leverages dynamic proxies)
  • Loosely coupled .NET Cache Provider using Dependency Injection
  • Inversion of Control and Dependency Injection: Working with Windsor Container
like image 106
smartcaveman Avatar answered Nov 16 '22 03:11

smartcaveman