The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

Relationships Between Objects

  • 9 min to read

There are three types of relationships between objects. The type of a relationship that is created depends upon how related objects are defined. To learn about a particular relationship type, click a corresponding link below.

 

This article demonstrates how to create one-to-many relationships and covers various aspects of their declaration in code. Click one of the following links to quickly navigate to the desired section of the article.

Introduction to One-to-Many Relationships

A one-to-many relationship is the most common type of relationship. In this relationship, a persistent object of type A can have many associated objects of type B, but an object of type B can have only one associated object of type A.

For example, orders can be associated with a specific customer by creating a relationship between the Orders property in the Customer object (the primary key) and the Customer property in the Order object (the foreign key). As a result, each customer can have multiple orders.

One_to_Many_Relat

The data model can be defined as shown below.


public class Customer : XPObject {
    public string ContactName {
        get { return fContactName; }
        set { SetPropertyValue(nameof(ContactName), ref fContactName, value); }
    }
    string fContactName;

    public string Phone {
        get { return fPhone; }
        set { SetPropertyValue(nameof(Phone), ref fPhone, value); }
    }
    string fPhone;

    // ...
    // Apply the Association attribute to mark the Orders property 
    // as the "many" end of the Customer-Orders association.
    [Association]
    public XPCollection<Order> Orders { 
        get { return GetCollection<Order>(nameof(Orders)); }
    }
}

public class Order : XPObject {
    public string ProductName {
        get { return fProductName; }
        set { SetPropertyValue(nameof(ProductName), ref fProductName, value); }
    }
    string fProductName;

    public DateTime OrderDate {
        get { return fOrderDate; }
        set { SetPropertyValue(nameof(OrderDate), ref fOrderDate, value); }
    }
    DateTime fOrderDate;

    // ...
    // Apply the Association attribute to mark the Customer property 
    // as the "one" end of the Customer-Orders association.
    [Association]
    public Customer Customer {
        get { return fCustomer; }
        set { SetPropertyValue(nameof(Customer), ref fCustomer, value); }
    }
    Customer fCustomer;

}
Important

Do not modify the XPCollection property declaration demonstrated above. Manipulating the collection or introducing any additional settings within the declaration may cause unpredictable behavior.

Here, the Association attribute (AssociationAttribute) is applied to the Customer.Orders and Order.Customer properties to identify both ends of the association. When one object relates to other objects, the "many" end of the association must be defined by an XPCollection or XPCollection<T> class.

Note that to be concise, we omitted all the getter/setter code that is required to track changes made to persistent properties. Otherwise, the Order.Customer property declaration code should look as follows.


public class Order : XPObject {
    // ...
    Customer fCustomer;
    [Association]
    public Customer Customer {
        get { return fCustomer; }
        set { SetPropertyValue(nameof(Customer), ref fCustomer, value); }
    }
}

To learn more about tracking changes in persistent objects, refer to Unit of Work.

Association Attribute Specifics

The AssociationAttribute takes the following arguments.

  • The type of an object being linked to.

    This argument can be omitted for the "one" end (Order.Customer) because it is already specified by the property type. In addition, you can omit this argument if an XPCollection<T> is used for the "many" end (Customer.Orders), because the associated object type is specified by T.

    
    public class Customer : XPObject {
        // ...
        [Association]
        public XPCollection<Order> Orders { 
            get { return GetCollection<Order>(nameof(Orders)); }
        }
    }
    
    public class Order : XPObject {
        // ...
        Customer fCustomer;
        [Association]
        public Customer Customer {
            get { return fCustomer; }
            set { SetPropertyValue(nameof(Customer), ref fCustomer, value); }
        }
    }
    
  • The name of the association.

    You can optionally provide a name to mark association ends for your convenience. To accomplish this, use the same name for both association ends.

    
    public class Customer : XPObject {
        // ...
        [Association("Customer-Orders")]
        public XPCollection<Order> Orders { 
            get { return GetCollection<Order>(nameof(Orders)); }
        }
    }
    
    public class Order : XPObject {
        // ...
        Customer fCustomer;
        [Association("Customer-Orders")]
        public Customer Customer {
            get { return fCustomer; }
            set { SetPropertyValue(nameof(Customer), ref fCustomer, value); }
        }
    }
    

    Normally, XPO automatically resolves one-to-many association ends by the types being linked - so you don't have to provide association names in most cases. If a type is used in more than one association and cannot be singled out for the one end, an association name should be provided. Consider the following example of a class used to store a folder hierarchy.

    
    public class Folder : XPObject {
        // ...
        Folder fParent;
        [Association]
        public Folder Parent {
            get { return fParent; }
            set { SetPropertyValue(nameof(Parent), ref fParent, value); }
        }
    
        [Association]
        public XPCollection<Folder> Children {
            get { return GetCollection<Folder>(nameof(Children)); }
        }
    }
    

    The Folder class is used in one association, so there is no need to name it. If we add another Folder-based association within the same class, we have to specify association names to help XPO resolve associations as shown below.

    
    public class Folder : XPObject {
        // ...
        Folder fParent;
        [Association("Parent-Children")]
        public Folder Parent {
            get { return fParent; }
            set { SetPropertyValue(nameof(Parent), ref fParent, value); }
        }
    
        [Association("Parent-Children")]
        public XPCollection<Folder> Children {
            get { return GetCollection<Folder>(nameof(Children)); }
        }
    
        Folder fLinkedTo;
        [Association("LinkedTo-Links")]
        public Folder LinkedTo {
            get { return fLinkedTo; }
            set { SetPropertyValue(nameof(LinkedTo), ref fLinkedTo, value); }
        }
    
        [Association("LinkedTo-Links")]
        public XPCollection<Folder> Links {
            get { return GetCollection<Folder>(nameof(Links)); }
        }
    }
    

    Here, we specified association names (Parent-Children and LinkedTo-Links) based on properties at the association ends. You can provide any unique names to identify associations.

Create and Persist Association Objects in Code

For more information about how to create and persist association objects in code, refer to the Add Persistent Objects Explicitily and Use the Back Master Reference Property sections.

Aggregate Relationships

To indicate an aggregate one-to-many relationship, add the Aggregated attribute (AggregatedAttribute) to the collection that specifies the association's "many" end. This indicates that the collection's objects are considered part of the associated object.


public class Customer : XPObject {
    // ...
    [Association, Aggregated]
    public XPCollection<Order> Orders {
        get { return GetCollection<Order>(nameof(Orders)); }
    }
}

Since it makes no sense to keep orders when the associated Customer object is deleted, the Aggregated attribute is applied to the Customer.Orders property. When a Customer object is deleted, the associated Order objects are deleted as well.

Note

If you have DevExpress WinForms components installed, you can try the functionality described here in the Object Relational Mapping | One to Many Relations section of the XPO Tutorials demo (C:\Users\Public\Documents\DevExpress Demos 19.2\Components\WinForms\Bin\XpoTutorials.exe).

See Also