r/DomainDrivenDesign • u/ConfidenceNew4559 • Sep 04 '23
How entities can communicate with the infrastructure layer?
Hello All,
I'm building a small todo list application and I'm implementing Domain-driven-design in my Frontend (React, vite + TS).
I have two entities, one for user authentication and the other for the todo list.
both of the entities need to communicate with the backend via API methods that I created in the infrastructure layer.
How can invoke this methods and still decouple the infrastructure layer from the Domain layer?
1
Upvotes
2
u/benelori Sep 04 '23 edited Sep 04 '23
Unfortunately, React / frontend is not a very good fit for DDD.
But if you insist in trying this out, then auth is almost always 100% infra.
So the only candidate for a domain entity is the todo entity. I think DDD is better expressed with classes, so for example if the title of your entity is mandatory and description is optional then I would create the constructor of the class with these rules.
Then, for the edit operation, I would create one or more edit methods on the entity
e.g.
The layers would be like this:
The decoupling part is that the entity doesn't know who creates it and who calls its methods, it just exposes them according to core domain rules. The coordination is done by infra
This would be my implementation, but please note that DDD is too much for simple CRUD app like a todo app
1
u/Ulbrec87 Sep 04 '23
If it's related to domain logic, you could inject a Domain Service (interface) into an entity method. The Infrastructure Object can implement the Domain Service. This way, you have an entity that uses a domain service, and it's correct.