Friday, June 14, 2013

Design Emerges - A Better Cached Repository


I really enjoy looking at code and seeing it evolve over time.  You learn a lot about the limitations of design decisions, and get to see the impact of their new changes.

In my previous post, I use a very simple wrapper to implement caching.  There are limitations with this model that you'll quickly run into as the complexity grows.

In this post, I will evolve the cached repository model a step forward - implementing better OO design.

So here is the interface and the normal/database repository:


And here is the new cached repository:


In this model, the cached repository is an implementation of the repository.  This layered approach is nice because it separates the concerns of caching out of the normal repo, making the code much cleaner and easier to understand.  The layers above it have no idea or care about caching.

You can also easily turn on/off caching by configuring your DI framework.  In the below example, I use Autofac: