質問

I am using DDD now for quiet some time, it took some time to get used to and separate everything. But am now stuck at updating my entity ...

Setup

Now I have a repo interface that defines the following methods

/**
 * @param AccountEntity $account
 * @return AccountEntity
 */
public function create(AccountEntity $account);

/**
 * @param AccountEntity $account
 * @return mixed
 */
public function update(AccountEntity $account);

My repo does exactly 0 in the function because it passes it to the mapper, which in turn creates/updates the data. So far so good.

The application service has the method create which accepts an array, the array is validated and if valid it will use an EntityBuilder to create the entity. (Entity requires the data by __construct). If the data is invalid it will throw an exception.

Now my issue is how to handle my update in the application service.

I get an array of data, just like the create, in the update and an id of the entity.

  1. Should I use an hydrator to loop over the array and update the fields in the entity and then throw it to my repo.
    • cons, I then have an entity hydrator and builder.
    • pros, it is easy in use
  2. Should I just validate the data and throw the array + id to the repo and let him figure out what to do
    • cons, just looks wrong
    • pros, again easy to make
  3. Should I do toArray or something similar on my entity, merge that with the form data & then use the entity builder to build the entity again and throw the entity then to the repo
    • cons, don't think an entity should have a toArray function
    • pros, sounds like it encapsulates the task of building the entity in one place so that seems right.
  4. ?

So in short, how to convert an array with data to an entity, or how to save it. I am not using Doctrine in any way, and not planning to

役に立ちましたか?

解決

First of all, your input data (array) should be validated somewhere at the controller level, to ensure is in the correct format (we're not talking about business rules, just formatting).

Then your entity can have something like this

 class MyEntity
 {
 public function update($data)
 {
     //update properties, enforce the relevant business rules
   //perhaps events are generated
 }
 }

The controller will probably use a service method to do the updating. The service will ask the repository for the entity, eventually creates the input format the entity expects it (if there is a difference) then calls the update method .

Then you send the entity to the repository which takes care of persisting it. Remember that the Repository is there to save/restore your entities not to change them.

他のヒント

This should probably have been a comment, but that requires 50 rep...

You should have a look at this article about datamappers: http://www.sitepoint.com/integrating-the-data-mappers/

I have been exactly in your situation, and the articles by that writer (Alejandro Gervasio) helped me immensely.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top