.NET 學習

.NET 學習生活感想... 万事成蹉跎..... 贵在坚持 及时整理自己做过和学过的东西

博客园 首页 新随笔 联系 订阅 管理

 例如给订单增加版本控制

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;
        }
    }
 
}

 欢迎转载,转载请注明出处:http://www.cnblogs.com/Tonyyang/

posted on 2011-03-23 12:27  Tonyyang  阅读(465)  评论(0编辑  收藏  举报
欢迎转载,转载请注明出处:http://www.cnblogs.com/Tonyyang/