开发者

Entity Framework Caching with the Repository Pattern

开发者 https://www.devze.com 2023-02-24 00:47 出处:网络
If I want to implement caching when I am using the repository pattern and the Entity Framework, couldn\'t I just do some simple lo开发者_运维知识库gic outside of the Entity Framework to handle the cac

If I want to implement caching when I am using the repository pattern and the Entity Framework, couldn't I just do some simple lo开发者_运维知识库gic 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号