Xafari Service. IServiceSpace

IServiceSpace concept is designed to ensure that context services could to expand the features both of IObjectSpace and UnitOfWork, i.e. IServiceSpace is an extension of this objects. IServiceSpace can be created either for IObjectSpace or for UnitOfWork. The life cycle of IServiceSpace is managed by a special service called ServiceSpacesManager.

There can be only one IServiceSpace object for each IObjectSpace or UnitOfWork object. Several services (IXafariService) can be tied to a single IServiceSpace object. Each IXafariService object can be used in several service spaces (IServiceSpace). The N:M relationship between IServiceSpace and IXafariService objects is provided by the IServiceSpaceContext.

xafari_service_3

Xafari framework includes  the ServiceSpace class, that implements IServiceSpace interface for working with IObjectSpace, UnitOfWork, INestedObjectSpace, and NestedUnitOfWork.

xafari_service_4

IServiceSpace operations

The ServiceSpace class has its own CommitChanges(), Rollback(), and Reload() methods. All these methods are synchronized with IObjectSpace or UnitOfWork. Also, when IObjectSpace.CommitChanges() or IServiceSpace.CommitChanges() is calling, the second one is called automatically straight after. The same system behavior is applied to Reload() and Rollback() methods.

How to use IServiceSpace

The code snippet below demonstrates how to access the IServiceSpace object from IObjectSpace (UnitOfWork) objects.

  • c#
  • VB

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

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