Back to Devexpress

eXpress Persistent Objects (XPO)

windowsforms-9607-controls-and-libraries-scheduler-data-binding-data-sources-express-persistent-objects-xpo.md

latest9.0 KB
Original Source

eXpress Persistent Objects (XPO)

  • Sep 10, 2023
  • 4 minutes to read

eXpress Persistent Objects™

A SchedulerDataStorage can be bound to an XPCollection object provided by the eXpress Persistent Objects suite. For example, the following code demonstrates how to implement custom appointment and resource classes, which are the XPObject class descendants.

This code snippet illustrates how to implement the Appointment object using XPO as the data source. Note the special methods required to implement a multi-resource assignment (resource sharing).

View Example

csharp
// XP object
[DeferredDeletion(false)]
public class XPAppointment : XPObject {
    public XPAppointment(Session session) : base(session) { }

    public bool AllDay; // Appointment.AllDay

    [Size(SizeAttribute.Unlimited)] // !!! To set the Memo field type.
    public string Description; // Appointment.Description

    public DateTime Finish; // Appointment.End
    public int Label; // Appointment.Label
    public string Location; // Appointment.Location

    [Size(SizeAttribute.Unlimited)] // !!! To set the Memo field type.
    public string Recurrence; // Appointment.RecurrenceInfo

    [Size(SizeAttribute.Unlimited)] // !!! To set the Memo field type.
    public string Reminder; // Appointment.ReminderInfo

    public DateTime Created; // Appointment.Start
    public int Status; // Appointment.Status
    [Size(SizeAttribute.Unlimited)] // !!! To set the Memo field type.
    public string Subject; // Appointment.Subject
    public int AppointmentType; // Appointment.Type

    [Association()]
    public XPCollection<XPResource> Resources {
        get {
            return GetCollection<XPResource>("Resources");
        }
    }

    [NonPersistent()]
    public string ResourceIds {
        get {
            return GenerateResourceIdsString();
        }
        set {
            ResourceIdCollection resourceIds = GenerateResourceIdsString(value);
            Resources.SuspendChangedEvents();
            try {
                ClearResources();
                int count = resourceIds.Count;
                for (int i = 0; i < count; i++) {
                    XPResource resource = this.Session.GetObjectByKey<XPResource>(resourceIds[i]);
                    if (resource != null)
                        Resources.Add(resource);
                }
            }
            finally {
                Resources.ResumeChangedEvents();
            }
        }
    }

    void ClearResources() {
        int count = Resources.Count;
        while (count > 0) {
            Resources.Remove(Resources[0]);
            count--;
        }
    }
    ResourceIdCollection GenerateResourceIdsString(string xml) {
        ResourceIdCollection result = new ResourceIdCollection();
        if (String.IsNullOrEmpty(xml))
            return result;

        return AppointmentResourceIdCollectionXmlPersistenceHelper.ObjectFromXml(result, xml);
    }

    string GenerateResourceIdsString() {
        ResourceIdCollection resourceIds = new ResourceIdCollection();
        int count = Resources.Count;
        for (int i = 0; i < count; i++)
            resourceIds.Add(Resources[i].Oid);

        AppointmentResourceIdCollectionXmlPersistenceHelper helper = new AppointmentResourceIdCollectionXmlPersistenceHelper(resourceIds);
        return helper.ToXml();
    }
}
vb
' XP object
    <DeferredDeletion(false)>
    Public Class XPAppointment
        Inherits XPObject

        Public Sub New(ByVal session As Session)
            MyBase.New(session)
        End Sub

        Public AllDay As Boolean ' Appointment.AllDay

        <Size(SizeAttribute.Unlimited)>
        Public Description As String ' Appointment.Description - !!! To set the Memo field type.

        Public Finish As Date ' Appointment.End
        Public Label As Integer ' Appointment.Label
        Public Location As String ' Appointment.Location

        <Size(SizeAttribute.Unlimited)>
        Public Recurrence As String ' Appointment.RecurrenceInfo - !!! To set the Memo field type.

        <Size(SizeAttribute.Unlimited)>
        Public Reminder As String ' Appointment.ReminderInfo - !!! To set the Memo field type.

        Public Created As Date ' Appointment.Start
        Public Status As Integer ' Appointment.Status
        <Size(SizeAttribute.Unlimited)>
        Public Subject As String ' Appointment.Subject - !!! To set the Memo field type.
        Public AppointmentType As Integer ' Appointment.Type

        <Association()>
        Public ReadOnly Property Resources() As XPCollection(Of XPResource)
            Get
                Return GetCollection(Of XPResource)("Resources")
            End Get
        End Property

        <NonPersistent()>
        Public Property ResourceIds() As String
            Get
                Return GenerateResourceIdsString()
            End Get
            Set(ByVal value As String)
'INSTANT VB NOTE: The local variable resourceIds was renamed since Visual Basic will not allow local variables with the same name as their enclosing function or property:
                Dim resourceIds_Renamed As ResourceIdCollection = GenerateResourceIdsString(value)
                Resources.SuspendChangedEvents()
                Try
                    ClearResources()
                    Dim count As Integer = resourceIds_Renamed.Count
                    For i As Integer = 0 To count - 1
                        Dim resource As XPResource = Me.Session.GetObjectByKey(Of XPResource)(resourceIds_Renamed(i))
                        If resource IsNot Nothing Then
                            Resources.Add(resource)
                        End If
                    Next i
                Finally
                    Resources.ResumeChangedEvents()
                End Try
            End Set
        End Property

        Private Sub ClearResources()
            Dim count As Integer = Resources.Count
            Do While count > 0
                Resources.Remove(Resources(0))
                count -= 1
            Loop
        End Sub
        Private Function GenerateResourceIdsString(ByVal xml As String) As ResourceIdCollection
            Dim result As New ResourceIdCollection()
            If String.IsNullOrEmpty(xml) Then
                Return result
            End If

            Return AppointmentResourceIdCollectionXmlPersistenceHelper.ObjectFromXml(result, xml)
        End Function

        Private Function GenerateResourceIdsString() As String
'INSTANT VB NOTE: The variable resourceIds was renamed since Visual Basic does not handle local variables named the same as class members well:
            Dim resourceIds_Renamed As New ResourceIdCollection()
            Dim count As Integer = Resources.Count
            For i As Integer = 0 To count - 1
                resourceIds_Renamed.Add(Resources(i).Oid)
            Next i

            Dim helper As New AppointmentResourceIdCollectionXmlPersistenceHelper(resourceIds_Renamed)
            Return helper.ToXml()
        End Function
    End Class

This code snippet illustrates how the Resource object is implemented using XPO as the data source. Note the special methods required to implement color value conversion.

View Example

csharp
// XP object
public class XPResource : XPObject {
    public XPResource(Session session) : base(session) { }
    public int ResId;
    [Size(SizeAttribute.Unlimited)] // !!! To set the Memo field type.
    public string Name; // Resource.Caption
    public Int32 Color;
    public Image Image;

    [Association()]
    public XPCollection<XPAppointment> Appointments {
        get {
            return GetCollection<XPAppointment>("Appointments");
        }
    }
}
vb
' XP object
Public Class XPResource
    Inherits XPObject

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public ResId As Integer
    <Size(SizeAttribute.Unlimited)>
    Public Name As String ' Resource.Caption - !!! To set the Memo field type.
    Public Color As Int32
    Public Image As Image

    <Association()>
    Public ReadOnly Property Appointments() As XPCollection(Of XPAppointment)
        Get
            Return GetCollection(Of XPAppointment)("Appointments")
        End Get
    End Property
End Class

See Also

How to bind the XtraScheduler to XPO