DevExpress has an amazing feature called “aggregation,” which is not explained terribly well in their documentation.
What is aggregation, and how does it work?
Simply, aggregation is two things:
- Cascading Delete: Aggregated objects can’t live without their parents; deleting the parent deletes the (aggregated) child. This is essentially cascading delete in database parlance.
- Aggregated Views: More importantly, fields for an aggregate object show up embedded in the parent’s form instead of linked by a drop-down.
Let’s say we had a
Person class with an
Address instance (associated 1:1). Without aggregation, when you add the address field to the person, you get an association drop-down, like so:
When aggregated, you will instead see all of the address fields on the Person detail view:
Aggregating an Object: Step by Step
So how do you create an aggregate object?
- Create the parent (aggregation-holder) and child (to be aggregated) classes, inheriting from
- Create a field in the parent with the child’s type, as you would with any other field type (eg.
- Add the
[Aggregated]attribute on the field you just created.
- In the
AfterConstructionmethod (still in the parent class), instantiate the new object (eg.
this.Address = new Address(this.Session)).
And you’re done!
Troubleshooting Aggregated Objects
Q: I can’t write to any of the fields in the aggregated object!
A: In the model editor, make sure that you haven’t set
AllowEdit to false for the field (under
Items). The default is
true. Additionally, make sure you’re instantiating a new instance of the field in the
AfterConstruction method, as described above.