Xafari Module Updater

To use the Xafari Module Updater functionality, first read the following sections:

The ModuleUpdater class updates the DB when the module version changes. It is a basic DevExpress XAF function. All Updaters executes within a single IObjectSpace object created specially for DB updating. An attempt to create a new IObjectSpace object while updating leads to restarting the procedure of DB update.

Some services included into Xafari platform require to create a separate IObjectSpace object. These services are Services Model Service, Branches Manager, and Settings Accessor. To simplify the situation with separate IObjectSpace object issue, Xafari provides a special XafariModuleUpdater base class.

Concepts

XafariModuleUpdater is based upon the Object Space Factory service.

The Xafari.BC.XafariModuleUpdater class is included into Xafari.BC.dll. It is the descendant of the DevExpress.ExpressApp.Updating.ModuleUpdater class. The usage of this class is quite similar to the usage of ModuleUpdater as the base class for other Updaters. The specific features are the next:

  • The Xafari BC module should be added;
  • In the descendant, the SafeUpdateDatabaseAfterUpdateSchema method should be overridden (instead of UpdateDatabaseAfterUpdateSchema);
  • The UpdateDatabaseBeforeUpdateSchema method is sealed (closed from inheritance), and a separate Updater (heir of ModuleUpdater) should be implemented to override it.

Important:

The code inside the SafeUpdateDatabaseAfterUpdateSchema method should use the IObjectSpace object received either from the ModuleUpdater.ObjectSpace property or through the Object Space Factory service.

Note:

The code inside the SafeUpdateDatabaseAfterUpdateSchema method should not call the BulkAction or BulkFunc methods of the ObjectSpaceFactory class. An exception is generated in case of founding such calls.

The example

The code sample below demonstrates the implementation of the Settings Accessor service. It is shown how to get an IObjectSpace instance in the service:

  • c#
  • VB

/// <summary>
/// Provides service to operate application settings.
/// </summary>
public class SettingsAccessor : XafariServiceBase<SettingsAccessor>, ICurrentSliceProvider
{
private IObjectSpace _objectSpace;
/// <summary>
/// Contains a reference to ObjectSpace.
/// </summary>
public IObjectSpace ObjectSpace
{
  get
  {
    if (_objectSpace == null || _objectSpace.IsDisposed)
      this._objectSpace = ObjectSpaceFactory.Instance.GetObjectSpace();
    return _objectSpace;
  }
}
}

''' <summary>
''' Provides service to operate application settings.
''' </summary>
Public Class SettingsAccessor
  Inherits XafariServiceBase(Of SettingsAccessor)
  Implements ICurrentSliceProvider
  Private __objectSpace As IObjectSpace
  ''' <summary>
  ''' Contains a reference to ObjectSpace.
  ''' </summary>
  Public ReadOnly Property ObjectSpace As IObjectSpace
    Get
      If __objectSpace Is Nothing OrElse __objectSpace.IsDisposed Then
        Me.__objectSpace = ObjectSpaceFactory.Instance.GetObjectSpace()
      End If
      Return __objectSpace
    End Get
  End Property
End Class

The implementation of an Updater in the Xafari.FeatureCenter application is shown below. The code fragment demonstrates how to request to SettingsAccessor in the Updater.

  • c#
  • VB

namespace Xafari.FeatureCenter
{
  public class Updater : XafariModuleUpdater
  {
    public Updater(IObjectSpace objectSpace, Version currentDbVersion)
      : base(objectSpace, currentDbVersion)
    {
    }
    public override void SafeUpdateDatabaseAfterUpdateSchema()
    {
      base.SafeUpdateDatabaseAfterUpdateSchema();
      SettingsAccessor.Instance.Updaters.Add(new FCSettingsNodeUpdater());
      this.ObjectSpace.AppSettings().Update();
      this.ObjectSpace.AppSettings().Root().KeysFCSettings().KeyString.Value = "new value with updater";
      ObjectSpace.CommitChanges();
    }
  }
}

Namespace Xafari.FeatureCenter
  Public Class Updater
    Inherits XafariModuleUpdater
    Public Sub New(ByVal objectSpace As IObjectSpace, ByVal currentDbVersion As Version)
      MyBase.New(objectSpace, currentDbVersion)
    End Sub
    Public Overrides Sub SafeUpdateDatabaseAfterUpdateSchema()
      MyBase.SafeUpdateDatabaseAfterUpdateSchema()
      SettingsAccessor.Instance.Updaters.Add(New FCSettingsNodeUpdater())
      Me.ObjectSpace.AppSettings().Update()
      Me.ObjectSpace.AppSettings().Root().KeysFCSettings().KeyString.Value = "new value with updater"
      ObjectSpace.CommitChanges()
    End Sub
  End Class
End Namespace