Back to Devexpress

Work with Structures

xpo-2044-create-a-data-model-work-with-structures.md

latest3.7 KB
Original Source

Work with Structures

  • May 18, 2022
  • 3 minutes to read

This example shows how to store and retrieve properties that are represented by structures and how to build query criteria based on them.

The Shape.Position property is represented by a Point struct type.

csharp
public struct Point {
    [Persistent("Abscissa")]
    public int X;
    public int Y;
}
public class Shape: XPObject {
    public Shape(Session session) : base(session) { }
    public string Name {
        get { return fName; }
        set { SetPropertyValue(nameof(Name), ref fName, value); }
    }
    string fName = "";

    [Persistent("Location")]
    public Point Position {
        get { return fPosition; }
        set { SetPropertyValue(nameof(Position), ref fPosition, value); }
    }
    Point fPosition;

}
vb
Public Structure Point
    <Persistent("Abscissa")> _
    Public X As Integer
    Public Y As Integer
End Structure 

Public Class Shape
    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 = ""

    <Persistent("Location")> _
    Public Property Position() As Point
        Get
            Return fPosition
        End Get
        Set(ByVal value as Point)
            SetPropertyValue(NameOf(Position), fPosition, value)
        End Set
    End Property
    Private fPosition As Point

End Class

By default, the property of a struct type (in our case this is the Position property) cannot be stored to the data store unless it is marked with the PersistentAttribute. In this tutorial, the ‘X’ struct member will be stored in the ‘Abscissa’ database column (its name is specified in the PersistentAttribute). The ‘Y’ struct member will be stored in a database column called ‘LocationY’. This is the default name for this struct member. This name is automatically constructed from the name of a struct type property followed by the struct’s member name.

Struct’s members can be accessed as shown below.

csharp
session1 = new DevExpress.Xpo.Session();
// ...

Shape shape1 = new Shape(session1);
shape1.Name = "Ellipse";
shape1.Position = new Point() { X = 1, Y = 2 };
shape1.Save();
vb
session1 = New DevExpress.Xpo.Session()
' ...

Dim shape1 As New Shape(session1)
shape1.Name = "Ellipse"
shape1.Position = New Point() With {.X = 1, .Y = 2}
shape1.Save()

Struct members can also be used to construct query criteria for the collection. The following code demonstrates how to query the data store for Shapes with Position.X equal to 1.

csharp
XPCollection<Shape> xpCollection1 = 
    new XPCollection<Shape>(session1, CriteriaOperator.Parse("Position.X = 1"));
vb
Dim xpCollection1 As 
_New XPCollection(Of Shape)(session1, CriteriaOperator.Parse("Position.X = 1"))

Note

We do not recommend using composite or compound keys for new databases. Composite keys may impose limitations on the default functionality. You may wish to refer to the following help topic to learn more: How to map persistent objects to database tables with composite keys.