DEV Community

Cover image for Domain Driven Design using a Monorepo
Sibelius Seraphini for Woovi

Posted on

Domain Driven Design using a Monorepo

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.

Image description

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

Restricting imports to internal paths in a monorepo, using Eslint

'no-restricted-imports': [
      'error',
      {
        patterns: [
          '@woovi/**/src',          
        ],
      },
    ],
Enter fullscreen mode Exit fullscreen mode

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' 
Enter fullscreen mode Exit fullscreen mode

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

Domain Analysis


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)

Collapse
 
erick_tmr profile image
Erick Takeshi

How come splitting the code into packages make it easier to share it?