mardi 17 mai 2016

Where do I place my lengthy code that prepares domain objects to be persisted in a Repository?

I have some code like so:

/* Part of Controller::saveAction() */ 

//create new object instance
$item = new Item();

//populate the Item
$item->setDescription($description);
$item->setQuantity($quantity);
$item->setPrice($price);

//once we have a fully populated object, 
//send it to Repository pattern, 
//which saves it to persistent storage
$this->repository->saveItem($item);

Imagine several blocks like the above in a single funciton/method, and you will see my issue ... I am pretty happy with my repository line of code, but I do not know where to place all the "prep work" that is done before the call to Repository.

Question:

Where do I place the bulk of code that creates &populates the Item object instance? It is cluttering up my Controller method and I can't seem to imagine another place to put it.

Goal

My goal is to find good structure/design rather than to purely reduce or minimize the number of "item preparation" lines.

Depending on Controller, I have roughly 5-7 Item instances that each have 10-16 lines of code creating and populating the instances.

Aucun commentaire:

Enregistrer un commentaire