Example Application - DDD in Ruby on Rails

There is a ton of materials on Domain-Driven Design. There are books, lectures, videos - you can choose whatever works for you. But one question is often asked - how to put all those concepts in code.

I have seen a lot of comments, that Ruby on Rails is not suitable for the domain-driven approach. That it somehow lacks the ability to hold and make use of all its elements.

Presentation of the application

Recently, at a Ruby meetup at Visuality, I have had a great pleasure to give a talk describing an example DDD application written in Ruby on Rails. The application uses plain Rails, with no additional gems (there is a glimpse of Sidekiq - just to see how well it fits).

It contains:
- Bounded contexts,
- Layered architecture,
- Application services,
- Entities,
- Aggregates,
- Repositories,
- Factory methods,
- Domain events and domain event handlers,
- Read models,
- and much more

All of the above, without using any additional dependencies. The presentation is in Polish - I will update this article if I ever have a chance to give this lecture in English. However, the slides are in English so you can always take a peek at how it’s all done.

If you are interested, please check it at https://www.youtube.com/watch?v=1h1ML-dUnLQ

Code Example

Unfortunately I cannot share the code for this application. I use it for educational purposes - for individual research and in-house workshops.

However, I've written a small example, which contains (or will contain shortly) all the pieces included in the presentation. If you'd like to play with it, please take a look at https://github.com/pstrzalk/ddd-example-in-ruby-on-rails

Honestly, it's just a piece of code written in a few hours. It's not perfect (by far) and it's not supposed to be. I wanted to give you a piece of code, which can be the base for future discussions. I would love if you could let me know on Twitter / Mastodon / Miłośnicy Ruby discord if you found it useful or if you have any comments.

Articles in this series