There are two common places for your business logic code - Controllers and business classes themselves. In Controllers, you can declare new and customize existing Actions and handle other Controllers' events. In business classes, you can put logic into property getters and setters, implement methods that are triggered automatically when the object is created, loaded, saved and deleted (see IXafEntityObject) and declare action methods. In these cases, Object Space is required when data the current business object exposes is insufficient for your business logic, and you need to query more data. You may also use Object Space in your database initialization code, in complex View Items, etc. This topic describes the API you can use to get Object Space in various contexts.
Get an Existing Object Space
Usually, you can use an existing Object Space instance accessible via the XAF API. The following table describes how you can get an Object Space from various contexts of your code:
Ways to Access Object Space
An Object Space reference is automatically assigned to the IObjectSpaceLink.ObjectSpace property when a business object supporting an IObjectSpaceLink is instantiated. You can implement this interface and access other business objects directly in the current object code using the Object Space passed to the ObjectSpace property.
We recommend using Controllers instead of accessing UI-related entities (Views, Controllers, Actions) and executing UI-specific logic within the business class code, because it violates the separation of concerns principle and is against the MVC architecture.
In XAF applications, an Object Space is automatically assigned for each View. In a View Controller, you can get the current View using the ViewController.View property. Then, access the Object Space with the View.ObjectSpace. You can also use the protected ViewController.ObjectSpace property that refers to the same Object Space as View.ObjectSpace.
A Window Controller does not expose a View directly. You can access the current Frame using the Controller.Frame property and get the View with Frame.View.
In a ModuleUpdater descendant, you can use the protected ModuleUpdater.ObjectSpace property to access the Object Space instance that can be used for database update operations. Do not use a new Object Space to update the database.
Dim objectSpace As IObjectSpace = Me.Application.CreateObjectSpace(GetType(MyBusinessClass))
Refer to the XafApplication class description to see how to obtain an XafApplication instance in various contexts. You cannot create new Object Spaces in the business object context because an XafApplication instance is not available there.
You should manually dispose of an Object Space when you are finished using it if you do not assign it to a View. An Object Space associated with a View is removed automatically together with this View.