Alex
Alex

Reputation: 887

Linking together Repository pattern, caching and web-service

I'm try to understand Repository pattern to implement it in my app. And I'm stuck with it in a some way.

Here is a simplified algorithm of how the app is accessing to a data:

  1. At first time the app has no data. It needs to connect to a web-service to get this data. So all the low-level logic of interaction with the web-service will be hiding behind the WebServiceRepository class. All the data passed from the web-service to the app will be cached.

  2. Next time when the app will request the data this data will be searched in the cache before requesting them from the web-service. Cache represents itself as a database and XML files and will be accessed through the CacheRepository.

    The cached data can be in three states: valid (can be shown to user), invalid (old data that can't be shown) and partly-valid (can be shown but must be updated as soon as possible).

    a) If the cached data is valid then after we get them we can stop.

    b) If the chached data is invalid or partly-valid we need to access WebServiceRepository. If the access to the web-service is ended with a success then requested data will be cached and then will be showed to user (I think this must be implemented as a second call to the CacheRepository).

    c) So the entry point of the data access is the CacheRepository. Web-service will be called only if there is no fully valid cache.

I can't figure out where to place the logic of verifying the cache (valid/invalid/partly-valid)? Where to place the call of the WebServiceRepository? I think that this logic can't be placed in no one of Repositories, because of violation the Single Responsibility Principle (SRP) from SOLID.

Should I implement some sort of RepositoryService and put all the logic in it? Or maybe is there a way to link WebServiceRepository and WebServiceRepository?

What are patterns and approaches to implement that?

Another question is how to get partly-valid data from cache and then request the web-service in the one method's call? I think to use delegates and events. Is there other approaches?

Please, give an advice. Which is the correct way to link all the functionality listed above?

P.S. Maybe I described all a bit confusing. I can give some additional clarifications if needed.

P.P.S. Under CacheRepository (and under WebServiceRepository) I meant a set of repositories - CustomerCacheRepository, ProductCacheRepository and so on. Thanks @hacktick for the comment.

Upvotes: 3

Views: 3339

Answers (1)

Stephan Schinkel
Stephan Schinkel

Reputation: 5530

if your webservice gives you crud methods for different entities create a repository for every entityroot. if there are customers create a CustomerRepository. if there are documents with attachments as childs create a DocumentRepository that returns documents with attachments as a property.

a repository is only responsible for a specific type of entity (ie. customers or documents). repositories are not used for "cross cutting concerns" such as caching. (ie. your example of an CacheRepository)

inject (ie. StuctureMap) a IDataCache instance for every repository.

a call to Repository.GetAll() returns all entities for the current repository. every entity is registered in the cache. note the id of that object in the cache.

a call to Repository.FindById() checks the cache first for the id. if the object is valid return it.

notifications about invalidation of an object is routed to the cache. you could implement client-side invalidation or push messages from the server to the client for example via messagequeues.

information about the status whether an object is currently valid or not should not be stored in the entity object itself but rather only in the cache.

Upvotes: 5

Related Questions