XAF 如何给記錄增加版本控制?

using System;
using System.ComponentModel;
 
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
 
using DevExpress.ExpressApp;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
 
namespace DXExample.Module
{
    [DefaultClassOptions]
    public class PO : BaseObject
    {
        public PO(Session session)
            : base(session)
        {
            // This constructor is used when an object is loaded from a persistent storage.
            
// Do not place any code here or place it only when the IsLoading property is false:
            
// if (!IsLoading){
            
//    It is now OK to place your initialization code here.
            
// }
            
// or as an alternative, move your initialization code into the AfterConstruction method.
        }
 
        private string _PONO;
        public string PONO
        {
            get
            {
                return _PONO;
            }
            set
            {
                SetPropertyValue("PONO"ref _PONO, value);
                if (!IsLoading)
                {
              //如果更改订单号,需重置Version
                    _Version = 0;
                }
            }
        }
 
        private int _Version;
        [Custom("AllowEdit","False")]
        public int Version
        {
            get
            {
                return _Version;
            }
            set
            {
                SetPropertyValue("Version"ref _Version, value);
            }
        }
 
        private string _Note;
        public string Note
        {
            get
            {
                return _Note;
            }
            set
            {
                SetPropertyValue("Note"ref _Note, value);
            }
        }
 
        private DateTime _CreateOn = DateTime.Now;
        [Custom("AllowEdit""False")]
        public DateTime CreateOn
        {
            get
            {
                return _CreateOn;
            }
            set
            {
                SetPropertyValue("CreateOn"ref _CreateOn, value);
            }
        }
        public override void AfterConstruction()
        {
            base.AfterConstruction();
            Version = 0;
        }
 
       
        private int GetPoNoNewVersion()
        {
            int ver = 1;
            ver = Utility.GetNewVersion<PO>(new BinaryOperator("PONO", PONO), Session, "Version""CreateOn");
            return ver;
        }
        protected override void OnSaving()
        {
            base.OnSaving();
            _Version = Version != 0 ? _Version : GetPoNoNewVersion();
        }
    }
 
    public static class Utility
    {
        /// <summary>
        
/// Get Version
        
/// Tonyyang
        
/// 2011-03-23
        
/// </summary>
        
/// <typeparam name="T">BO</typeparam>
        
/// <param name="criteriaOperator"></param>
        
/// <param name="session"></param>
        
/// <param name="propertyName"></param>
        
/// <param name="sortPropertyName"></param>
        
/// <returns></returns>
        public static int GetNewVersion<T>(CriteriaOperator criteriaOperator, Session session, string propertyName, string sortPropertyName) where T : XPCustomObject
        {
            int ver = 1;
            XPCollection<T> xpc = new XPCollection<T>(session) { Criteria = criteriaOperator };
            xpc.Sorting.Clear();
            xpc.Sorting.Add(new SortProperty(sortPropertyName, DevExpress.Xpo.DB.SortingDirection.Descending));
            if (xpc.IsLoaded != true)
                xpc.Load();
 
            ver = xpc.Count == 0 ? ver : (int)xpc[0].GetMemberValue(propertyName) + 1;
            return ver;
        }
    }
 
}
原文地址:https://www.cnblogs.com/ddlzq/p/2212398.html