All docs
V21.1
21.2 (EAP/Beta)
21.1
20.2
20.1
19.2
The page you are viewing does not exist in version 19.2. This link will take you to the root page.
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.
.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

PersistentBase.OnChanged(String, Object, Object) Method

Invoked after an object’s property has been changed.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v21.1.dll

Declaration

protected virtual void OnChanged(
    string propertyName,
    object oldValue,
    object newValue
)

Parameters

Name Type Description
propertyName String

A property whose value was changed.

oldValue Object

An old value.

newValue Object

A new value.

Remarks

Override this method in your PersistentBase descendants to add custom logic to a persistent object after the current object has been changed.

The code sample below demonstrates how to use the OnChanged method to declare persistent properties.

DateTime fOrderDate;
public DateTime OrderDate {
    get { return fOrderDate; }
    set {
        if(fOrderDate != value) {
            DateTime OldValue = fOrderDate;
            fOrderDate = value;
            OnChanged(nameof(OrderDate), OldValue, fOrderDate);
        }
    }
}

Example

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

using DevExpress.Xpo;  
...

namespace YourSolutionName.BusinessObjects {  
    [DefaultClassOptions]  
    public class Contact : BaseObject {  
        public Contact(Session session)  
            : base(session) {  
        }  
        // Get a current active user
        User GetCurrentUser() {  
            return Session.GetObjectByKey<User>(SecuritySystem.CurrentUserId);  
        }  
        // 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;
                }
            }
        }

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