Xafari Service. Context Service Sample

This topic demonstrates example of a context service that exposes ServiceMethod() service method and an extension for this method.

Note

The complete sample project is available at CustomXafariServices.

To add new Xafari service to the project, use a special Visual Studio template.

xafari_service_9

The code snippet below defines CustomService class:

  • c#
  • VB

public class CustomService : XafariServiceBase<CustomService>
{
  public class ServiceContext : ServiceSpaceContextBase
  {
    protected override void InitializeCore()
    {
      base.InitializeCore();
      // Add here specific initialize code
    }
    protected override void DisposeCore()
    {
      // Add here specific dispose code
      base.DisposeCore();
    }
    protected override void Rollback()
    {// Add here code to implement service rollback operation
    }
    protected override void Reload()
    {// Add here code to implement service reload operation
    }
    protected override void CommitChanges()
    {// Add here code to implement service commit changes operation
    }
    public void ServiceMethod()
    {// Add here code to implement any service method.
    }
  }
  protected override ServiceSpaceContextBase CreateServiceContextCore(IServiceSpace serviceSpace)
  {
    return new CustomService.ServiceContext();
  }
  public override void Setup(XafApplication application)
  {
    base.Setup(application);
  }
  protected override void InitializeCore()
  {
    base.InitializeCore();
    // Add here specific initialize code
  }
  protected override void ResetCore()
  {
    // Add here specific initialize code
    base.ResetCore();
  }
  protected override void OnActivated()
  {// Add here specific code on activate service
  }
  protected override void OnDeactivated()
  {// Add here specific code on deactivate service
  }
}
public static class CustomServiceExtensions
{
  public static CustomService.ServiceContext CustomService(this IObjectSpaceHelper objectSpaceHelper)
  {
    return objectSpaceHelper.ServiceSpace.FindServiceContext<CustomService.ServiceContext>(CustomService.Instance);
  }
  public static CustomService.ServiceContext CustomService(this UnitOfWorkHelper unitOfWorkHelper)
  {
    return unitOfWorkHelper.ServiceSpace.FindServiceContext<CustomService.ServiceContext>(CustomService.Instance);
  }
}

Public Class CustomService
  Inherits XafariServiceBase(Of CustomService)
  Public Class ServiceContext
    Inherits ServiceSpaceContextBase
    Protected Overrides Sub InitializeCore()
      MyBase.InitializeCore()
      ' Add here specific initialize code
    End Sub
    Protected Overrides Sub DisposeCore()
      ' Add here specific dispose code
      MyBase.DisposeCore()
    End Sub
    Protected Overrides Sub Rollback()
      ' Add here code to implement service rollback operation
    End Sub
    Protected Overrides Sub Reload()
      ' Add here code to implement service reload operation
    End Sub
    Protected Overrides Sub CommitChanges()
      ' Add here code to implement service commit changes operation
    End Sub
    Public Sub ServiceMethod()
      ' Add here code to implement any service method.
    End Sub
  End Class
  Protected Overrides Function CreateServiceContextCore(ByVal serviceSpace As IServiceSpace) As ServiceSpaceContextBase
    Return New CustomService.ServiceContext()
  End Function
  Public Overrides Sub Setup(ByVal application As XafApplication)
    MyBase.Setup(application)
  End Sub
  Protected Overrides Sub InitializeCore()
    MyBase.InitializeCore()
    ' Add here specific initialize code
  End Sub
  Protected Overrides Sub ResetCore()
    ' Add here specific initialize code
    MyBase.ResetCore()
  End Sub
  Protected Overrides Sub OnActivated()
    ' Add here specific code on activate service
  End Sub
  Protected Overrides Sub OnDeactivated()
    ' Add here specific code on deactivate service
  End Sub
End Class
Public Module CustomServiceExtensions
  <System.Runtime.CompilerServices.Extension> _
Public Function CustomService(ByVal objectSpaceHelper As IObjectSpaceHelper) As CustomService.ServiceContext
    Return objectSpaceHelper.ServiceSpace.FindServiceContext(Of CustomService.ServiceContext)(CustomService.Instance)
  End Function
  <System.Runtime.CompilerServices.Extension> _
Public Function CustomService(ByVal unitOfWorkHelper As UnitOfWorkHelper) As CustomService.ServiceContext
    Return unitOfWorkHelper.ServiceSpace.FindServiceContext(Of CustomService.ServiceContext)(CustomService.Instance)
  End Function
End Module

Important

When creating a new service, it is recommended to implement extension class for the IObjectSpaceHelper in accordance with the template above. It can simplify the usage of the service in the code a lot.

The example of calling the ServiceMethod() through the extension is shown below:

  • c#
  • VB

private void simpleAction1_Execute(object sender, SimpleActionExecuteEventArgs e)
{
  this.ObjectSpace.Xafari().CustomService().ServiceMethod();
  this.ObjectSpace.CommitChanges();
}

Private Sub simpleAction1_Execute(ByVal sender As Object, ByVal e As SimpleActionExecuteEventArgs)
  Me.ObjectSpace.Xafari().CustomService().ServiceMethod()
  Me.ObjectSpace.CommitChanges()
End Sub

That is the sequence of operations during the call:

  1. The IServiceSpace object is created for the ObjectSpace object.
  2. CustomService is registered in the newly created IServiceSpace object; then the CustomService.ServiceContext object is created.
  3. ServiceMethod() is called for the current context object.
  4. The CommitChanges() method is called to approve all actions executed in the service method.

Important

During the implementation of a context service, all interaction with persistent data should be done via the DataAccessor object. This object is provided by the appropriate DataAccessor property. Such approach is required to stabilize and to unify the work of both IObjectSpace and UnitOfWork services.