数据模型,一般在开发的时候为一个Model,在开发的过程中一般跟表走。有交互表的时候一般就是生成一个 表By表 的新实体。我设计时有个疑惑:我有角色、用户组、组织结构3个实体对象,他们都有层级关系,即:角色可以包含角色、用户组可以包含用户组、组织结构可以包含组织结构因此我在设计的时候想这样设计关系,代码如下(代码是最好的图示,希望能看懂)。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ModelDesign
{
    #region 数据实体层
    /// <summary>
    /// 基础模型
    /// </summary>
    [Serializable]
    public class BaseModel
    {
        private int parentID;
        private int currentID;        public int ParentID
        {
            get { return parentID; }
            set { parentID = value; }
        }        public int CurrentID
        {
            get { return currentID; }
            set { currentID = value; }
        }
    }    /// <summary>
    /// 角色实体对象
    /// </summary>
    [Serializable]
    public class Role : BaseModel
    {
        private string roleName;
        private string roleRe;        /// <summary>
        /// 角色名称
        /// </summary>
        public string RoleName
        {
            get { return roleName; }
            set { roleName = value; }
        }        /// <summary>
        /// 角色备注
        /// </summary>
        public string RoleRe
        {
            get { return roleRe; }
            set { roleRe = value; }
        }
    }    /// <summary>
    /// 用户组实体对象
    /// </summary>
    [Serializable]
    public class UserGroup : BaseModel
    {
        private string groupName;
        private string groupAdmin;
        private string groupRe;        /// <summary>
        /// 用户组名称
        /// </summary>
        public string GroupName
        {
            get { return groupName; }
            set { groupName = value; }
        }        /// <summary>
        /// 用户组管理员
        /// </summary>
        public string GroupAdmin
        {
            get { return groupAdmin; }
            set { groupAdmin = value; }
        }        /// <summary>
        /// 用户组备注
        /// </summary>
        public string GroupRe
        {
            get { return groupRe; }
            set { groupRe = value; }
        }
    }    /// <summary>
    /// 层级关系组装对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Serializable]
    public class LevelModel<T> where T : BaseModel, new()
    {
        private T baseModel = new T();
        private List<LevelModel<T>> levelModelList = new List<LevelModel<T>>();        /// <summary>
        /// 基础对象实体
        /// </summary>
        public T BaseModel
        {
            get { return baseModel; }
            set { baseModel = value; }
        }        /// <summary>
        /// 包含的子对象
        /// </summary>
        public List<LevelModel<T>> LevelModelList
        {
            get { return levelModelList; }
            set { levelModelList = value; }
        }        //可以自定义实现一些处理,如下:实现查找出某个子对象列表
        /// <summary>
        /// 自定义索引器
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public List<LevelModel<T>> this[int parentID]
        {
            get
            {
                return LoopFind(this, parentID);
            }
        }        /// <summary>
        /// 查找当前节点的子项
        /// </summary>
        /// <param name="findModel"></param>
        /// <param name="parentID"></param>
        /// <returns></returns>
        private List<LevelModel<T>> LoopFind(LevelModel<T> findModel, int parentID)
        {
            List<LevelModel<T>> results = null;
            if (findModel.BaseModel.CurrentID == parentID)
            {
                results = findModel.LevelModelList;
            }
            else
            {
                foreach (LevelModel<T> objLeveModel in findModel.LevelModelList)
                {
                    results = LoopFind(objLeveModel, parentID);
                    if (results != null)
                        break;
                }
            }
            return results;
        }
    }
    #endregion    #region 业务实现层
    /// <summary>
    /// 定义加载当前对象的方法
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="loopObject"></param>
    /// <param name="para"></param>
    public delegate void LoadLevelModelByCurrent<T>(T loopObject, params string[] para) where T : BaseModel, new();    /// <summary>
    /// 定义加载子节点对象的方法
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="loopObject"></param>
    /// <param name="para"></param>
    /// <returns></returns>
    public delegate List<T> LoadLevelModelByChild<T>(T loopObject, params string[] para) where T : BaseModel, new();    /// <summary>
    /// 组装业务逻辑
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LoadLevelModel<T> where T : BaseModel, new()
    {
        private LoadLevelModelByCurrent<T> loadObjectByCurrent;        public LoadLevelModelByCurrent<T> LoadObjectByCurrent
        {
            get { return loadObjectByCurrent; }
            set { loadObjectByCurrent = value; }
        }        private LoadLevelModelByChild<T> loadObjectByChild;        public LoadLevelModelByChild<T> LoadObjectByChild
        {
            get { return loadObjectByChild; }
            set { loadObjectByChild = value; }
        }        /// <summary>
        /// 返回加载完成后的对象
        /// </summary>
        /// <param name="loopObject"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public LevelModel<T> GetLoopObjectInfo(T levelModel, params string[] para)
        {
            LevelModel<T> results = new LevelModel<T>();
            if (loadObjectByCurrent != null)
            {
                loadObjectByCurrent(levelModel, para);
            }
            results.BaseModel = levelModel;
            loopLoad(results, para);
            return results;
        }        /// <summary>
        /// 递归加载
        /// </summary>
        /// <param name="results"></param>
        /// <param name="para"></param>
        private void loopLoad(LevelModel<T> results, params string[] para)
        {
            if (loadObjectByChild != null)
            {
                List<T> childList = loadObjectByChild(results.BaseModel, para);
                foreach (T child in childList)
                {
                    LevelModel<T> newChild = new LevelModel<T>();
                    newChild.BaseModel = child;
                    loopLoad(newChild, para);
                    results.LevelModelList.Add(newChild);
                }
            }
        }
    }    public class RoleBusiness
    {
        public LevelModel<Role> GetRole(Role queryRole, params string[] para)
        {
            LoadRoleInfo loadRole = new LoadRoleInfo();
            LoadLevelModel<Role> loadLevelModel = new LoadLevelModel<Role>();
            loadLevelModel.LoadObjectByChild = loadRole.GetRoleByChild;
            loadLevelModel.LoadObjectByCurrent = loadRole.GetRoleByCurrent;            return loadLevelModel.GetLoopObjectInfo(queryRole, para);
        }
    }
    #endregion    #region 数据访问层
    /// <summary>
    ///  处理角色获取数据的处理
    /// </summary>
    public class LoadRoleInfo
    {
        private string[][] roleList = new string[][] 
        { new string[]{ "1", "0", "角色01", "01的描述" }, 
          new string[]{ "2", "0", "角色02", " 01的描述" },
          new string[]{ "3", "1", "角色11", "11的描述" } ,
          new string[] {"4","1","角色12","12的描述"},
          new string[] {"5","2","角色21","21的描述"},
          new string[] {"6","2","角色22","22的描述"}
        };        /// <summary>
        /// 定义了获取自身方法,以满足获取单独的菜单内容
        /// </summary>
        /// <param name="menuInfo"></param>
        /// <returns></returns>
        public void GetRoleByCurrent(Role roleInfo, params string[] para)
        {
            foreach (string[] str in roleList)
            {
                if (int.Parse(str[0]) == roleInfo.ParentID)
                {
                    roleInfo.CurrentID = int.Parse(str[0]);
                    roleInfo.ParentID = int.Parse(str[1]);
                    roleInfo.RoleName = str[2];
                    roleInfo.RoleRe = str[3];
                }
            }
        }        public List<Role> GetRoleByChild(Role roleInfo, params string[] para)
        {
            List<Role> childRoleList = new List<Role>();
            foreach (string[] str in roleList.Where(o => (int.Parse(o[1]) == roleInfo.CurrentID)))
            {
                Role role = new Role();
                role.CurrentID = int.Parse(str[0]);
                role.ParentID = int.Parse(str[1]);
                role.RoleName = str[2];
                role.RoleRe = str[3];
                childRoleList.Add(role);
            }
            return childRoleList;
        }
    }
    #endregion    #region 客户端调用
    public class OutputTest
    {
        public static void Main()
        {
            //定义查找条件
            Role role = new Role();
            role.CurrentID = 0;
            //调用查找业务
            RoleBusiness roleBussiness = new RoleBusiness();
            LevelModel<Role> roleInfo = roleBussiness.GetRole(role, null);
            //显示查找结果
            Console.WriteLine(roleInfo.LevelModelList.Count);
            Console.WriteLine(roleInfo.LevelModelList[0].BaseModel.RoleName);
            Console.Read();
        }    }
    #endregion
}

