Kill Anemic Domain Models with Encapsulation: The Hard Part

  Рет қаралды 9,840

Zoran Horvat

Zoran Horvat

Күн бұрын

Пікірлер: 35
@MahmoudIsmail-rw7hg
@MahmoudIsmail-rw7hg Жыл бұрын
thanks Zoran, I like your explanation.
@milanmladenovic
@milanmladenovic Жыл бұрын
Hvala Zorane
@WolfieVenturi
@WolfieVenturi Жыл бұрын
Awesome
@samosimoncic5020
@samosimoncic5020 Жыл бұрын
amazing video. I do not understand how you tell the ef that AuthorsCollection has to be loaded in Book. If AuthorsCollection is internal (in domain project) and the repository is placed in an infrastructure project, it is impossible that Book DbSet reaches AuthorsCollection and includes them when GetBooks method from repository is called.
@zoran-horvat
@zoran-horvat Жыл бұрын
Entity Framework is using reflection. It can even populate private members if needed, though you must specify non-public members with a string, rather than with a lambda.
@nickbarton3191
@nickbarton3191 9 ай бұрын
​​@@zoran-horvatIt's been years since I've written a dB application and haven't used EF. So it uses reflection, that's awesome. I've watch many of your videos wondering how the data is materialised, now I know. But I dislike Blazer, once again MS have gone for interleaving HTML and code, so messy. This is no better than classic VB Web pages in this respect, written plenty of those. I much prefer markup like AngularJS. Maybe HTMX has the solution to this old problem.
@zoran-horvat
@zoran-horvat 9 ай бұрын
@@nickbarton3191 Blazor is just one of the implementations of WebAssembly, which is a separate standard defining a virtual machine and the corresponding byte code. It has little to do with Microsoft. Wasm can execute on any system, such as a browser or an embedded system, though it seems like only the browsers have accepted it so far.
@nickbarton3191
@nickbarton3191 9 ай бұрын
@@zoran-horvat Ah OK, I'm out of touch with web programming.
@alexeycherepanov9150
@alexeycherepanov9150 8 ай бұрын
10:20 - why did you not sort list in AuthorsCollection's setter?
@zoran-horvat
@zoran-horvat 8 ай бұрын
Because it is populated by EF.
@alexeycherepanov9150
@alexeycherepanov9150 8 ай бұрын
10:20 there is easy way to do same - you can sort authors in setter of AuthorCollection property.
@zoran-horvat
@zoran-horvat 8 ай бұрын
That property is managed by EF.
@alexeycherepanov9150
@alexeycherepanov9150 8 ай бұрын
​@@zoran-horvat and that is problem with it?? ef still need to call setter...
@zoran-horvat
@zoran-horvat 8 ай бұрын
@@alexeycherepanov9150 It is not the problem with that property. It is its purpose. How would you sort it anyway? It is an ICollection.
@alexeycherepanov9150
@alexeycherepanov9150 8 ай бұрын
@@zoran-horvat something like this: private ICollection _authorsCollection; internal ICollection AuthorsCollection { get => _authorsCollection; set => _authorsCollection = value.OrderBy(x=>x.Ordinal).ToArray(); }
@alexeycherepanov9150
@alexeycherepanov9150 8 ай бұрын
@@zoran-horvat something like this: private ICollection _authorsCollection; internal ICollection AuthorsCollection { get => _authorsCollection; set => _authorsCollection = value.OrderBy(x=>x.Ordinal).ToArray(); }
@superpcstation
@superpcstation Жыл бұрын
Hi Zoran, doesn't the bool TryMoveAuthorUp(int index) functions breaks the command/query separation principle? Or is it something you don't worry about?
@zoran-horvat
@zoran-horvat Жыл бұрын
Command/query separation principle has a corollary: it is common to return the command execution status in situations when querying for the same information after the command would look awkward, inefficient, needlessly complex or be outright impossible. A common case is when a command creates a child object and adds it to private structures that it also returns the newly created object, or at least a reference to it, like an ID, for the caller's bookkeeping (e.g. to add it to a DbContext, so that the child object will be persisted). However, it would be a violation of the principle if the command would return a result that would otherwise qualify as a query. The danger comes from some caller using such command in order to fetch its result, not knowing, or tolerating, the side effects caused by its action, which might not be appropriate in that situation.
@software_developer5
@software_developer5 Жыл бұрын
I am working on a project, where we are using anemic design pattern throughout the project. We have anemic domain classes which are used everywhere and we have services to perform business logic, we are also using CQRS which then call these services so buniess logic divided between services and CQRS. Can you make a video or suggest how to move from anemic design model to rich domain in such cases
@zoran-horvat
@zoran-horvat Жыл бұрын
I have plans for a series of videos dedicated to the gradual process of improving some typical deficiencies in the design, though I still don't have a timeline when I will start recording them. I also have a workshop ready, which I am holding in companies, titled Gradually Improving Software Design. If your managers are interested in organizing a training for your team, they can contact me directly for details.
@VoroninPavel
@VoroninPavel Жыл бұрын
Once again. I hate EF Core for poor DDD support. All these code tricks with Lazy and alike are needed because navigation properties cannot be set through constructor. And there's no way to force EF to call some private method after entity is materialized. EF team does an excellent job and delivers many features, but in most cases those features are adding convenience for anemic models. ='(
@7th_CAV_Trooper
@7th_CAV_Trooper 8 ай бұрын
I agree. It's time for MSFT to rewrite EF with modern design principles in mind.
@TreeLuvBurdpu
@TreeLuvBurdpu Жыл бұрын
They could still sort by last name.
@zoran-horvat
@zoran-horvat Жыл бұрын
Yes, but that wouldn't make it the list of authors of a book.
I Asked Copilot To Write Unit Tests - It Wasn’t Great
13:53
Zoran Horvat
Рет қаралды 18 М.
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН
How to Avoid Null Reference Exceptions: Optional Objects in C#
18:13
Is an Anemic Domain Model an Anti-Pattern?
9:54
Milan Jovanović
Рет қаралды 23 М.
Master the Power of Composite Pattern in Rich Domain Modeling
14:07
This Is How the Anemic Domain Model Turns Bad
13:08
Zoran Horvat
Рет қаралды 10 М.
17 Pieces of C# Syntax That Make Your Code Short
12:41
Zoran Horvat
Рет қаралды 27 М.
Use Null Object Pattern in Your Rich Domain Model
13:16
Zoran Horvat
Рет қаралды 12 М.
Is an ANEMIC Domain Model really that BAD?
10:36
CodeOpinion
Рет қаралды 18 М.
Why Favor Object Composition Over Class Inheritance? A Deep Dive
19:00
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН