Persistence ignorance in Rails. ActiveRecord 6 and beyond!

Hi guys,

thank you for your effort so far and for your contribution.

I’ve been playing around with DDD concepts lately and trying to apply them to Rails. It’s been super annoying and frustrating trying to fight the framework to apply them especially when it comes to Persistence ignorance. It would be nice if the framework supported that out of the box either with a DSL likeish framework or configuration files. Also to have a more safe typing approach regarding data attributes I have to either implement it myself or use existing libraries like dry-rb or something.

I was curious why so many years we don’t decouple active record into two pieces. One to keep it as is. Second, allow users to decouple entities and data access objects and have a framework that will support them in better domain modeling without having to write much-supporting code to do so.

I would appreciate any thoughts.

Best

LP

1 Like

Building this capability into Rails would be contrary to the current project philosophy, so don’t expect it soon.

However, Ryan Bigg has been working on similar features recently. See his reports here: ROM + Dry Showcase: Part 1 - Application + Database setup - Ryan Bigg

Thank you for your reply,

I understand that it is against the active record philosophy, it would be nice though to support both patterns and let the developer choose. I end up writing a lot of code every time I use AR only as the persistence on top of a repo instance.

Thanks again.

LP

Just my two cents here.

Do not waste time trying to apply code concepts (factories, repositories, etc.) from the Eric Evans book, if you think you are DDD only by using those, you are doing it wrong.

DDD is much more about Crunching Knowledge, Ubiquitous Language, Bounded Contexts and converging these to express your domain on your model always evolving to a deeper insight.

DDD is about tackling complexity on your software. Isn’t writing a lot of code just to implement Persistence Ignorance increasing the complexity of what you have?

Enjoy what ActiveRecord gives you in a healthy way.

2 Likes

Well said, Pedro. I don’t think there’s much interest in adding the repository pattern to Active Record.

1 Like

I don't think that using tactical patterns in DDD should be avoided. I agree that AR is awesome but expressing the domain when you involve persistence you have conflicts. As well said evolving your model should not affect my persistence layer.

Thanks for the feedback both much appreciated.

Best LP

Sorry to come late to this discussion but I was starting to look if the subject already came before creating one and I’m not sure to understand why not. Can you details the reasons why splitting ActiveRecord in a way that it allow an advanced usage could be a good way to make the library evolve? If I remember well ActiveModel is the exact example of a decoupling between what has been in ActiveRecord and has been splitted to allow advance usage. So why not the database management part? I’ve worked in a lot of projects and sometimes it’s very difficult to have the data persistence, validations and callbacks in the same place. And you don’t need to go full DDD, just trying to use event sourcing could be a pain. And it also increase the complexity because this kind of architectural behavior is conflicting with the Rails conventions.