The intention of this post is to share the data that I have gathered in researching and experimenting in making domain models 100% ignorant of persistence and making them ORM unaware. This will also serve as a personal note where I can refer back if I need to.

If you’re wondering what’s the meaning of Hexagonal architecture and how it works, you can refer to these links:

http://victorsavkin.com/post/42542190528/hexagonal-architecture-for-rails-developers

https://www.youtube.com/watch?v=u6oTg5oRH24

What I mean by ORM unaware is the domain models are not tied to an ORM and when you’re using a repository, the objects being returned are just POPOs (plain old php objects in PHP / plain old python objects in Python).

Regarding Python, I tried a popular data mapper named SQLAlchemy. It’s really a nice tool. The only downside that I see is the objects being returned when you fetch a record in the database is a POPO but it is ORM aware (Objects being returned is an instance of SQLAlchemy). I think its a leaky abstraction but I guess they need to do that so they can support things like lazy loading etc…

Gain the skills you need to be a pro at Python development: http://treehouse.7eer.net/c/245500/245646/3944

In PHP, one popular data mapper is Doctrine 2. At first, I thought that the objects being returned when you fetch a record is a POPO but it is ORM aware. But Matthew said that the objects being returned are POPOs and also ORM unaware. I haven’t tried it though. But if its accurate, then its really a good thing. But I’m wondering how do they support things like lazy loading if the POPOs are really ORM unaware. If you know the answer, kindly share it by leaving a comment.

This is a demo app that demonstrates Hexagonal architecture in PHP.

The domain models are 100% ignorant of persistence and are ORM unaware (if Matthew is right) so the core is framework agnostic. It can be dropped in to any framework and wired up to an implementation. He really did a great job demonstrating how it works.

Core:

Core used in Laravel 5:

If you’re interested in Domain Driven Design and REST, check this post that Matthew wrote: http://mattallan.org/2016/rest-and-ddd/

If you have some additional information that you want to share that is related to this post, kindly share it by leaving a comment.

Here is our conversation earlier: