Where do all "bulk" operations belong in DDD?
Asked Answered
D

4

15

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?

Downhearted answered 18/2, 2009 at 17:45 Comment(1)
Are you asking about performing large updates on a set of records? (Say, calculating millions of prices with a single update statement.)Vietnam
C
9

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.

Copyboy answered 26/8, 2009 at 3:48 Comment(2)
It's been 9 years, I hope someone can help to answer this follow up question. When we implement the service, would it loop all the aggregate and save/add them back to repository afterwards?Norahnorbert
@Norahnorbert It sounds reasonable to loop them all, BUT each of Aggregate Root (AR) is a "Consistency Boundary", so one must be done to save the next AR, hence there should be no transaction to rollback if one of them was failed. If you want to rollback in that situation, you might have modeled the AR wrong. Maybe some are Entities/Value Objects, for example an Order AR can batch insert OrderItems entities, and it's OK to rollback if one OrderItem gone wrong.Dustcloth
S
5

What you need is called a service in domain-driven design. Services are used to model procedural tasks. A bulk update operation, like the one you describe, would be an ideal candidate for a service.

Solidstate answered 26/8, 2009 at 3:26 Comment(1)
the link to the DDD service appears dead (no content on page/domain)Sempstress
L
4
void DoLongInvolvedTask();

I don't see anything wrong with putting bulk tasks as methods in your repository. They don't leak anything. Having a Bulk operation does not imply any database specific operations, that is unless your method is something like ReBuildMSSQLIndexesOnMyBigTable().

Lungki answered 18/2, 2009 at 17:54 Comment(0)
A
2

You should not have any save, retrieve logic in domain object (I am assuming you are using domain model). That is the responsibility of Repository. So your bulk method belongs in repository.

If you are using ORM then your repositories won't depend on database. they would jut forward all request to the ORM layer.

If you are writing your own mapper then repository would forward request to mapper for the entity. And I think this coupling is ok.

Attainture answered 18/2, 2009 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.