Skip to main content
.NET 6.0+

How to: Create an Aggregated Object

  • 3 minutes to read

An aggregated object is considered a part of the owning object and cannot exist separately. When the owner is deleted, aggregated objects must be deleted as well. XPO can handle aggregated object deletion automatically. To do this, use the AggregatedAttribute to specify links to aggregated objects. In the following code example, the DefaultAddress and Addresses properties are considered to be a natural part of the Person objects, so we mark these properties as aggregated.

public class Address : XPObject {
    // For brevity, we omitted the declaration of constructors
    // and fields corresponding to persistent properties.
    // ...
    public string City {
        get { return fCity; }
        set { SetPropertyValue(nameof(City), ref fCity, value); }
    }
    string fCity;

    public string Street {
        get { return fStreet; }
        set { SetPropertyValue(nameof(Street), ref fStreet, value); }
    }
    string fStreet;


    [Association("PersonAddresses")]
    public Person Owner {
        get { return fOwner; }
        set { SetPropertyValue(nameof(Owner), ref fOwner, value); }
    }
    Person fOwner;

}

public class Person : XPObject {
    // For brevity, we omitted the declaration of constructors
    // and fields corresponding to persistent properties.
    // ...
    public override void AfterConstruction() {
        base.AfterConstruction();
        DefaultAddress = new Address(Session);
    }

    [Aggregated]
    public Address DefaultAddress {
        get { return fDefaultAddress; }
        set { SetPropertyValue(nameof(DefaultAddress), ref fDefaultAddress, value); }
    }
    Address fDefaultAddress;


    [Aggregated, Association("PersonAddresses")]
    public XPCollection<Address> Addresses {
        get { return GetCollection<Address>(nameof(Addresses)); }
    }
}

Note

We did not mark the Address class as aggregated, because the aggregation is an attribute of an object association and is not a class in itself.

The following help topics demonstrate how to apply the Aggregated attribute to a collection property (the “Many” side of “One-to-Many” relationships):

See Also