Xafari Context Service. Context Creation

A context object is created when the service is registered in the IServiceSpace object. In the code snippet below, a context object is created when the Numerators() extension method is called.

  • c#
  • VB

public void IServiceSpace()
{
  using (var objectSpace = this.Application.CreateObjectSpace())
  {
    var context = objectSpace.Xafari().Numerators();
    var context2 = objectSpace.Xafari().Numerators();
  }
}

Public Sub IServiceSpace()
  Using objectSpace = Me.Application.CreateObjectSpace()
    Dim context = objectSpace.Xafari().Numerators()
    Dim context2 = objectSpace.Xafari().Numerators()
  End Using
End Sub

Note

The Numeratiors() extension method makes it easier to access to the context of the Numerators service. In fact, this method refers to the IServiceSpace methods though it is not explicitly shown in the example. It is recommended to implement the same extension methods for your own services to keep the code simple.

Only one context object of a particular service can be registered for a single IServiceSpace object. In the previous example, context and context2 variables refer to the same context object.

There is the protected virtual method Xafari.BC.Services.XafariServiceBase<TService>.ServiceSpaceContextBase.InitializeCore(). This method is called during the service registration, it performs the basic initialization of the context object. At the time of the call, there are already established references to the IXafariService and IServiceSpace objects, DataAccessor object is also available. The example below shows how the InitializeCore() method is implemented in the code of the AppSettings service.

  • c#
  • VB

protected override void InitializeCore()
{
  base.InitializeCore();
  this.RootSlice = this.CreateSlice(this.Service._rootSliceType);
  this.CurrentSlice = this.Service.CurrentSlice;
}

Protected Overrides Sub InitializeCore()
  MyBase.InitializeCore()
  Me.RootSlice = Me.CreateSlice(Me.Service._rootSliceType)
  Me.CurrentSlice = Me.Service.CurrentSlice
End Sub

To create context object, override the XafariServiceBase.CreateServiceContexCore() method of the context service, this method is supposed to be implemented in each service.

  • c#
  • VB

protected override ServiceSpaceContextBase CreateServiceContextCore(IServiceSpace serviceSpace)
{
  return new NumeratorManager.ServiceSpaceContext();
}

Protected Overrides Function CreateServiceContextCore(ByVal serviceSpace As IServiceSpace) As ServiceSpaceContextBase
  Return New NumeratorManager.ServiceSpaceContext()
End Function

If a context object was successfully created in a service, the XafariServiceBase.ServiceSpaceContextCreated event is generated.