My Authors
Read all threads
What's wrong with this controller?
How would you improve the design?
Separation of concerns.
This controller is responsible for way too much.

Controllers are a part of the infrastructure layer.
Infrastructure doesn't hold application or domain logic.
Keeping application & domain logic out of infrastructure allows you to switch and support different vendors, frameworks, and transport-layer technologies easily without duplication.

Example: migrating from REST to GraphQL, or serving them both simultaneously!
So currently, one of the main issues here is that this controller holds both:
- the domain logic that specifies how to create a comment
- the application logic that specifies what should happen
This is bad for several reasons:

- we have to repeat the validation logic in every controller (- maintainability)
- we have to **remember** to call upon the validation logic (- security)
- increases file size & duplication (- size)
- there's no architecture! (- maintainability)
Here's a substantial improvement to the structural software quality of this controller.
1. Proper separation of concerns.
- Inner layers are responsible for application & domain logic.
- Validation logic is represented as a domain concept in a Value Object.

khalilstemmler.com/articles/types…
2. Strictly-typed Functional Error Handling.

Every command has one way it can go right, and at least one way it can go wrong- usually more. Those ways it can go wrong deserve to be strictly typed so that we can map 'em to error responses.

khalilstemmler.com/articles/enter…
Make Illegal States Unrepresentable!

Types are a good programming practice. With types, we can do something that comments can't: enforce policy.

We can make it virtually impossible to perform illegal operations

khalilstemmler.com/articles/types…
For example, it is now virtually impossible to issue the `PostComment` use case without a valid `Comment` instance.

It is also impossible to create a valid `Comment` without passing in a valid string text.

Here's an example of the CommentText value object.
If you're looking for a decent example of an application that uses the DDD and Clean Architecture principles using TypeScript, DDDForum is my best example **so far**.

github.com/stemmlerjs/ddd…
Design principle: "Strive for loosely coupled design between objects that interact".

As for handling the side-effects of issuing this use case, like sending a slack message once the comment is posted, it's best to use some form of the Observer pattern.

khalilstemmler.com/articles/types…
Missing some Tweet in this thread? You can try to force a refresh.

Enjoying this thread?

Keep Current with Khalil Stemmler

Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!