Application Settings. Slice Designer

The custom structure of Values Slices is defined via the Slices Designer. Slices Designer is a SettingsAccessorDesigner class (or its descendant), which provides methods to define Values Slices and their parameters and the method to create the SettingsAccessor object based on the given properties.

SettingsAccessor.CreateServiceInstance static handler provides a creation of the SettingsAccessor object. It is recommended to perform in the static constructor of the module class:

  • c#
  • VB

static Module1()
{
  SettingsAccessor.CreateServiceInstance = xafApplication => new SettingsAccessorDesigner<Department, UserWithDepartment, State>().First<Department>().Next<UserWithDepartment, Department>(getName: slice => slice.SliceObject.UserName, getParentSliceObject: slice => slice.SliceObject.Department, getSliceObjectsCriteria: slice => CriteriaOperator.Parse("Department = ?", slice.SliceObject)).Next<State, UserWithDepartment>().SetupCurrentSlice(accessor =>
  {
    var currentUser = SecuritySystem.CurrentUser as UserWithDepartment;
    if (currentUser != null)
      accessor.ObjectSpace.AppSettings().CurrentSlice(currentUser.Department, currentUser);
  }).CreateSettingsAccessor();
}

Shared Sub New()
  SettingsAccessor.CreateServiceInstance = Function(ByVal xafApplication)
    New SettingsAccessorDesigner(Of Department, UserWithDepartment, State)().First(Of Department)().[Next](Of UserWithDepartment, Department)(getName := Function(ByVal slice) slice.SliceObject.UserName, getParentSliceObject := Function(ByVal slice) slice.SliceObject.Department, getSliceObjectsCriteria := Function(ByVal slice) CriteriaOperator.Parse("Department = ?", slice.SliceObject)).[Next](Of State, UserWithDepartment)().SetupCurrentSlice(Sub(ByVal accessor)
      Dim currentUser = TryCast(SecuritySystem.CurrentUser, UserWithDepartment)
      If currentUser IsNot Nothing Then
        accessor.ObjectSpace.AppSettings().CurrentSlice(currentUser.Department, currentUser)
      End If
    End Sub).CreateSettingsAccessor()
  End Function
End Sub

Basic tools for tiered definition of Values Slices is First() and Next() methods. There are optional parameters of these methods, which are equal to null by default, these parameters are described below:

  • getName defines the handler to calculate the name of the Slice Object in the hierarchy. If parameter is not specified, the name gets the value from the ToString() method of the Slice Object.
  • getSliceObjectsCriteria defines the handler to calculate the criterion, which is used to get the list of child objects from the lower (next) Sllice. Thus, this parameter determines the rules to build a hierarchy of Slice Objects. If the not null getSlices parameter is defined in the code, then the getSliceObjectsCriteria value will be ignored.
  • getParentSliceObject defines the handler to calculate a parent object in the in the previous (higher) Slice.
  • getSlices defines the handler to calculate subordinate Slices. If this parameter is specified, then the value of the getSliceObjectsCriteria parameter is ignored.

SetupCurrentSlice() method is intended to provide a Current Slice. The method parameter is a handler that is automatically called when initializing the SettingsAccessor service.

CreateSettingsAccessor() method creates a SettinsAccessor object in accordance with the settings specified in the Designer.

For ease of use, Application Setting module supplies three SettingsAccessorDesigner heirs that implement a single-level, two-level and three-level data structure. This is the most common use cases (see Typical Scenarios). The parameters are the persistent types for the corresponding Slice Basis Types:

  • SettingsAccessorDesigner<TKey1>: data structure with one Slice, see Example 1 below.
  • SettingsAccessorDesigner<TKey1, TKey2>: data structure with two Slices, see Example 2 below.
  • SettingsAccessorDesigner<TKey1, TKey2, TKey3>: data structure with three Slices, see Example 3 below.

Example 1

An example of a single-level structure:

  • c#
  • VB

