Back to Devexpress

ManyToManyAliasAttribute Class

xpo-devexpress-dot-xpo-f85ae266.md

latest8.9 KB
Original Source

ManyToManyAliasAttribute Class

Applied to properties. Binds parts of the many-to-many relationship defined with an intermediate object.

Namespace : DevExpress.Xpo

Assembly : DevExpress.Xpo.v25.2.dll

NuGet Package : DevExpress.Xpo

Declaration

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

Remarks

The many-to-many relationship between two persistent objects with an intermediate object consists of two one-to-many relationships between “many” parts and the intermediate object. This technique allows you to create a many-to-many relationship in a legacy database or add custom properties to an intermediate object.

The following example demonstrates how to use this attribute:

cs
using DevExpress.Xpo;
using System.Collections.Generic;
using System.ComponentModel;
// ...
// The Location class, which contains its name and information 
// about the departments at the location.
public class Location : XPObject {
    public Location(Session session) : base(session) { }
    public string Name {
        get { return fName; }
        set { SetPropertyValue(nameof(Name), ref fName, value); }
    }
    string fName;
    // Apply the Association attribute to mark the LocationDepartmentLinks property 
    // as the "many" end of the one-to-many association between Location and an intermediate object.
    [Association, Browsable(false)]
    public IList<LocationDepartmentLink> LocationDepartmentLinks {
        get {
            return GetList<LocationDepartmentLink>(nameof(LocationDepartmentLinks));
        }
    }
    // Apply the ManyToManyAlias attribute to mark the Departments property 
    // as the "many" end of the many-to-many association between Location and Department.
    [ManyToManyAlias(nameof(LocationDepartmentLinks), nameof(LocationDepartmentLink.LinkDepartment))]
    public IList<Department> Departments {
        get {
            return GetList<Department>(nameof(Departments));
        }
    }
}
// The Department class that contains its name 
// and references all the locations of the department's offices.
public class Department : XPObject {
    public Department(Session session) : base(session) { }
    public string Name {
        get { return fName; }
        set { SetPropertyValue(nameof(Name), ref fName, value); }
    }
    string fName;
    // Apply the Association attribute to mark the LocationDepartmentLinks property 
    // as the "many" end of the one-to-many association between Department and an intermediate object.
    [Association, Browsable(false)]
    public IList<LocationDepartmentLink> LocationDepartmentLinks {
        get {
            return GetList<LocationDepartmentLink>(nameof(LocationDepartmentLinks));
        }
    }
    // Apply the ManyToManyAlias attribute to mark the Locations property 
    // as the "many" end of the many-to-many association between Location and Department.
    [ManyToManyAlias(nameof(LocationDepartmentLinks), nameof(LocationDepartmentLink.LinkLocation))]
    public IList<Location> Locations {
        get {
            return GetList<Location>(nameof(Locations));
        }
    }
}
// The intermediate class that has links to Location and Department.
public class LocationDepartmentLink : XPObject {
    public LocationDepartmentLink(Session session) : base(session) { }
    private Location _LinkLocation;
    // Apply the Association attribute to mark the LinkLocation property 
    // as the "one" end of the one-to-many association between Location and an intermediate object.
    [Association]
    public Location LinkLocation {
        get { return _LinkLocation; }
        set { SetPropertyValue(nameof(LinkLocation), ref _LinkLocation, value); }
    }
    private Department _LinkDepartment;
    // Apply the Association attribute to mark the LinkDepartment property 
    // as the "one" end of the one-to-many association between Department and an intermediate object.
    [Association]
    public Department LinkDepartment {
        get { return _LinkDepartment; }
        set { SetPropertyValue(nameof(LinkDepartment), ref _LinkDepartment, value); }
    }
}
vb
Imports DevExpress.Xpo
Imports System.Collections.Generic
Imports System.ComponentModel
' ...
' The Location class, which contains its name and information 
' about the departments at the location.
Public Class Location
    Inherits XPObject
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    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
    ' Apply the Association attribute to mark the LocationDepartmentLinks property 
    ' as the "many" end of the one-to-many association between Location and an intermediate object.
    <Association, Browsable(False)>
    Public ReadOnly Property LocationDepartmentLinks() As IList(Of LocationDepartmentLink)
        Get
            Return GetList(Of LocationDepartmentLink)(NameOf(LocationDepartmentLinks))
        End Get
    End Property
    ' Apply the ManyToManyAlias attribute to mark the Departments property 
    ' as the "many" end of the many-to-many association between Location and Department.
    <ManyToManyAlias(nameof(LocationDepartmentLinks), nameof(LocationDepartmentLink.LinkDepartment))>
    Public ReadOnly Property Departments() As IList(Of Department)
        Get
            Return GetList(Of Department)(NameOf(Departments))
        End Get
    End Property
End Class
' The Department class, which contains its name 
' and references all the locations of the department's offices.
Public Class Department
    Inherits XPObject
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    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
    ' Apply the Association attribute to mark the LocationDepartmentLinks property 
    ' as the "many" end of the one-to-many association between Department and an intermediate object.
    <Association, Browsable(False)>
    Public ReadOnly Property LocationDepartmentLinks() As IList(Of LocationDepartmentLink)
        Get
            Return GetList(Of LocationDepartmentLink)(NameOf(LocationDepartmentLinks))
        End Get
    End Property
    ' Apply the ManyToManyAlias attribute to mark the Locations property 
    ' as the "many" end of the many-to-many association between Location and Department.
    <ManyToManyAlias(nameof(LocationDepartmentLinks), nameof(LocationDepartmentLink.LinkLocation))>
    Public ReadOnly Property Locations() As IList(Of Location)
        Get
            Return GetList(Of Location)(NameOf(Locations))
        End Get
    End Property
End Class
' The intermediate class that has links to Location and Department.
Public Class LocationDepartmentLink
    Inherits XPObject
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Private _LinkLocation As Location
    ' Apply the Association attribute to mark the LinkLocation property 
    ' as the "one" end of the one-to-many association between Location and an intermediate object.
    <Association>
    Public Property LinkLocation() As Location
        Get
            Return _LinkLocation
        End Get
        Set(ByVal value As Location)
            SetPropertyValue(NameOf(LinkLocation), _LinkLocation, value)
        End Set
    End Property
    Private _LinkDepartment As Department
    ' Apply the Association attribute to mark the LinkDepartment property 
    ' as the "one" end of the one-to-many association between Department and an intermediate object.
    <Association>
    Public Property LinkDepartment() As Department
        Get
            Return _LinkDepartment
        End Get
        Set(ByVal value As Department)
            SetPropertyValue(NameOf(LinkDepartment), _LinkDepartment, value)
        End Set
    End Property
End Class

Inheritance

Object Attribute ManyToManyAliasAttribute

See Also

ManyToManyAliasAttribute Members

DevExpress.Xpo Namespace