I’ve been looking into implementing caching at Repository level using the .NET 4.0 Caching features or via a third party abstraction interface like this one and I’ve just run into an issue where I will have to support two use cases.
My application might be split in multiple areas, for instance the Admin area and the User frontend (website frontend). I don’t want any caching to take place in the Admin area, Admins should always have the latest refreshed data displayed to them.
now, do I have to refactor all my repository methods to take in a parameter whether to return the cached results or not? Are there any other options?
You could do the caching at the controller level, not at the repositories. This way in the admin area controllers you will always query the repository to get fresh data and in the others you will cache the results. If you absolutely need to do the caching of the objects at the repository layer you need an additional parameter to indicate whether the call is being made from the Admin area or not. Or you could also use a DI framework such as NInject which allows you to pass parameters from the HttpContext to dependencies that are allocated per request lifetime. So you could inject the
areaRouteData parameter to your repository’s constructor.