R – Where do all “bulk” operations belong in DDD

abstractionddd-repositoriesdomain-driven-design

In DDD one of the key concepts is Repository, which allows you to retrieve Entities (or Aggregate Roots) and then save them back after they are updated.

Let assume that we need to perform some 'bulk' operation with entities, and the number of entities makes it absolutely impossible to retrieve them into memory. I.e. operation can only be performed in the database.

Where is the place for such 'bulk' operation? Should it be a method on repository? Won't it "leak" Repository abstraction with database specific operation? Won't it move business operation from Entity to Repository?

Best Solution

I think it should be a service.

Evans recommends in his book, that when you are in doubt whether to put a method that "smells bad" inside a class because you think that it doesn't belong there, make a ServiceFoo class with the operation inside.

Related Question