Question

I am developing an application in Zend Framework and have the following layers: controller, service, domain model and mapper.

My domain objects need to be represented as arrays for several different purposes. For example, the mapper turns the object into an array prior to persisting it; the form requires an array as a parameter to its populate() method; and there are a few other examples too.

So, my question is, in what layer should I be doing the translation to an array? The translation for an object varies slightly depending on the context in which it is used, e.g. the object ID is not required when persisting a new object, but is required when persisting updates, or when populating a form.

As far as I can see there are two options. Option 1 is to do the translation in each component (i.e. the mapper, the form, etc). Option 2 is to put it in the domain object itself, and let the client code pass a parameter to determine which array variation they require, e.g. $array = $domObject->toArray('populateForm');

Option 1 would involve a fair amount of duplication, but it seems to adhere to the separation of concerns rule. Option 2 would avoid the duplication, but I'm not sure that my domain object should be concerned with representing itself in variety of different ways.

Perhaps the domain object should just produce a 'vanilla' array, and each component should tailor it for its own use? And another point relates to patterns. Should I be looking to apply the decorator or some other pattern?

Your thoughts are much appreciated!

Was it helpful?

Solution

My thoughts on this are first, do you think you are doing this the right way? It sounds fairly complicated without any real knowledge about the project. It feels like you are trying to do a typical solution someone very new to Zend would do to then realise it was unnecessary. Then again I do not know alot about your project.

Secondly, I would probably put such functionality in the service layer. Service layer to me has almost always been about manipulating objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top