解决方案 »

  1.   


    我设计时产生的疑惑:
    1、LevelModel对象的定义是否合理?
    2、LevelModel我想在其中实现一些处理,这个符合Model层的设计理念么?
    3、有什么好的设计书籍,是业务建模层理解比较好的书籍可以推荐下
      

  2.   

    Model层主要在程序中作为数据载体使用,传递数据。
      

  3.   

    Model,我个人的理解,就是结构模型。
      

  4.   


    Model我觉得有两个理解:
    1、数据传递
    2、业务模型如果是数据传递的话,不一定要定义个Class,用键值对最好,如果是业务模型的话则包含业务
      

  5.   


    既然是结构,就可以有解析结构的方法了例如有一个Model对象为人,Person,有一个属性年龄age,我们一般都知道人的年龄肯定大于0的,也基本上没有大于150岁的,因此,我觉得在获取或设置age时候,就用自身进行验证,而不是在每个地方写一堆IF语句public class Person
    {
        private int _age=0;
        public int Age
        {
            get
            {
                if(150<_age<0)
                    throw new Exception("年龄实在错误");
                return _age;
            }
            set
            {
                if(150<_value<0)
                    throw new Exception("年龄实在错误");
                 _age=value;
            }
        }
        //年龄是否正确
        public bool IsOrderlyAge()
        {
            if(150<_age<0)
                return false;
            return true
        }
    }其实属性的处理也是一种逻辑,该逻辑只是不依赖于其他的对象,在这个时候可以有个方法,判断年龄是否合法,如上