Ways to Implement Business Logic
- 5 minutes to read
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.
If you additionally implement an IXafEntityObject, then you can place your logic into the IXafEntityObject.OnCreated, IXafEntityObject.OnLoaded and IXafEntityObject.OnSaving methods. You can use the XAF Business Object | EF 6 Business Object from Template Gallery to add a class that supports both IXafEntityObject and IObjectSpaceLink.
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 View handles Object Space events to update the UI each time an object changes. We recommend creating a new Object Space that is not bound to a current View instead of using the View.ObjectSpace property to process large amounts of data. You should also use a new Object Space when you create a new View with the XafApplication.CreateListView or XafApplication.CreateDetailView methods.
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.
A reference to the CollectionSourceBase object is automatically passed to the IComplexListEditor.Setup method if your custom List Editor supports the IComplexListEditor interface. You can implement this interface and access the Object Space via the CollectionSourceBase.ObjectSpace property.
View Item or Property Editor
A reference to an Object Space is automatically passed to the IComplexViewItem.Setup method if your custom View Item or Property Editor supports the IComplexViewItem interface. You can implement this interface and store the Object Space reference to a local variable for future use.
Object Space is also available using arguments passed to various events of the XafApplication class.
Create a New Object Space
It is often necessary to instantiate a new Object Space (for example, when you create a View using the XafApplication.CreateListView or XafApplication.CreateDetailView methods). Use the XafApplication.CreateObjectSpace(Type) method instead of an Object Space constructor to create an Object Space. For instance, the Controller.Application property is available in a Controller context, and you can create an Object Space as follows:
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.
- Add a Parametrized Action
- Add an Action that Displays a Pop-up Window
- Add an Action with Option Selection
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.
Once you have obtained or created an Object Space, you can use it to query or modify data (see Create, Read, Update and Delete Data).