Introduction to Services in XtraScheduler

  • 3 minutes to read

This document briefly introduces the concept of services and containers applied to the XtraScheduler component object model.

Functionality enhancements of XtraScheduler may require intense code modifications, and result in properties and methods becoming obsolete. To minimize the chance of breaking changes in future versions, we decided to separate the actual implementation of a feature in code from the implementation of methods used to access this feature. We can manage this task via extensive use of interfaces. It was also necessary to find a way to break apart dependencies in the application, and achieve looser coupling between its parts. This approach usually results in greater extensibility.

To achieve this, we decided to follow a concept of services and service containers, implemented in .NET framework.

What is a service? Service is a class with a known interface, addressable by its type. A service can be obtained from a service provider, instantiated and stored within a service container. The XtraScheduler incorporates classes and interfaces that inherit from base interfaces, contained within the System.ComponentModel namespace of .NET - first of all, IServiceProvider and IServiceContainer. Since the Scheduler implements the IServiceProviderinterface, you can obtain a service using the SchedulerControl.GetService or the SchedulerControl.GetService<T> method, as illustrated below:


using DevExpress.XtraScheduler.Services;
// ...
IDateTimeNavigationService navigationService = 
 schedulerControl1.GetService<IDateTimeNavigationService>();
if (navigationService != null)
{
    // Use methods of service:
    navigationService.NavigateBackward();
    navigationService.NavigateForward();
    navigationService.GoToToday();
    // ... and so on.
}

This code tries to get a reference to a service which is responsible for moving backwards and forwards to different points in time within the scheduler's coordinate system. An implicit cast is required because GetService method returns a Object.

NOTE

Note that the service availability is not guaranteed. Whenever you query a service type by calling the GetService method, you must always check if GetService returned a valid object. Or, you may use the generic SchedulerControl.GetService<T> method.

Previously services could present challenges to a developer because of the service's obscure nature - you had to possess a prior knowledge of the service type and its methods to use it, and there was no Intellisense to guide you. Nowadays you can use the dot(.) operator with the SchedulerControl.Services property to access XtraScheduler services, so their methods become easily discoverable.

You can add your own services to the service container via the SchedulerControl.AddService method, and use them throughout your application. Do not forget to remove them using a SchedulerControl.RemoveService method for the proper disposal of a container when it is no longer needed.

IAppointmentFormatStringService oldService = scheduler.GetService<IAppointmentFormatStringService>();
if (oldService != null)
{
    MyAppointmentFormatStringService newService = new MyAppointmentFormatStringService(oldService);
    scheduler.RemoveService(typeof(IAppointmentFormatStringService));
    scheduler.AddService(typeof(IAppointmentFormatStringService), newService);
}
scheduler.ActiveView.LayoutChanged();