Back to Devexpress

AggregatedAttribute Class

xpo-devexpress-dot-xpo-e5c5ae81.md

latest7.8 KB
Original Source

AggregatedAttribute Class

Indicates that persistent objects referenced by the target property are aggregated.

Namespace : DevExpress.Xpo

Assembly : DevExpress.Xpo.v25.2.dll

NuGet Package : DevExpress.Xpo

Declaration

csharp
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = true)]
public sealed class AggregatedAttribute :
    Attribute
vb
<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field, Inherited:=True)>
Public NotInheritable Class AggregatedAttribute
    Inherits Attribute

Remarks

An aggregated object is a dependent object that is linked to its owner and cannot exist without its owner. When an owner is deleted, any aggregated objects it owns are deleted as well. All aggregated objects are saved automatically when the owner object is saved.

You can apply Xpo.AggregatedAttribute to an XPO reference property or to a collection property that forms a relationship. If a collection property is not marked with the AssociationAttribute, then it is not part of a relationship. Xpo.AggregatedAttribute has no effect on such a property.

Note

In XAF applications, use this attribute for XPO objects only. If you use Entity Framework or non-persistent objects, and need to mark properties as aggregated, apply the DC.AggregatedAttribute.

Example

The following example demonstrates how to implement a one-to-many relationship between persistent objects, so that child objects are considered a part of their owner (when an owner is deleted, its aggregated objects will be automatically deleted; similarly, when an object is saved its aggregated objects will also be saved).

A one-to-many relationship is set up using the AssociationAttribute attribute. The AggregatedAttribute attribute is used to implement aggregation.

In this example, a Person object can have multiple addresses that are stored in the Person.Addresses collection. Each address in this collection is represented by an Address persistent class. The Person.Addresses property is marked with the AssociationAttribute and AggregatedAttribute attributes.

If you run the code below as is, the console output will be:

7654 Amsterdam Ave, New York, NY 555 Harbor Way, Santa Barbara, CA

If you remove the Aggregated attribute, the output will change to:

7654 Amsterdam Ave, New York, NY 7654 Amsterdam Ave, New York, NY

csharp
using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.Xpo;

namespace ConsoleApplication1 {
    public class Person : XPObject {
        public string Name {
            get { return fName; }
            set { SetPropertyValue(nameof(Name), ref fName, value); }
        }
        string fName;

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

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

        public string AddressInfo {
            get { return fAddressInfo; }
            set { SetPropertyValue(nameof(AddressInfo), ref fAddressInfo, value); }
        }
        string fAddressInfo;

    }

    class Program {
        static void Main(string[] args) {
            // Create a new instance of the Person class
            Person person = new Person() { Name = "Andrew Smith" };
            // Add an address for the person.
            Address address = new Address() { AddressInfo = "7654 Amsterdam Ave, New York, NY" };
            person.Addresses.Add(address);

            // Save the created Person object. The contents of the Addresses collection will be saved as well.
            person.Save();
            // Reload the object to verify if changes were saved.
            address.Reload();
            Console.WriteLine(address.AddressInfo);

            address.AddressInfo = "555 Harbor Way, Santa Barbara, CA";
            person.Save();
            // Reload the object to verify if changes were saved.
            address.Reload();
            Console.WriteLine(address.AddressInfo);

            Console.ReadKey();
        }
    }
}
vb
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports DevExpress.Xpo

Namespace ConsoleApplication1
    Public Class Person
        Inherits XPObject

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

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

    Public Class Address
        Inherits XPObject

        <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

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

    End Class

    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            ' Create a new instance of the Person class
            Dim person As New Person() With {.Name = "Andrew Smith"}
            ' Add an address for the person.
            Dim address As New Address() With {.AddressInfo = "7654 Amsterdam Ave, New York, NY"}
            person.Addresses.Add(address)

            ' Save the created Person object. The contents of the Addresses collection will be saved as well.
            person.Save()
            ' Reload the object to verify if changes were saved.
            address.Reload()
            Console.WriteLine(address.AddressInfo)

            address.AddressInfo = "555 Harbor Way, Santa Barbara, CA"
            person.Save()
            ' Reload the object to verify if changes were saved.
            address.Reload()
            Console.WriteLine(address.AddressInfo)

            Console.ReadKey()
        End Sub
    End Class
End Namespace

Inheritance

Object Attribute AggregatedAttribute

See Also

AggregatedAttribute Members

Built-In Attributes

How to: Create an Aggregated Object

DevExpress.Xpo Namespace