Back to Devexpress

How to: Bind an ASPxScheduler to XPO via the Unit of Work

aspnet-4837-components-scheduler-examples-data-binding-how-to-bind-an-aspxscheduler-to-xpo-via-the-unit-of-work.md

latest10.2 KB
Original Source

How to: Bind an ASPxScheduler to XPO via the Unit of Work

  • Oct 06, 2023
  • 5 minutes to read

This topic provides guidelines on binding the ASPxScheduler control to an XPO (eXpress Persistent Objects) data source using the UnitOfWork session class.

Five steps are required to accomplish the task.

1. Prepare the Data Sources

Define two persistent object classes - one for appointments, the other - for resources. Name them Task and Employee. The code is shown below:

csharp
using System;
using DevExpress.Xpo;

public class Task : XPObject {
    public Task(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
    public string Subject; // Appointment.Subject
    public int AppointmentType; // Appointment.Type    
    public Employee Employee;
}

public class Employee : XPObject {
    public Employee(Session session) : base(session) { }

    [Size(SizeAttribute.Unlimited)] // !! To set the Memo field type.
    public string Name; // Resource.Caption    
}
vb
Imports Microsoft.VisualBasic
Imports System
Imports DevExpress.Xpo

Public Class Task
    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

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

    <Size(SizeAttribute.Unlimited)> _
    Public Recurrence As String ' Appointment.RecurrenceInfo

    <Size(SizeAttribute.Unlimited)> _
    Public Reminder As String ' Appointment.ReminderInfo

    Public Created As DateTime ' Appointment.Start
    Public Status As Integer ' Appointment.Status
    Public Subject As String ' Appointment.Subject
    Public AppointmentType As Integer ' Appointment.Type
    Public Employee As Employee
End Class

Public Class Employee
    Inherits XPObject
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub

    <Size(SizeAttribute.Unlimited)> _
    Public Name As String ' Resource.Caption
End Class

Add two XPO data sources to your project by dragging them from the toolbox onto a form. Specify the IDs and XPObjectType.TypeName for these controls - the first will be the appointmentDataSource with the Task type assigned, the second will be the resourceDataSource with the Employee type assigned.

You may also find the Get Started with XPO document helpful.

2. Specify the Mappings

Since the data source assignment is done at runtime via code, the mappings should be specified manually. Review the code below which illustrates this:

csharp
ASPxScheduler1.Storage.BeginUpdate();
try {
    ASPxAppointmentMappingInfo aptMappings = 
        ASPxScheduler1.Storage.Appointments.Mappings;
    aptMappings.AllDay = "AllDay";
    aptMappings.Description = "Description";
    aptMappings.End = "Finish";
    aptMappings.Label = "Label";
    aptMappings.Location = "Location";
    aptMappings.RecurrenceInfo = "Recurrence";
    aptMappings.ReminderInfo = "Reminder";
    aptMappings.ResourceId = "Employee!Key";            
    aptMappings.Start = "Created";
    aptMappings.Status = "Status";
    aptMappings.Subject = "Subject";    
    aptMappings.Type = "AppointmentType";
    aptMappings.AppointmentId = "Oid";                        

    ASPxResourceMappingInfo resourceMappings = 
        ASPxScheduler1.Storage.Resources.Mappings;
    resourceMappings.Caption = "Name";
    resourceMappings.ResourceId = "Oid";            
}
finally {
    ASPxScheduler1.Storage.EndUpdate();
}
vb
ASPxScheduler1.Storage.BeginUpdate()
Try
    Dim aptMappings As ASPxAppointmentMappingInfo = 
        ASPxScheduler1.Storage.Appointments.Mappings
    aptMappings.AllDay = "AllDay"
    aptMappings.Description = "Description"
    aptMappings.End = "Finish"
    aptMappings.Label = "Label"
    aptMappings.Location = "Location"
    aptMappings.RecurrenceInfo = "Recurrence"
    aptMappings.ReminderInfo = "Reminder"
    aptMappings.ResourceId = "Employee!Key"
    aptMappings.Start = "Created"
    aptMappings.Status = "Status"
    aptMappings.Subject = "Subject"
    aptMappings.Type = "AppointmentType"
    aptMappings.AppointmentId = "Oid"

