kgrzybek / modular-monolith-with-ddd

Full Modular Monolith application with Domain-Driven Design approach.
MIT License
11.12k stars 1.75k forks source link

database call in entity? #28

Closed shijialee closed 4 years ago

shijialee commented 4 years ago

Hi,

Coming from a dynamic language developer background, I am little puzzled by this piece of code:

 var nextWaitlistMember = _waitlistMembers
                .Where(x => x.IsActive())
                .OrderBy(x => x.SignUpDate)
                .FirstOrDefault();

from https://github.com/kgrzybek/modular-monolith-with-ddd/blob/c680815b5c9d8af2d0a42ca768ba2d15312a0558/src/Modules/Meetings/Domain/Meetings/Meeting.cs#L177

That looks like a db query in entity to me. Am I mistaken? I read that calling repository from Aggregate/Entity is not recommended in DDD.

Can someone shed some light?

thanks

James

rmaziarka commented 4 years ago

Hello,

_waitlistMembers is a List, therefore there is no query to the database: private List<MeetingWaitlistMember> _waitlistMembers;

kgrzybek commented 4 years ago

As @rmaziarka wrote, there is no query to the database.

Aggregate pattern, in DDD terms, is immediate consistency boundary so during Aggregate loading you load every aggregated object (like _waitlistMembers list) to memory and save the whole Aggregate in one transaction.

Additional note: There is a pattern called Lazy Loading when objects are loaded as late as possible but this is often considered as an anti-pattern. I don't recommend this approach too and it is not used in this project, but I know DDD practitioners who use it.

shijialee commented 4 years ago

Thanks.

So that .Where().OrderBy()... usage is the from LINQ? That's the first reference when searching FirstOrDefault.

kgrzybek commented 4 years ago

Yes, this is LINQ to Objects https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-objects