Skip to main content
.NET 8.0+

PersistentBase.OnSaving() Method

Invoked when the current object is about to be saved.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v24.2.dll

Declaration

protected virtual void OnSaving()

Remarks

Override this method in your PersistentBase descendants to add custom logic to a persistent object when the object is about to be saved.

Example

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

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 snippet (auto-collected from DevExpress Examples) contains a reference to the OnSaving() 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.

See Also