Skip to main content
A newer version of this page is available. .

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:

Context

Ways to Access Object Space

Examples

Business Object

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 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.

Controller

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.

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.

Module Updater

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.

List Editor

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.

Event Object Space Parameter
XafApplication.CreateCustomCollectionSource CreateCustomCollectionSourceEventArgs.ObjectSpace
XafApplication.CreateCustomLogonWindowObjectSpace CreateCustomPropertyCollectionSourceEventArgs.ObjectSpace
XafApplication.CreateCustomPropertyCollectionSource CreateCustomLogonWindowObjectSpaceEventArgs.ObjectSpace
XafApplication.ObjectSpaceCreated ObjectSpaceCreatedEventArgs.ObjectSpace
XafApplication.ViewCreating ViewCreatingEventArgs.ObjectSpace

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:

IObjectSpace objectSpace = this.Application.CreateObjectSpace(typeof(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.

Examples:

Important

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).

See Also