Skip to main content
Inspiring
October 10, 2008
Question

Best Practice Question - Business Logic in Value Objects?

  • October 10, 2008
  • 3 replies
  • 926 views
Just wondering what people's thoughts on best practices for setting properties of Value Objects.

For instance, I have several getter/setters in one of my Value Objects with logic in the setter that uses the value to set values of other properties.

For example, I have a Value Object that has the following properties:

category (of type Category, which is another Value Object with properties "name" and "id")
categoryId (of type int)
categoryUpdated (of type Boolean)

I have a collection of Category Objects in the Model. When I set the categoryId of this class, I set the "categoryUpdated" to true, and dispatch an CairngormEvent to that find the "category" with the specified "categoryId" and set the "category" property to this item.

So what is the best practice? To simply make the "categoryId" a public variable, and create a new Event/Command to perform all this logic? Or is it ok to do it all in the Value Object setter?

Thanks.
This topic has been closed for replies.

3 replies

Participant
October 12, 2008
Hello,

I have a similar question to the original, from time to time I would like to be able to return from my VO the VO's properties in XML format, for example I may have a VO such as:

[Bindable]
public class exampleVO implements IValueObject
{
public var property1 : String;
...
}

It makes sense to me to add a toXML method:

public function toXML() : XML
{
var voXML : XML = ;

voXML.appendChild( this.property1 );
...

return voXML;

}

Should this be in a separate Class that wraps the VO:

[Bindable]
public class example
{
public var vo : exampleVO;
...
}

Then all access to the vo would be through this class and the toXML and any other methods should appear here?

Cheers

Rory
Inspiring
October 11, 2008
That's the thing - I am updating the CategoryID with a value returned by a WebService - no user interaction.

The Category is updated using a Cairngorm Event/Command called from the setter.
Participating Frequently
October 11, 2008
Hi Eric,

I can't speak for best practices, but the only logic I've ever added to a VO were getters: an example was a set of getters on an airline flight VO to get overall flight departure/arrival times/cities from an array of flight segments in a property of the VO.

I feel uneasy (in the nicest possible way) about your VO for two reasons: firstly it has a strong dependency on bits of the Cairngorm framework to look up the category (and VOs normally don't need to depend on anything), and secondly the intent of Commands in Cairngorm is more to represent user gestures than to wire up VO properties (I often see people shoehorning stuff into Commands that might be better of as plain old business utility classes). I would rather see an UpdateCategoryCommand (that's what the user is trying to do?) that updates categoryId and hits a delegate to populate the category property.

That said you might have a very good reason for doing this. Could you tell us where the code is that's setting categoryId, and if anything is using categoryUpdated?

Cheers,
Robin