Service Spaces Manager

Service Spaces Manager is a special Xafari Service intended to manage the service spaces life cycle and maintain the context service activity. The diagram below illustrates the relations between Service Spaces Manager and other components.

 

Service Spaces Manager supports two use cases:

  • Creating IServiceSpace for IObjectSpace or UnitOfWork.
  • Performing synchronizations between IObjectSpace or UnitOfWork objects and the service space.

Service Spaces Creation

Service Spaces Manager is a factory of IServiceSpace objects. It ensures that these objects satisfy the requirement: there can be only one service space object for each owner IObjectSpace or UnitOfWork object.

Service space objects are created by the CreateServiceSpace() method. However, it is not recommended to refer directly to this method. More rational way is to use extension methods for IObjectSpace or UnitOfWork objects. The code snippet below demonstrates how to create service space objects:

  • c#
  • VB

public void SomeMethod()
{
  // IObjectSpace
  using (var objectSpace = this.Application.CreateObjectSpace())
  {
    var serviceSpace = objectSpace.Xafari().ServiceSpace();
    // any code
    serviceSpace.CommitChanges();
  }
  // UnitOfWork
  using (var unitOfWork = this.Application.Xafari().CreateUnitOfWork())
  {
    var serviceSpace = unitOfWork.Xafari().ServiceSpace();
    // any code
    serviceSpace.CommitChanges();
  }
}

Public Sub SomeMethod()
  ' IObjectSpace
  Using objectSpace = Me.Application.CreateObjectSpace()
    Dim serviceSpace = objectSpace.Xafari().ServiceSpace()
    ' any code
    serviceSpace.CommitChanges()
  End Using
  ' UnitOfWork
  Using unitOfWork = Me.Application.Xafari().CreateUnitOfWork()
    Dim serviceSpace = unitOfWork.Xafari().ServiceSpace()
    ' any code
    serviceSpace.CommitChanges()
  End Using
End Sub

Synchronizing IObjectSpace Objects with the Service Space

IServiceSpace is aggregated by its owner IObjectSpace or UnitOfWork object. Thus, the life cycle of an IServiceSpace object is limited by the life cycle of its owner IObjectSpace or UnitOfWork object.

This feature is maintained by Service Spaces Manager disposing the IServiceSpace object when its IObjectSpace or UnitOfWork owner object is deleted. The service also deregisters the IServiceSpace object when the object is being removed.

The code snippet below creates a serviceSpace object in the context of an objectSpace object and deletes this object afterwards:

  • c#
  • VB

public void SomeMethod()
{
  using (var objectSpace = this.Application.CreateObjectSpace())
  {
    using (var serviceSpace = objectSpace.Xafari().ServiceSpace)
    {
      objectSpace.CommitChanges();
    }
    using (var serviceSpace = objectSpace.Xafari().ServiceSpace)
    {
      objectSpace.CommitChanges();
    }
  }
}

Public Sub SomeMethod()
  Using objectSpace = Me.Application.CreateObjectSpace()
    Using serviceSpace = objectSpace.Xafari().ServiceSpace
      objectSpace.CommitChanges()
    End Using
    Using serviceSpace = objectSpace.Xafari().ServiceSpace
      objectSpace.CommitChanges()
    End Using
  End Using
End Sub

The code above runs with no exceptions because the service space object deregistration is guaranteed by Service Spaces Manager and allows creating the second object.

For more info, refer to the Xafari Service section.