In my application I have Users, Organisations and Employees. There are domain objects for each of these, and database mappers.
When a User initially sets up an Organisation, I want to automatically create an Employee record for that User/Organisation combination. (The type of record will be Owner). Basically this comes down to two database inserts: one for Organisation, the other for Employee.
I can see two ways to go about this:
- The controller can create the new Organisation object and use a mapper,
orgMapper::insert($newOrg), to persist it. With that done it can then create the new Employee object and again use a mapper,employeeMapper::insert($newOwner), for persistence. - The controller can create the new Organisation object, and this object does the rest of the work itself. It would call
orgMapper::insert($this), and with the key which is returned would then create and persist the new Employee object.
There seems to be a number of benefits in the second approach: the business logic (i.e. an employee/Owner record for each new Organisation) is embedded in the model, and the controller is skinnier. The only reason I hesitate is that this means my domain is becoming dependent on my mapper.
I would use a factory in the domain to get the mapper, so all coupling would come down to a single point. Does this make the dependency acceptable? Or do I have a deeper design issue?
I have read a bit about a service layer. Perhaps this is where I should be looking?
I am using PHP and Zend Framework
Your thoughts are much appreciated. My cat and I have exhausted our own ideas…
Since I am not a ZF user (and have been avoiding it), maybe my solution would not apply.
Data mappers are not supposed to work with just a single table in database. Instead, when you are storing the new
Organizationentry (and i guess theOrganizationinstance contains a collection ofEmployeeobjects), the mapper also should create all the entries for the Employees, and the accompanying data inOrganizationEmpoyeestable.The short answer: neither, storage is responsibility of mapper.
If I misunderstood the DB structure, and how it relates to “(..) Employee record for that User/Organisation combination” and the the
Employeedomain objects are completely separate fromOrganization, then the 1st of your solutions would be acceptable.As for adding service on top of domain objects and mappers, that would be a good idea. Because you have been leaking domain logic inside the controller already. Seems like a bad thing to do.