How To Decide Where To Put Domain Logic | Domain Driven Design w/ TypeScript

In this post, I walk you through a process for determining where different types of domain logic belongs in a Domain-Driven Design project.

The reason we use Domain-Driven Design is because when our applications get sufficiently complex, we need to figure out better ways to organize domain logic than how we normally do with MVC in RESTful CRUD applications.

Domain Events

During the process of figuring out our requirements and understanding the domain, sometimes we'll hear language being used that sounds similar to:

"when this, then do this" or "when this, do this only if this has this"

When we hear that, we should automatically think "Domain Events".

Domain events should be named in the form of [Entity][Past Tense Verb]Event.

Some examples are: VinylCreatedEvent, VinylUpdatedEvent, OfferAcceptedEvent, OfferDeclinedEvent.

Just the, it naturally signifies that the model will be responsible for the verb/method.

Therefore: VinylCreatedEvent = Vinyl.create(props: VinylProps, id?: UniqueEntityId).

Therefore: VinylUpdatedEvent = vinyl.update(props: VinylProps).

Therefore: OfferAcceptedEvent = offer.accept().

And it just so happens that we would also create those domain events within those entity method invokations.

Validation and Invariant Logic => Value Objects & Entities



Discussion

Liked this? Sing it loud and proud 👨‍🎤.


0 Comments

Be the first to leave a comment

Submit

Stay in touch!



About the author

Khalil Stemmler,
Developer Advocate @ Apollo GraphQL ⚡

Khalil is a software developer, writer, and musician. He frequently publishes articles about Domain-Driven Design, software design and Advanced TypeScript & Node.js best practices for large-scale applications.



View more in Domain-Driven Design



You may also enjoy...

A few more related articles

How to Handle Updates on Aggregates - Domain-Driven Design w/ TypeScript
In this article, you'll learn approaches for handling aggregates on Aggregates in Domain-Driven Design.
Decoupling Logic with Domain Events [Guide] - Domain-Driven Design w/ TypeScript
In this article, we'll walk through the process of using Domain Events to clean up how we decouple complex domain logic across the...
Does DDD Belong on the Frontend? - Domain-Driven Design w/ TypeScript
Should we utilize Domain-Driven Design principles and patterns in front-end applications? How far does domain modeling reach from ...
An Introduction to Domain-Driven Design - DDD w/ TypeScript
Domain-Driven Design is the approach to software development which enables us to translate complex problem domains into rich, expr...

Want to be notified when new content comes out?

Join 5000+ other developers learning about Domain-Driven Design and Enterprise Node.js.

I won't spam ya. 🖖 Unsubscribe anytime.

Get updates