tikhop
tikhop

Reputation: 2087

DDD: caching aggregate objects that received from sever

Suppose I have a repository (ContactsRepository) with method like getAllContacts(userId). The (ContactsRepository) fetch all data from server. After first call getAllContacts(userId) method I want to cache this data. My question is where I should realize caching mechanism, in the same (ContactsRepository) and when I invoke getAllContacts(userId) method again the repository back my caching data or I need put the data into another place (maybe Repository).

Upvotes: 6

Views: 2363

Answers (2)

Sebastian Good
Sebastian Good

Reputation: 6360

You can cache it, but don't forget cache coherency if you execute domain logic with more than one repository, e.g. on multiple machines.

Upvotes: 1

coder_bro
coder_bro

Reputation: 10773

You can use Repository to obtain the data either from the cache or from the database. if your Repository classes have update methods you can effectively invalidate the cache items as well.

You can thus encapsulate the access to cache within the Repository: http://martinfowler.com/eaaCatalog/repository.html

Another example to implement caching for Repository: http://ardalis.com/building-a-cachedrepository-via-strategy-pattern

Upvotes: 4

Related Questions