Search code examples
laraveldomain-driven-designentityrepository-patternhexagonal-architecture

DDD Laravel. Repository pattern. How to retrieve an object from persistency and convert it into a not Laravel Entity model?


I'm aplying DDD in Laravel.

In this architecture, the entity (conformed by the corresponding value objects) is not a Laravel Model extended class (because the domain layer needs to be agnostic to the infrastructure)

So... when I retrieve some data inside the repository implementation, the result is a stdclass object, and I need to return it as the entity object.

Anybody knows the best approach to do this?

Thanks!


To get this, I tried to convert from stdclass to entity by manual, but it's look hacky and dirty.


Solution

  • Ok, got it.

    I found two different approaches, just in case others are fighting with the same problem.

    Option 1: Embracing the Eloquent Active Record.

    Inside the infrastructure layer, I created a Eloquent model to represent the Entity, and I use it as a vehicule for eloquent queries. Like this, all the conection with the framework stay contained in the infrastructure, without polluting other layers.

    Option 2: Apply Doctrine in Laravel.

    Doctrine has a package for laravel. Doctrine, as occurs in Synfony, is using data mapping, so no worries with that.

    Thanks anyway!