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 roleRemark; /// <summary> /// 角色名称 /// </summary> public string RoleName { get { return roleName; } set { roleName = value; } } /// <summary> /// 角色备注 /// </summary> public string RoleRemark { get { return roleRemark; } set { roleRemark = value; } } } /// <summary> /// 用户组实体对象 /// </summary> [Serializable] public class UserGroup : BaseModel { private string groupName; private string groupAdmin; private string groupRemark; /// <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 GroupRemark { get { return groupRemark; } set { groupRemark = 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.RoleRemark = 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.RoleRemark = 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、该不该这样设计? 2、要怎么样设计才好? 3、这样设计会出现什么问题?