Skip to main content

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

  • 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:

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    
}

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:

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();
}

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:

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);            
    }
}

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.