This article provides practical advice on using Domain Driven Design in a Monorepo.
What is Domain Driven Design (DDD)?
DDD is used to understand the domain/concepts of the problem-solution space you are trying to solve.
They are also used to define a clear boundary of services.
Even if you are not going to use microservices right it out, it helps to define clear interfaces among the modules.
This helps you decouple your code, and make it easy to scale.
What is a Monorepo?
A Monorepo is a repository where you can have many packages inside of it, check the example below:
packages
- ledger
- account
- card
- payment
Instead of keeping all the code of your application in a single package (monolith), you can break them in many smaller packages.
It also provides more benefits such as easier code sharing, simplified dependency management, and improved build and deployment times
DDD in a Monorepo
How can we create a bounded context domain for each monorepo package? only exposing the Public API of each domain?
Each package has an entrypoint, where you can expose the Public API of the package, here is an example of ledger
// ledger/src/index.ts
export { createTransaction } from './createTransaction'
Restricting imports to internal paths in a monorepo, using Eslint
'no-restricted-imports': [
'error',
{
patterns: [
'@woovi/**/src',
],
},
],
If someone tries to import an internal path from another package like this:
import {
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
} from '@woovi/ledger/internal/path/secret'
Eslint will throw an error
In Conclusion
Using a monorepo is a good way to break your monolith before going to microservices.
Thinking in domain makes your code better to reason about it, when you have clear interfaces.
This is how we are structuring our backends at Woovi.
How are you using DDD in your product?
References
Woovi
Woovi is a Startup that enables shoppers to pay as they like. To make this possible, Woovi provides instant payment solutions for merchants to accept orders.
If you want to work with us, we are hiring!
Photo by Andrew Draper on Unsplash
Top comments (1)
How come splitting the code into packages make it easier to share it?