Aggregate Objects in DevExpress

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:

  1. 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.
  2. 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?

  1. Create the parent (aggregation-holder) and child (to be aggregated) classes, inheriting from BaseObject.
  2. Create a field in the parent with the child’s type, as you would with any other field type (eg. String).
  3. Add the [Aggregated] attribute on the field you just created.
  4. In the AfterConstruction method (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!
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.

About Ashiq Alibhai, PMP

Ashiq has been coding C# since 2005. A desktop, web, and RIA application developer, he's touched ASP.NET MVC, ActiveRecord, Silverlight, NUnit, and all kinds of exciting .NET technologies. He started C# City in order to accelerate his .NET learning.
This entry was posted in Libraries, Web and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *