数据编辑基类模板

    /// <summary>

    /// 数据模型操作基础类

    /// </summary>

    /// <typeparam name="T">模型类型</typeparam>

    /// <typeparam name="KT">主索引类型</typeparam>

    public abstract class ModelLogicAdapter<T,KT> : System.MarshalByRefObject, IModelLogic where T:new()

    {

        #region IModelLogic 成员

        private T mItem= new T();

        public T Item

        {

            get

            {

                return mItem;

            }

            set

            {

                mItem = value;

            }

        }

 

        public void Add()

        {

            OnCheckModel();

            OnAdd();

        }

 

        public void Edit()

        {

            OnCheckModel();

            OnEdit();

        }

 

        public void Load(KT index)

        {

            OnLoad(index);

        }

 

        public void Delete()

        {

            OnCheckModel();

            OnDelete();

        }

 

        #endregion

 

        protected virtual void OnAdd()

        {

            DAOContext.Add(Item);

        }

 

        protected virtual void OnDelete()

        {

            DAOContext.Delete(Item);

        }

 

        protected virtual void OnEdit()

        {

            DAOContext.Edit(Item);

        }

 

        protected virtual void OnLoad(KT index)

        {

            mItem = DAOContext.Load<T>(index);

        }

        protected virtual void OnCheckModel()

        {

            if(Item == null)

                throw new LogicException("object is null!");

        }

 

     

    }

应用例程:

    public class User:BaseModel<Entities.SysUser,string>

    {

        protected override void OnAdd()

        {

            if ((Entities.DBMapping.SysUser.UserCode == Item.UserCode).CountOf() > 0)

            {

                throw (new HFSoft.BusinessLogic.LogicException(Properties.Resources.USER_HAVED));

            }

            base.OnAdd();

        }

        private bool mLoadRoles = false;

        /// <summary>

        /// 是否加载相关用户角色

        /// </summary>

        public bool LoadRoles

        {

            get

            {

                return mLoadRoles;

            }

            set

            {

                mLoadRoles = value;

            }

        }

        private List<Entities.Role> mRoles = new List<IDC.Entities.Role>();

        public List<Entities.Role> Roles

        {

            get

            {

                return mRoles;

            }

 

        }

        protected override void OnLoad(string index)

        {

            base.OnLoad(index);

            if (LoadRoles)

            {

                mRoles = (Entities.DBMapping.Role.RoleUser == (

                    Entities.DBMapping.UserRole.UserCode == (string)index)).List<Entities.Role>();

            }

        }

 

    }


posted on 2006-10-07 14:45  henry  阅读(846)  评论(0编辑  收藏  举报

导航