static Module1()
{
  SettingsAccessor.CreateServiceInstance = xafApplication => new SettingsAccessorDesigner<User>().SetupCurrentSlice(accessor =>
  {
    var currentUser = SecuritySystem.CurrentUser as User;
    if (currentUser != null)
      accessor.ObjectSpace.AppSettings().CurrentSlice(currentUser);
  }).CreateSettingsAccessor();
}

Shared Sub New()
  SettingsAccessor.CreateServiceInstance = Function(ByVal xafApplication)
    New SettingsAccessorDesigner(Of User)().SetupCurrentSlice(Sub(ByVal accessor)
      Dim currentUser = TryCast(SecuritySystem.CurrentUser, User)
      If currentUser IsNot Nothing Then
        accessor.ObjectSpace.AppSettings().CurrentSlice(currentUser)
      End If
    End Sub).CreateSettingsAccessor()
  End Function
End Sub

Example 2

An example of a two-level structure:

  • c#
  • VB

static Module1()
{
  SettingsAccessor.CreateServiceInstance = xafApplication => new SettingsAccessorDesigner<Branch, UserWithBranch>().Slice1().Slice2(getName: slice => slice.SliceObject.UserName, getParentSliceObject: slice => slice.SliceObject.Branch, getSliceObjectsCriteria: slice => CriteriaOperator.Parse("Branch = ?", slice.SliceObject)).SetupCurrentSlice(accessor =>
  {
    var currentUser = SecuritySystem.CurrentUser as UserWithBranch;
    if (currentUser != null)
      accessor.ObjectSpace.AppSettings().CurrentSlice(currentUser.Branch, currentUser);
  }).CreateSettingsAccessor();
}

Shared Sub New()
  SettingsAccessor.CreateServiceInstance = Function(ByVal xafApplication)
    New SettingsAccessorDesigner(Of Branch, UserWithBranch)().Slice1().Slice2(getName := Function(ByVal slice) slice.SliceObject.UserName, getParentSliceObject := Function(ByVal slice) slice.SliceObject.Branch, getSliceObjectsCriteria := Function(ByVal slice) CriteriaOperator.Parse("Branch = ?", slice.SliceObject)).SetupCurrentSlice(Sub(ByVal accessor)
      Dim currentUser = TryCast(SecuritySystem.CurrentUser, UserWithBranch)
      If currentUser IsNot Nothing Then
        accessor.ObjectSpace.AppSettings().CurrentSlice(currentUser.Branch, currentUser)
      End If
    End Sub).CreateSettingsAccessor()
  End Function
End Sub

Example 3

An example of a three-tier structure:

  • c#
  • VB

static Module1()
{
  SettingsAccessor.CreateServiceInstance = xafApplication => new SettingsAccessorDesigner<Department, UserWithDepartment, State>().Slice1().Slice2(getName: slice => slice.SliceObject.UserName, getParentSliceObject: slice => slice.SliceObject.Department, getSliceObjectsCriteria: slice => CriteriaOperator.Parse("Department = ?", slice.SliceObject)).Slice3().SetupCurrentSlice(accessor =>
  {
    var currentUser = SecuritySystem.CurrentUser as UserWithDepartment;
    if (currentUser != null)
      accessor.ObjectSpace.AppSettings().CurrentSlice(currentUser.Department, currentUser);
  }).CreateSettingsAccessor();
}

Shared Sub New()
  SettingsAccessor.CreateServiceInstance = Function(ByVal xafApplication)
    New SettingsAccessorDesigner(Of Department, UserWithDepartment, State)().Slice1().Slice2(getName := Function(ByVal slice) slice.SliceObject.UserName, getParentSliceObject := Function(ByVal slice) slice.SliceObject.Department, getSliceObjectsCriteria := Function(ByVal slice) CriteriaOperator.Parse("Department = ?", slice.SliceObject)).Slice3().SetupCurrentSlice(Sub(ByVal accessor)
      Dim currentUser = TryCast(SecuritySystem.CurrentUser, UserWithDepartment)
      If currentUser IsNot Nothing Then
        accessor.ObjectSpace.AppSettings().CurrentSlice(currentUser.Department, currentUser)
      End If
    End Sub).CreateSettingsAccessor()
  End Function
End Sub