Reputation: 744
If I want to implement caching when I am using the repository pattern and the Entity Framework, couldn't I just do some simple logic outside of the Entity Framework to handle the caching?
E.g.
if(Cache[ProductsKey] != null)
{
return ConvertToProducts(Cache[ProductsKey]);
}
else
{
var products = repository.Products;
Cache[ProductsKey] = products;
return products;
}
It seems like a lot of people are over-complicating this. Or is doing it this way going to be limiting in some way?
Upvotes: 6
Views: 2825
Reputation: 14781
It is better to cash the entire ObjectContext
which is here the (Repository).
Use the Session_Start
and Session_End
to initialize and dispose the object respectively.
Upvotes: 4
Reputation: 8198
I prefer my repository to be clean. I prefer implementing caching in my service layer if needed.
So i 100% agree with your sample. Your repository returns products (by running query) and you can cache it or not in other layers.
P.S.: I assume that you start your object context when it's needed(session start) and dispose it when session ends.
Upvotes: 4