Back to Devexpress

IObjectSpace.CreateObject<ObjectType>() Method

expressappframework-devexpress-dot-expressapp-dot-iobjectspace-dot-createobject-1.md

latest8.7 KB
Original Source

IObjectSpace.CreateObject<ObjectType>() Method

Creates an object of the type designated by the specified generic type parameter.

Namespace : DevExpress.ExpressApp

Assembly : DevExpress.ExpressApp.v25.2.dll

NuGet Package : DevExpress.ExpressApp

Declaration

csharp
ObjectType CreateObject<ObjectType>()
vb
Function CreateObject(Of ObjectType) As ObjectType

Type Parameters

Name
ObjectType

Returns

TypeDescription
ObjectType

A created object of the specified type.

|

Remarks

The following code snippet uses a Parametrized Action to create a new Department object and add it to the Departments collection of the current Contact object.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
// ...
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) {
        Department department = ObjectSpace.CreateObject<Department>();
        department.Title = e.ParameterCurrentValue as string;
        Contact contact = (Contact)View.CurrentObject;
        contact.Departments.Add(department);
    }
}

If you implement the IObjectSpace interface in the BaseObjectSpace class’ descendant, you don’t have to override the CreateObject<ObjectType> method entirely. The BaseObjectSpace.CreateObject<ObjectType> 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<ObjectType>() 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-display-a-list-of-non-persistent-objects/CS/EFCore/NonPersistentListViewEF/NonPersistentListViewEF.Module/DatabaseUpdate/Updater.cs#L23

csharp
Book bookOne = ObjectSpace.CreateObject<Book>();
bookOne.Title = "A Visitor For Bear";

xaf-how-to-show-persistent-objects-in-a-non-persistent-objects-view/CS/EFCore/ComplexDialogEF/ComplexDialogEF.Module/Controllers/MyController.cs#L23

csharp
e.Context = TemplateContext.PopupWindow;
var template = os.CreateObject<OrderTemplate>();
e.View = Application.CreateDetailView(os, template);

xaf-win-enable-inplace-editing-in-tree-list-view/CS/EFCore/TreeListInplaceEF/TreeListInplaceEF.Module/DatabaseUpdate/Updater.cs#L23

csharp
}
Contact p1 = ObjectSpace.CreateObject<Contact>();
p1.FirstName = "Contact1";

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

csharp
ISecurityUserLoginInfo ISecurityUserWithLoginInfo.CreateUserLoginInfo(string loginProviderName, string providerUserKey) {
    ApplicationUserLoginInfo result = ((IObjectSpaceLink)this).ObjectSpace.CreateObject<ApplicationUserLoginInfo>();
    result.LoginProviderName = loginProviderName;

xaf-custom-list-editor-blazor/CS/XPO/CustomEditor/CustomEditor.Module/DatabaseUpdate/Updater.cs#L25

csharp
if (image1 == null) {
    image1 = ObjectSpace.CreateObject<PictureItem>();
    image1.Text = "Green";

connect-winforms-grid-to-backend-using-middletier-server/VB/DataModel.Shared/BusinessObjects/ApplicationUser.vb#L34

vb
Implements ISecurityUserWithLoginInfo.CreateUserLoginInfo
    Dim result As ApplicationUserLoginInfo = DirectCast(CType(Me, IObjectSpaceLink).ObjectSpace.CreateObject(Of ApplicationUserLoginInfo)(), ApplicationUserLoginInfo)
    result.LoginProviderName = loginProviderName

XAF-CRUD-for-Non-Persistent-Objects-Stored-Remotely/VB/NonPersistentObjectsDemo.Module/DatabaseUpdate/Updater.vb#L43

vb
If sampleUser Is Nothing Then
    sampleUser = ObjectSpace.CreateObject(Of PermissionPolicyUser)()
    sampleUser.UserName = "User"

XAF_Non-Persistent-Objects-Filtering-Demo/VB/NonPersistentObjectsDemo.Module/DatabaseUpdate/Updater.vb#L32

vb
If sampleUser Is Nothing Then
    sampleUser = ObjectSpace.CreateObject(Of PermissionPolicyUser)()
    sampleUser.UserName = "User"

xaf-separate-employees-data-in-different-departments-using-security-permissions/VB/MainDemo.Module/DatabaseUpdate/Updater.vb#L165

vb
If administratorRole Is Nothing Then
    administratorRole = ObjectSpace.CreateObject(Of PermissionPolicyRole)()
    administratorRole.Name = "Administrators"

xaf-how-to-display-a-collection-property-as-a-checked-list-box/VB/DXExample.Module/Updater.vb#L20

vb
If master Is Nothing Then
    master = ObjectSpace.CreateObject(Of Master)()
    master.MasterName = "TestMaster"

See Also

Create, Read, Update and Delete Data

IObjectSpace Interface

IObjectSpace Members

DevExpress.ExpressApp Namespace