Back to Devexpress

How to: Create an Aggregated Object

xpo-2048-examples-how-to-create-an-aggregated-object.md

latest4.4 KB
Original Source

How to: Create an Aggregated Object

  • Apr 29, 2022
  • 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.

csharp
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)); }
    }
}
vb
Public Class Address
    Inherits XPObject
    ' For brevity, we omitted the declaration of constructors
    ' and fields corresponding to persistent properties.
    ' ...
    Public Property City() As String
        Get
            Return fCity
        End Get
        Set(ByVal value as String)
            SetPropertyValue(NameOf(City), fCity, value)
        End Set
    End Property
    Private fCity As String

    Public Property Street() As String
        Get
            Return fStreet
        End Get
        Set(ByVal value as String)
            SetPropertyValue(NameOf(Street), fStreet, value)
        End Set
    End Property
    Private fStreet As String

    <Association("PersonAddresses")> _
    Public Property Owner() As Person
        Get
            Return fOwner
        End Get
        Set(ByVal value as Person)
            SetPropertyValue(NameOf(Owner), fOwner, value)
        End Set
    End Property
    Private fOwner As Person

End Class

Public Class Person
vInherits XPObject
    ' For brevity, we omitted the declaration of constructors
    ' and fields corresponding to persistent properties.
    ' ...
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        DefaultAddress = New Address(Session)
    End Sub

    <Aggregated> _
    Public Property DefaultAddress() As Address
        Get
            Return fDefaultAddress
        End Get
        Set(ByVal value as Address)
            SetPropertyValue(NameOf(DefaultAddress), fDefaultAddress, value)
        End Set
    End Property
    Private fDefaultAddress As Address

    <Aggregated, Association("PersonAddresses")> _
    Public ReadOnly Property Addresses() As XPCollection(Of Address)
        Get
            Return GetCollection(Of Address)(NameOf(Addresses))
        End Get
    End Property
End Class

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

AggregatedAttribute

AssociationAttribute