AggregatedAttribute Class
Indicates that persistent objects referenced by the target property are aggregated.
Namespace: DevExpress.Xpo
Assembly: DevExpress.Xpo.v18.2.dll
Declaration
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = true)]
public sealed class AggregatedAttribute :
Attribute
Remarks
An aggregated object is considered a part of the owning object and cannot exist separately. When an owner is deleted, any aggregated objects owned by it are deleted as well. Saving the owner object automatically saves all aggregated objects.
The AggregatedAttribute can be applied to either a reference property or to a collection property that participates in a relationship. If you apply the AggregatedAttribute to a collection property that doesn’t participate in a relationship, i.e. it doesn’t have the AssociationAttribute applied, the AggregatedAttribute will have no effect.
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
using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.Xpo;
namespace ConsoleApplication1 {
public class Person : XPObject {
public string Name;
[Association("PersonAddresses"), Aggregated]
public XPCollection<Address> Addresses {
get { return GetCollection<Address>("Addresses"); }
}
}
public class Address : XPObject {
[Association("PersonAddresses")]
public Person Owner;
public string AddressInfo;
}
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();
}
}
}