Xafari Context Service. CommitChanges/Rollback/Reload

The IServiceSpaceContext object is the representation of IXafariService in IObjectSpace or UnitOfWork through the IServiceSpace object. The figure below shows the relationship between this classes.

xafari_service_5

Thus, the features and stages of the IServiceSpace's and IObjectSpace's life cycles is also actual for the IServiceSpaceContext object.

There are three methods of IObjectSpace which is very important to context services: CommitChanges, Rollback, and Reload. These methods can influence the functioning of the service itself. Hence, there are similar virtual methods for context objects. The invocation of these methods is synchronized with the corresponding methods in IServiceSpace and further, in accordance with the relationships, they are synchronized with the IObjectSpace methods. These methods (CommitChanges/Rollback/Reload) are assigned the same way as IObjectSpace methods. To perform custom operations while committing, reloading, or rolling back, it is needed to override the appropriate methods.

For instance, before executing IObjectSpace.CommitChanges() in the Numerators service, the automatic numeration should be applied to all properties specified in the Service Model. When the Rollback runs, all inner data supporting the declared service activity is reset.

Because of the synchronization between CommitChanges/Rollback/Reload methods launching and IServiceSpace and IObjectSpace or UnitOfWork, there is no need to call these methods directly within the context object. The code below demonstrates how the CommitChanges() method works. In this example, the CommitChanges() method executes for all context objects registered in the IServiceSpace object.

  • c#
  • VB

void CommitChanges()
{
  // 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();
  }
}

Private Sub CommitChanges()
  ' 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

Note

The synchronous execution of CommitChanges/Rollback/Reload happens only for active context objects (see Activation and Deactivation topic).