    Dim resourceMappings As ASPxResourceMappingInfo = 
        ASPxScheduler1.Storage.Resources.Mappings
    resourceMappings.Caption = "Name"
    resourceMappings.ResourceId = "Oid"
Finally
    ASPxScheduler1.Storage.EndUpdate()
End Try

3. Post Data Back to the Database

Handle the ASPxSchedulerDataWebControlBase.AppointmentsInserted, ASPxSchedulerDataWebControlBase.AppointmentsChanged and ASPxSchedulerDataWebControlBase.AppointmentsDeleted events to commit changes in the data source. Call the UnitOfWork.CommitChanges method of the Unit of Work object for this purpose.

4. Provide the Correct Appointment Identifier

It is necessary to obtain a correct appointment identifier to track the newly created appointment. To accomplish this, a new XPORowInsertionProvider class is defined. It is aimed at retrieving the key identifier value of the last inserted appointment, and correct the corresponding value of the appointment in the storage. It handles the Session.ObjectSaved event of the Session object and the ASPxSchedulerDataWebControlBase.AppointmentsInserted event of the ASPxScheduler. The code for this class is presented below:

csharp
public class XPORowInsertionProvider {
    object lastInsertedAppointmentId;
    ASPxScheduler control;
    Session session;
    public void ProvideRowInsertion(ASPxScheduler control, 
XpoDataSource dataSource, Session session) {
        this.control = control;
        this.session = session;            
        session.ObjectSaved += 
            new ObjectManipulationEventHandler(Session_ObjectSaved);
        control.AppointmentsInserted += 
            new PersistentObjectsEventHandler(ControlOnAppointmentsInserted);                        
    }
    void Session_ObjectSaved(object sender, 
                    ObjectManipulationEventArgs e) {
        this.lastInsertedAppointmentId = ((XPObject)e.Object).Oid;
    }       
    void ControlOnAppointmentsInserted(object sender, 
            PersistentObjectsEventArgs e) {
        // Autoincremented primary key case
        int count = e.Objects.Count;
        System.Diagnostics.Debug.Assert(count == 1);
        Appointment apt = (Appointment)e.Objects[0];
        ASPxSchedulerStorage storage = (ASPxSchedulerStorage)sender;
        storage.SetAppointmentId(apt, lastInsertedAppointmentId);            
    }
}
vb
Public Class XPORowInsertionProvider
    Private lastInsertedAppointmentId As Object
    Private control As ASPxScheduler
    Private session As Session
    Public Sub ProvideRowInsertion(ByVal control As ASPxScheduler, _
            ByVal dataSource As XpoDataSource, ByVal session As Session)
        Me.control = control
        Me.session = session
        AddHandler session.ObjectSaved, AddressOf Session_ObjectSaved
        AddHandler control.AppointmentsInserted, _
            AddressOf ControlOnAppointmentsInserted
    End Sub
    Private Sub Session_ObjectSaved(ByVal sender As Object, _
            ByVal e As ObjectManipulationEventArgs)
        Me.lastInsertedAppointmentId = (CType(e.Object, XPObject)).Oid
    End Sub
    Private Sub ControlOnAppointmentsInserted(ByVal sender As Object, _
            ByVal e As PersistentObjectsEventArgs)
        ' Autoincremented primary key case
        Dim count As Integer = e.Objects.Count
        System.Diagnostics.Debug.Assert(count = 1)
        Dim apt As Appointment = CType(e.Objects(0), Appointment)
        Dim storage As ASPxSchedulerStorage = _
            CType(sender, ASPxSchedulerStorage)
        storage.SetAppointmentId(apt, lastInsertedAppointmentId)
    End Sub
End Class

5. Attach the Data Sources

Now you should set the ASPxSchedulerDataWebControlBase.AppointmentDataSource and ASPxSchedulerDataWebControlBase.ResourceDataSource values to the corresponding XPO data sources ( appointmentDataSource and resourceDataSource ) and call the ASPxWebControl.DataBind method to perform binding.

After completing these steps, run the project and observe the result - a web site with the ASPxScheduler control bound to an XPO data source. Note that you can connect XPO to a database server as described in the Connecting XPO to a Database Server (ASP.NET) article. By default, your data persists in the .mdb file of MS Access format within the App_Data folder.