Back to Devexpress

IObjectSpace.CreateObject(Type) Method

expressappframework-devexpress-dot-expressapp-dot-iobjectspace-dot-createobject-x28-system-dot-type-x29.md

latest7.0 KB
Original Source

IObjectSpace.CreateObject(Type) Method

Creates an object of the specified type.

Namespace : DevExpress.ExpressApp

Assembly : DevExpress.ExpressApp.v25.2.dll

NuGet Package : DevExpress.ExpressApp

Declaration

csharp
object CreateObject(
    Type type
)
vb
Function CreateObject(
    type As Type
) As Object

Parameters

NameTypeDescription
typeType

A Type object which is the type of the object to be created.

|

Returns

TypeDescription
Object

An object that represents the created object of the specified type.

|

Remarks

The following example uses a Parametrized Action to create a new Department object and refresh the Detail View. The new department becomes available in the Contact.Department Lookup List View.

csharp
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using MainDemo.Module.BusinessObjects;
// ...
public class AddDepartmentController : ObjectViewController<DetailView, Contact> {
    public AddDepartmentController() {
        ParametrizedAction addDepartmentAction = new ParametrizedAction(
            this, "AddDepartment", PredefinedCategory.Edit, typeof(string));
        addDepartmentAction.Execute += AddDepartmentAction_Execute;
    }
    private void AddDepartmentAction_Execute(object sender, ParametrizedActionExecuteEventArgs e) {
        using(IObjectSpace objectSpace = Application.CreateObjectSpace(typeof(Department))) {
            Department department = objectSpace.CreateObject<Department>();
            department.Title = e.ParameterCurrentValue as string;
            objectSpace.CommitChanges();
        }
        View.Refresh();
    }
}

If you implement the IObjectSpace interface in the BaseObjectSpace class’ descendant, you don’t have to override the CreateObject method entirely. The BaseObjectSpace.CreateObject method invokes a protected virtual BaseObjectSpace.CreateObjectCore method and then sets the returned object modified by calling the BaseObjectSpace.SetModified method for it. So, you should only override the CreateObjectCore method.

The created object will be saved to the database when calling the IObjectSpace.CommitChanges method.

The following code snippets (auto-collected from DevExpress Examples) contain references to the CreateObject(Type) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

xaf-how-to-implement-a-custom-security-operation-that-can-be-permitted-at-the-type-level/CS/EF/CustomPermissionEF/CustomPermissionEF.Module/Controllers/RemoveBaseTypePermissionNewActionItemController.cs#L17

csharp
if (e.ObjectType == typeof(PermissionPolicyTypePermissionObject)) {
    e.NewObject = e.ObjectSpace.CreateObject(typeof(CustomTypePermissionObject));
}

XAF_Non-Persistent-Objects-Edit-Linked-Persistent-Objects-Demo/CS/EFCore/NonPersistentEditEF/NonPersistentEditEF.Module/Controllers/NonPersistentObjectActivatorController.cs#L39

csharp
var objectSpace = Application.CreateObjectSpace(objectType);
var obj = objectSpace.CreateObject(objectType);
objectSpace.RemoveFromModifiedObjects(obj);

XAF-search-objects-using-complex-criterion/CS/Dennis.Search.Win/SearchObjectViewController.cs#L31

csharp
IObjectSpace os = Application.CreateObjectSpace(type);
object obj = os.CreateObject(type);
DetailView dv = Application.CreateDetailView(os, obj);

XAF_logon-form-manage-users-register-a-new-user-restore-a-password/CS/Security.Extensions/Controllers/ManageUsersOnLogonController.cs#L74

csharp
var objectSpace = Application.CreateObjectSpace(logonActionParametersType);
var logonActionParameters = objectSpace.CreateObject(logonActionParametersType);
var detailView = Application.CreateDetailView(objectSpace, logonActionParameters);

XAF-search-objects-using-complex-criterion/VB/Dennis.Search.Win/SearchObjectViewController.vb#L42

vb
Dim os As IObjectSpace = Application.CreateObjectSpace(type)
Dim obj As Object = os.CreateObject(type)
Dim dv As DetailView = Application.CreateDetailView(os, obj)

See Also

Create, Read, Update and Delete Data

IObjectSpace Interface

IObjectSpace Members

DevExpress.ExpressApp Namespace