D
D
dmitriy2015-10-03 12:22:58
PHP
dmitriy, 2015-10-03 12:22:58

How to organize a factory to create objects?

In the process of studying DDD, I will immediately say that I do not use ORM. There are the following entities:
UserService, User(Entity),Group(Entity),Address(ValueObject)
To get a user with connections, I do something like this:

class UserService{

function findByid($id){
     $User = $this->UserRepository->find($id);
     $Group = $this->GroupRepository->find($User->getGroupId());
     $User->setGroup( $Group ) ;
     $User->setAddress(new Address( $User->getCity(), $User->getStreet()  ));

return $User;
}

}

The question is, where to build the finished entity for further use, maybe in the factory, something like $UserFactory->build( $User, $Group ); but then the question is with saving the entity in the database, somewhere you need to translate the object into a format suitable for saving, perhaps you need a UserMapper that will do this, or do all the work with saving through the UserService?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-10-03
Protko @Fesor

I’ll say right away I don’t use ORM

At once I will tell - use ORM.
And where is DDD? Where is the common language?
In a repository or in one of its dependencies. In particular, in Doctrine2, the repository requests the entity in the entity manager, which first loads them into the unit of work, which stores everything in an identity map in order to avoid duplicate entities (if we somehow request the same entity, The ORM will return the same object to you, which makes using the ORM native and convenient). The internal mapper maps data from the database to entities using hydrators.
When saving, a Unit-of-Work flush occurs, during which changes in all entities are calculated and SQL is generated.
that is, in any case, you need to implement an object mapper or take a ready-made one, then you need to implement Unit-of-work, then a data loader from the database, and so on. I was thinking about the option of an easy replacement for Doctrine2 and realized that this is practically impossible in principle. It is for this reason that there are not many implementations of data-mapper, for PHP it is only a doctrine, for Java it is Hibernate, for Python it is SQLAlchemy ... and there are no more implementations.
Uncle Bob in one of his reports on the topic of application architecture (I forgot the name, I'll look) told a lot of things on this topic, they say, about the "do not use ORM" pens that were in the 90s, and why the implementation of a date mapper is a very difficult task. ...
ps conclusion - take Doctrine2 ORM and don't clutter your head with trying to implement a date mapper yourself - this is a very difficult task. That is why there are not many implementations of this approach.

A
Alew, 2015-01-01
@Alew

The repository (DDD) must return READY objects (in DDD aggregates). How he collects them, himself or delegates a dozen finely chopped factories, whether he uses ORM or not, this is the tenth matter. In general, I recommend double-checking your understanding of the basic ideas of DDD.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question