Object Space Factory
Overview
The Object Space Factory service solves a specific problem. The key thing is that some services require to create a separate IObjectSpace object. These services are Services Model Service, Branches Manager, and Settings Accessor.
It worth mentioning that the creation of an own IObjectSpace object is not an issue. Troubles emerge only when such services should be used in DatabaseUpdater (read also Update Application and Database Versions). In this case, all Updaters are running within a single IObjectSpace object created specially for DB updating. An attempt to create a new IObjectSpace object leads to restarting the procedure of DB update.
To solve this issue, Xafari provides a special XafariModuleUpdater base class.
In general, the Object Space Factory service can be used in various options requiring guaranteed execution withing the only IObjectSpace object.
Use Cases
- Guaranteed execution of code within the only IObjectSpace object.
- Create/get an IObjectSpace object.
Executing the code within the only IObjectSpace object
The BulkAction and BulkFunc methods start the process that uses the common IObjectSpace object. The code fragment below demonstrates this call:
- c#
- VB
public void SomeMethod()
{
ObjectSpaceFactory.Instance.BulkAction(ObjectSpace, objectSpace =>
{
//any code
});
}
Public Sub SomeMethod()
ObjectSpaceFactory.Instance.BulkAction(ObjectSpace, Function(ByVal objectSpace)
'any code
End Function)
End Sub
The BulkAction or BulkFunc method gets the IObjectSpace object and the delegate for code execution. The received IObjectSpace object is the root for create/get operations.
Note
It is prohibited to use nested calls to BulkAction or BulkFunc methods. An exception is generated when a nested request is found.
Create/get an IObjectSpace object
The CreateObjectSpace() method creates a new IObjectSpace object. If there is a root IObjectSpace object, INestedObjectSpace is created for it. Otherwise, a new IObjectSpace object is created. See the example of a new IObjectSpace object creation below.
- c#
- VB
public void SomeMethod()
{
using (var objectSpace = ObjectSpaceFactory.Instance.CreateObjectSpace())
{
// in dependent how this code is used objectSpace variable contains IObjectSpace or INestedObjectSpace
// but always new created object
// any code
}
}
Public Sub SomeMethod()
Using objectSpace = ObjectSpaceFactory.Instance.CreateObjectSpace()
' in dependent how this code is used objectSpace variable contains IObjectSpace or INestedObjectSpace
' but always new created object
' any code
End Using
End Sub
Use the GetObjectSpace() method to get the current IObjectSpace method. This method returns the root IObjectSpace object if it is neither empty nor disposed. Otherwise, a new IObjectSpace is created. The example of getting an IObjectSpace object is shown below:
- c#
- VB
var objectSpace = ObjectSpaceFactory.Instance.GetObjectSpace();// in dependent how this code is used objectSpace variable contains current root IObjectSpace
// or new created IObjectSpace but always IObjectSpace and never INestedObjectSpace
Private objectSpace As var = ObjectSpaceFactory.Instance.GetObjectSpace()' in dependent how this code is used objectSpace variable contains current root IObjectSpace ' or new created IObjectSpace but always IObjectSpace and never INestedObjectSpace
Refer to the next sections for more info: