2

All examples on the internet have the basic 4 crud methods.

I am really confused how a big system will implement the repository pattern. Let's say I have a user which belongs to groups and has written books.

Sometimes I only need the user data, some times with his books data and sometimes with groups data. Do I implement a method for each of the usecases ?

And how to abstract updates? If I am using MongoDB then I have $inc, $pull and other awesome operators.

How do I actually abstract these types of updates if I need to combine them, for example $set with $inc. Do I need to have a separate method for each usecase of the system ?

2 Answers 2

2

Sometimes I only need the user data, some times with his books data and sometimes with groups data. Do I implement a method for each of the usecases ?

It's certainly possible.

One of the interesting approaches to consider is CQRS, which creates an explicit separation between "designs we use to change information" and "designs we use to report information".

For changes, we certainly want to load information into representations that can be used to ensure that the invariant of our domain dynamics is maintained.

For reports, though, that's not nearly as important. In fact, it is often the case that (for reporting) we need only a slice of the information used in our domain logic, not the entire graph of information. In which case, it makes sense that we would have a different protocol for converting a query protocol into an interesting and useful data structure.

Furthermore, your reads and your writes might not need to be using common information at all - it might be better to generate your reports from cached copies of information (trading away some data freshness in exchange for lower latency, for example).

Part of the point of REPOSITORY is that it acts as an encapsulation boundary, decoupling the plumbing of information management from the components in your solution that just don't care.


And how to abstract updates? If I am using MongoDB then I have $inc, $pull and other awesome operators.

Choose the appropriate layer of abstraction.

If mongo, or mongo like, capabilities are a necessary constraint on your choice of persistent storage, then being able to detect this from the repository interface is a good thing.

If those capabilities only show up in a few code paths, then it is likely that you will have multiple methods available - a set of methods for those paths that don't need the extra power that you get from mongo, and another set of methods for those paths that do.

Sign up to request clarification or add additional context in comments.

Comments

0

In my case, I create one repository for one aggregate, and one store method. In the store method, I update the differences between the persisted data and the modified object. When there is no persisted data, I do Insert. The repository of the aggregate will also manage CRUD for child elements that have a parent-child relationship with the aggregate. Detailed implementation may separate the classes.

1 Comment

Could you please share an example of what you have done? I mean managing CRUD for child elements that have a parent-child relationship with the aggregate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.