Skip to main content
A newer version of this page is available. .
All docs
V21.2

Create a New Object

  • 4 minutes to read

Useful API

You can use following Object Space methods to create a new object:

IObjectSpace.CreateObject
Creates an object of the specified type.
IObjectSpace.CreateObject<ObjectType>
Creates an object of the type designated by the specified generic type parameter.
IObjectSpace.IsNewObject
Indicates whether a specified object has been created but has not been saved to the database.
IObjectSpace.CommitChanges
Saves all the changes made to the persistent objects belonging to the current Object Space to the database.

In a Controller (the ORM-Independent Technique)

  1. Create a new View Controller in a Module project (for example, in MySolution.Module).
  2. Use the ViewController.ObjectSpace property to access an Object Space for data-aware operations.
  3. Use the Object Space’s methods to create a new object.
  4. Call the CommitChanges() method to save changes.

Example 1

The following Controller contains the AddTask Action that creates a new Task object:

File: MySolution.Module\Controllers\TaskViewController.cs(.vb).

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
// ...
public class TaskViewController : ObjectViewController<ListView, Task> {
    public TaskViewController() {
        SimpleAction addTaskAction = new SimpleAction(this, "AddTask", PredefinedCategory.Edit);
        addTaskAction.Execute += AddTaskAction_Execute;
    }
    private void AddTaskAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
        Task task = ObjectSpace.CreateObject<Task>();
        task.Subject = "Demo Task";
        View.CollectionSource.Add(task);
        ObjectSpace.CommitChanges();
        View.Refresh();
    }
}

Example 2

The following Controller contains the AddTask Action that adds a new Task object to the Employee‘s Tasks collection. This Controller activates for the Employee Detail View and accesses the nested Tasks List View as described in the following topic: How to: Access Master Detail View and Nested List View Environment.

File: MySolution.Module\Controllers\EmployeeViewController.cs(.vb).

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.Editors;
// ...
public class EmployeeViewController : ObjectViewController<DetailView, Employee> {
    public EmployeeViewController() {
        SimpleAction addTaskAction = new SimpleAction(this, "AddTask", PredefinedCategory.Edit);
        addTaskAction.Execute += AddTaskAction_Execute;
    }
    private void AddTaskAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
        ListPropertyEditor listPropertyEditor = View.FindItem(nameof(Employee.Tasks)) as ListPropertyEditor;
        IObjectSpace os = listPropertyEditor.ListView.ObjectSpace;
        Task task = os.CreateObject<Task>();
        task.Subject = "Demo Task";
        listPropertyEditor.ListView.CollectionSource.Add(task);
        os.CommitChanges();
        listPropertyEditor.ListView.Refresh();
    }
}

In an XPO Business Class

In an XPO business class, use the required business class constructor to create a new object and pass the current object’s Session as the constructor parameter. You can do this in the overridden AfterConstruction method.

The following code snippet demonstrates how to initialize Employee‘s Address1 and Manager reference properties with new and existing objects:

File: MySolution.Module\BusinessObjects\Contact.cs(.vb).

using DevExpress.Data.Filtering;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;
// ...
public class Employee : Person {
    //...
    public override void AfterConstruction() {
        base.AfterConstruction();
        Address1 = new Address(Session);
        Address1.Country = Session.FindObject<Country>(CriteriaOperator.Parse("Name = 'USA'"));
        if(Address1.Country == null) {
            Address1.Country = new Country(Session);
            Address1.Country.Name = "USA";
        }
        Manager = Session.FindObject<Employee>(CriteriaOperator.Parse(
            "FirstName = 'John' && LastName = 'Doe'"));
    }
}

In a EF Core Business Class

  1. Implement the IObjectSpaceLink interface in your EF Core business class.
  2. Use the ObjectSpace property to access an Object Space for data-aware operations. XAF automatically assigns an Object Space instance to this property.
  3. Use the Object Space’s methods to create a new object.

The following code snippet demonstrates how to initialize Employee‘s Address1 and Manager reference properties with new and existing objects:

File: MySolution.Module\BusinessObjects\Employee.cs.

using DevExpress.ExpressApp;
using DevExpress.Persistent.BaseImpl;
// ...
public class Employee : Person, IXafEntityObject, IObjectSpaceLink {
    //...
    void IXafEntityObject.OnCreated() {
        // ...
        Address1 = objectSpace.CreateObject<Address>();
        Address1.Country = objectSpace.FirstOrDefault<Country>(c => c.Name == "USA");
        if (Address1.Country == null) {
            Address1.Country = objectSpace.CreateObject<Country>();
            Address1.Country.Name = "USA";
        }
        Manager = objectSpace.FirstOrDefault<Employee>(e => e.FirstName == "John" && e.LastName == "Doe");
    }
    void IXafEntityObject.OnLoaded() { }
    void IXafEntityObject.OnSaving() { }
    private IObjectSpace objectSpace;
    IObjectSpace IObjectSpaceLink.ObjectSpace {
        get { return objectSpace; }
        set { objectSpace = value; }
    }
}