Repository Pattern - Caching

不打扰是莪最后的温柔 提交于 2019-12-03 18:46:22

问题


I'm not sure where I should implement the caching in my repository pattern.

Should I implement it in the service-logic or in the repository?

GUI -> BusinessLogic (Services) -> DataAccess (Repositories)


回答1:


I would handle it in the repository/data access layer. The reasoning is because it isn't up to the business layer on where to get the data from, that is the job of the repository. The repository will then decide where to get the data from, the cache (if it's not too old) or from the live data source based on the circumstances of the data access logic.

It's a data access concern more than a business logic issue.




回答2:


It's a good idea not to put the caching logic directly into your repository, as that violates the Single Responsibility Principle (SRP) and Separation of Concerns. SRP essentially states that your classes should only have one reason to change. If you conflate the concerns of data access and caching policy in the same class, then if either of these needs to change you'll need to touch the class. You'll also probably find that you're violating the DRY principle, since it's easy to have caching logic spread out among many different repository methods, and if any of it needs to change, you end up having to change many methods.

The better approach is to use the Proxy or Strategy pattern to apply the caching logic in a separate type, for instance a CachedRepository, which then uses the actual db-centric repository as needed when the cache is empty. I've written two articles which demonstrate how to implement this using .NET/C#, which you will find on my blog, here:

  • http://ardalis.com/introducing-the-cachedrepository-pattern
  • http://ardalis.com/building-a-cachedrepository-via-strategy-pattern

If you prefer video, I also describe the pattern in the Proxy Design Pattern on Pluralsight, here:

  • http://pluralsight.com/training/courses/TableOfContents?courseName=patterns-library


来源:https://stackoverflow.com/questions/3442102/repository-pattern-caching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!