Skip to content
danschultz edited this page Apr 21, 2011 · 4 revisions

Aggregates allow your models to group related sets of properties into value objects. This feature allows you to express relationships such as a Person is composed of an Address. Each aggregate describes how to construct a value object from an entity's properties, and how value objects are turned back into values on the entity.

Aggregates are normally defined in the model's constructor:

public class Customer extends Entity
{
	[Bindable] public var address:Address;
	[Bindable] public var street:String;
	[Bindable] public var city:String;
	
	public function Customer()
	{
		super();
		aggregate("address", Address, ["street:street","city:city"]);
	}
}

public class Address {
	public function Address(street:String, city:String) {
		_street = street;
		_city = city;
	}
	
	private var _street:String;
	public function get street():String {
		return _street;
	}
	
	private var _city:String;
	public function get city():String {
		return _city;
	}
}

With this definition, whenever customer.street or customer.city change, a new Address will be created and assigned to customer.address. Inversely, if customer.address changes, then customer.street and customer.city will be updated to the new values in Address.

customer.street = "123 Acme St."
customer.city = "San Jose";
customer.address = new Address("123 Acme St.", "Mountain View");
trace(customer.city); // Mountain View

customer.city = "San Francisco";
trace(customer.address.city); // San Francisco

Value objects are normally defined with a multiple attributes, like Address. The mapping definition for the aggregate defines the order of the arguments in the value object's constructor.

Writing Value Objects

Value objects – not to be confused with VO's or transfer objects – represent immutable data types where its attributes define its identity. For instance, money can be represented as a value object where two Money objects of $20 should be equal to each other. Contrast this to an entity, where an entities equality is determined by its identity, which is usually some unique ID given by a database.

Value objects should define an equals() method that accepts an instance of the value object, and compares the attributes of each other. This method is used when determining if an entity is dirty or not.

It's important to treat value objects as immutable, by not creating methods on a value object that can change its attributes. For instance, if Money had an add() method on it, var sum:Money = money.add(new Money(10)); should return a new instance of Money.

Clone this wiki locally