Back to Devexpress

PersistentBase.AfterConstruction() Method

xpo-devexpress-dot-xpo-dot-persistentbase.md

latest9.4 KB
Original Source

PersistentBase.AfterConstruction() Method

Invoked when the current object is about to be initialized after its creation.

Namespace : DevExpress.Xpo

Assembly : DevExpress.Xpo.v25.2.dll

NuGet Package : DevExpress.Xpo

Declaration

csharp
public virtual void AfterConstruction()
vb
Public Overridable Sub AfterConstruction

Remarks

Override this method in your PersistentBase descendants to add custom initialization logic to your persistent objects (see the code example below).

csharp
using DevExpress.Xpo;

...
public class MyDataObject : XPObject {
    public MyDataObject()
        : base() {
        // This constructor is used when an object is loaded from a persistent storage.
        // Do not place any code here.
    }

    public MyDataObject(Session session)
        : base(session) {
        // This constructor is used when an object is loaded from a persistent storage.
        // Do not place any code here.
    }

    public override void AfterConstruction() {
        base.AfterConstruction();
        // Place your initialization code here.
    }
}

The base AfterConstruction method implementation includes only functionality relevant for persistent objects associated with UnitOfWork. In essence, the default implementation marks the current object to be saved to a data store if both of the following conditions are met:

  • the current object is associated with a unit of work;
  • the current object is persistent (see XPTypeInfo.IsPersistent).

Example

The example below demonstrated how to automatically populate the CreatedBy, CreatedOn, UpdatedBy, and UpdatedOn properties (audit columns) in an XAF business class.

csharp
using DevExpress.Xpo;  
...

namespace YourSolutionName.BusinessObjects {  
    [DefaultClassOptions]  
    public class Contact : BaseObject {  
        public Contact(Session session)  
            : base(session) {  
        }  
        // Get a current active user
        ApplicationUser GetCurrentUser() {  
            return Session.GetObjectByKey<ApplicationUser>(
                Session.ServiceProvider.GetRequiredService<ISecurityStrategyBase>().UserId);
        }  
        // Automatically fill the CreatedOn and CreatedBy columns 
        // when a current user creates a new Contact item.
        public override void AfterConstruction() {  
            base.AfterConstruction();  
            CreatedOn = DateTime.Now;  
            CreatedBy = GetCurrentUser();  
        }  
        // Automatically fill the UpdatedOn and UpdatedBy columns 
        // when a current user saves a modified Contact item.
        protected override void OnSaving() {  
            base.OnSaving();  
            UpdatedOn = DateTime.Now;  
            UpdatedBy = GetCurrentUser();  
        }

        protected override void OnChanged(string propertyName, object oldValue, object newValue) {
            base.OnChanged(propertyName, oldValue, newValue);
            if(!IsLoading) {
                switch(propertyName) {
                    // Run custom code when the "Email" property value is changed.
                    case nameof(Email):
                        UpdateContacts();
                        break;
                }
            }
        }

        ApplicationUser createdBy;  
        public ApplicationUser CreatedBy {  
            get { return createdBy; }  
            set { SetPropertyValue("CreatedBy", ref createdBy, value); }  
        }  
        DateTime createdOn;  
        public DateTime CreatedOn {  
            get { return createdOn; }  
            set { SetPropertyValue("CreatedOn", ref createdOn, value); }  
        }  
        ApplicationUser updatedBy;  
        public ApplicationUser UpdatedBy {  
            get { return updatedBy; }  
            set { SetPropertyValue("UpdatedBy", ref updatedBy, value); }  
        }  
        DateTime updatedOn;  
        public DateTime UpdatedOn {  
            get { return updatedOn; }  
            set { SetPropertyValue("UpdatedOn", ref updatedOn, value); }  
        }  
    }  
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the AfterConstruction() 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-customize-xpo-business-model-at-runtime/CS/CustomizeXPOModel/MyXPOClassLibrary/PersistentObject2.cs#L14

csharp
public override void AfterConstruction() {
    base.AfterConstruction();
    // Place here your initialization code.

XPO_how-to-get-the-maximum-or-minimum-objects-value-from-a-collection-e468/CS/ConsoleApplication1/Quote.cs#L17

csharp
public override void AfterConstruction() {
    base.AfterConstruction();
    // Place here your initialization code.

XPO_how-to-clone-a-persistent-object-e804/CS/SimpleObject/ObjectClass.cs#L8

csharp
public override void AfterConstruction() {
    base.AfterConstruction();
}

xpo-json-serialization/CS/XpoSerialization/DxSampleModelCode/Customer.cs#L14

csharp
public Customer(Session session) : base(session) { }
    public override void AfterConstruction() { base.AfterConstruction(); }
}

winforms-reporting-create-report-bound-to-xpobjectsource/CS/ORMDataModel1Code/Categories.cs#L12

csharp
public Categories(Session session) : base(session) { }
    public override void AfterConstruction() { base.AfterConstruction(); }
}

XPO_how-to-clone-a-persistent-object-e804/VB/SimpleObject/ObjectClass.vb#L12

vb
Public Overrides Sub AfterConstruction()
    MyBase.AfterConstruction()
End Sub

XPO_how-to-track-changes-made-to-persistent-objects-and-write-them-into-a-separate-table-e2419/VB/Q149895/Northwind.vb#L110

vb
Public Overrides Sub AfterConstruction()
    MyBase.AfterConstruction()
End Sub

winforms-reporting-create-report-bound-to-xpobjectsource/VB/ORMDataModel1Code/Categories.vb#L15

vb
Public Overrides Sub AfterConstruction()
    MyBase.AfterConstruction()
End Sub

winforms-filtercontrol-implement-ifilteredcomponent/VB/Q200298/Northwind.vb#L64

vb
Public Overrides Sub AfterConstruction()
    MyBase.AfterConstruction()
End Sub

XDL_how-to-convert-the-criteriaoperator-to-a-lambda-expression-to-use-it-for-iqueryable-e2596/VB/WindowsFormsApplication152/Products.vb#L78

vb
Public Overrides Sub AfterConstruction()
    MyBase.AfterConstruction()
End Sub

See Also

OnChanged(String, Object, Object)

OnDeleting()

OnSaving()

PersistentBase Class

PersistentBase Members

DevExpress.Xpo Namespace