A SOLID hackernews-inspired forum site built with TypeScript using the clean architecture and DDD best practices.
DDDForum.com is the application that we build in solidbook.io - The Software Design and Architecture Handbook.
You can run the project using Docker. First, copy .env
template file. Feel free to change passwords and app secrets.
cp .env.template .env
Then build and run the image.
docker-compose up
You can visit the app by going to http://localhost:3000
.
Note
: It's currently deployed on free tier Heroku, which has some undesirable side-effects like shutting off the server during periods of inactivity. So if it's down for you, refresh a couple of times. Thinking about migrating this to a serverless architecture later on.
- Sequelize - The ORM for Node.js
- Express.js - Lightweight webserver
- Redis - For holding onto JWT tokens and refresh tokens
We built this based on the Clean Architecture, SOLID principles, and Domain-Driven Design best practices using TypeScript.
There's obviously a lot that went into building this from front to back.
The Clean Architecture is a way to reason about where different types of application logic belongs.
There's a lot more to learn about the clean architecture, but for now- just know that it's a way to really separate the concerns of everything that goes into building complex enterprise applications. You'll never see any infrastructure
-related code alongside domain
layer code.
The clean architecture, when combined with Domain-Driven Design, is very powerful :)
In DDD, we build applications on top of a number of subdomains.
A subdomain is a cohesive unit of code that represents exactly one core concept and is responsible for a specific set of concerns in an application architecture. For example, every appliciation has a
users
subdomain. That's responsible for users, identity & access management, authentication, authorization, etc. Sometimes you don't want to build that yourself. Sometimes you can go with an off-the-shelf solution like Auth0. But there are subdomains in your application that you cannot simply outsource. These are the family jewels; the thing that's actually novel about your app. This is the subdomain that no one (except you) can code. Know why? Because only you have the domain knowledge to build it exactly the way that it should be built. You understand the domain.
In DDDForum, we have 2 subdomains: The users
subdomain and the forum
subdomain.
Each subdomain has a:
domain
layer: where the highest-level policy, domain objects, and domain rules belong (user
,email
, etc)application
layer: where the use cases / features that utilize domain objects belong (createUser
,login
, etc)adapter
layer: where we define abstractions so thatapplication
layer code can interact withinfrastructure
layer concepts, without actually requiring oninfrastructure
(because that would break the dependency rule). Here we write things likeIUserRepo
- repository adapter,IJWTTokenService
- an abstraction of a cache (redis) that manages tokens, etc.infrastructure
layer: where we create concrete implementations of the abstractions from theadapter
layer so that they can be spun up at runtime thanks to the power of polymorhpism :) (more on this later).
If you haven't already, I recommend you read this article on use cases and subdomains.
Let's identify some of the actual concepts that exist in each subdomain.
In the users
subdomain, we're only concerned with concepts that are related to authentication, roles, etc. Here are a few examples of classes and concepts that exist at each layer.
domain
layer:user
(aggregate root),userEmail
(value object),userCreated
(domain event).application
layer:createUserUseCase
(use case),getUserByUserName
(use case).adapter
layer:IUserRepo
(respository interface adapter)infrastructure
layer:SequelizeUserRepo
(a concrete implementation of the IUserRepo),UserDTO
(data transmission objects).
In the forum
subdomain, we're only concerned with concepts that have to do with building a forum. You won't see any domain concepts from the user
in forum
. In the forum
subdomain, the concept most equivalent to a user
, is a member
.
Here are a few examples of concepts from the forum
subdomain.
domain
layer:member
,comment
,post
,postVote
,commentVote
,commentVotesChanged
application
layer:replyToComment
,getMemberByUserName
,upvotePost
,downvotePost
adapter
layer:ICommentRepo
,IPostRepo
,IMemberRepo
infrastructure
layer:SequelizeCommentRepo
,SequelizePostRepo
,SequelizeMemberRepo
DDDForum is an open source project, and contributions of any kind are welcome! Open issues, bugs, and enhancements are all listed on the issues tab and labeled accordingly. Feel free to open bug tickets and make feature requests. Easy bugs and features will be tagged with the good first issue label.
Thanks goes to these wonderful people (emoji key):
Anthony Denneulin π» |
Khalil Stemmler π» π π π π€ |
Faisol Chehumar π» |
Trung Tran π |
This project follows the all-contributors specification. Contributions of any kind welcome!
This project is licensed under the ISC License - see the LICENSE.md file for details