Skip to main content

Introduction to Services

  • 3 minutes to read

This document briefly introduces the concept of services applied to the ASPxScheduler component object model.

Functionality enhancements of ASPxScheduler 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? A 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 core of ASPxScheduler and 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 by supplying the type of the service to the ASPxScheduler.GetService method, as illustrated below:

using DevExpress.Web.ASPxScheduler.Services;
// ...
IDateTimeNavigationService navigationService =
(IDateTimeNavigationService)(ASPxScheduler1.GetService( _
    typeof(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 the 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.

Previously, services could present challenges to a developer because of the service’s obscure nature - you had to possess 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 ASPxScheduler.Services property to access ASPxScheduler services, so their methods become easily discoverable. Hence, instead of the code above you can write:

ASPxScheduler1.Services.DateTimeNavigation.NavigateBackward();
ASPxScheduler1.Services.DateTimeNavigation.NavigateForward();
ASPxScheduler1.Services.DateTimeNavigation.GoToToday();

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

For deeper insight into service containers and related concepts, refer to the Lightweight Containers and Plugin Architectures: Dependency Injection and Dynamic Service Locators in .NET article by Daniel Cazzulino. He analyzes an article Inversion of Control Containers and the Dependency Injection pattern by Martin Fowler in light of .NET, and elaborates the idea.