权限设计 以及 树的存储

平时看到各位园子的朋友真的很厉害,设计了很多关于权限管理的东西,

很羡慕,但同时也觉得在一些小型项目上,那样的设计是否有点设计过度呢

其实这也说不清,可能是自己资历尚浅,还没看明白各位高人的设计

自己也写了个,帖出来请教下园子的各位朋友

用PD 弄了个图,

权限设计1

这里想说明的是关于部门表和模块表的 索引 字段

因为大家都知道部门是树结构的数据

那么怎样来体现这个树结构,成了很有意思的事情,

原来我的设想是 添加字段 “FatherElement”   ,记录父节点的id  

但是考虑到这个遍历表会遍历很多遍,效率貌似比较低

于是采用第二种方法

添加字段"ElementIndex" 之后编号规则为,父节点编号 + 双位自己的编号

考虑到方便处理,设置起始编号为10

如果不太明白的话,举个例子

博客园   里面有 新手区,精华区,新手区有这个blog 于是编号如下

------------------------------------------------------------

Name                                              Index

------------------------------------------------------------

博客园                                            10

博客园新手区                                 1010

博客园精华区                                 1011

博客园新手区的第一篇文章            101010

博客园新手区的第二篇文章            101011

------------------------------------------------------------

这样的话,当用select 语句的时候,读到一个记录后就可以以它的索引加"__"寻找它的字节点了,

写个递归,一个树就轻松加愉快的出来了。

 

这是自己做的个生成Ext中tree 所需要的代码。

 

欢迎博客园各位达人能给小弟一些意见,建议,本人在此先谢谢了。

 

 

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
 
 
/// <summary>
/// 在这里我使用了theModule.xsd作为我的数据提供,
///最后的筛选条件是 WHERE ModuleIndex LIKE @theIndex
/// <author>
/// ATPKING
/// </author>
/// <time>
/// 2008-6-5
/// </time>
/// </summary>
public class BuildModuleTree
{
    private StringBuilder JsonStr;
    public BuildModuleTree()
 
    {            }    /// <summary>
    /// 建立一个Json形式的字符串,来自于表Module,使用一个索引编号制定根节点。
    /// </summary>
    /// <param name="rootModuleIndex">根节点的编号</param>
    /// <returns></returns>
    public string buildModuleTree(string rootModuleIndex)
    {
        JsonStr = new StringBuilder();
        string theIndex = rootModuleIndex + "__";
//根据索引设计规则,它的子节点必然为它自身的节点再加两位
        
        theModule.ModuleDataTable themodule = this.theModule(theIndex);
//返回一个强类型的datatable
        this.buildTree(themodule);
        JsonStr.Remove(0, 9);
        return JsonStr.ToString();
    }
    /// <summary>
    /// 建立一个Json形式的字符串,来自于表Module,从根节点开始执照
    /// </summary>
    /// <returns></returns>
    public string buildModuleTree()
    {
        string temp = this.buildModuleTree("");
        return temp;
    }
 
 
    private void buildTree(theModule.ModuleDataTable themodule)
    {
        JsonStr.Append("children:[");//建立子结点
        foreach (theModule.ModuleRow row in themodule)
        {
            JsonStr.Append("{ ");
            
            JsonStr.Append("text:");//此为ModuleName
            JsonStr.Append("'" + row.ModuleName + "'");
 
            string tempIndex = row.ModuleIndex;//获取这个节点的索引
 
            theModule.ModuleDataTable tempModule = 
     this.theModule(tempIndex + "__");//获取此节点的子节点表
            if ( tempModule.Rows.Count == 0 )   //如果子节点表为空
            {
                JsonStr.Append(",leaf:true");   //则标记该节点为叶子节点
            }
            else
            {
                JsonStr.Append(",");            //如果该节点的子节点表不空,
                buildTree(tempModule);          //递归查询子节点
            }
            JsonStr.Append("},");
        }
        JsonStr.Remove(JsonStr.Length - 1, 1);
        JsonStr.Append("]");
    }
    /// <summary>
    /// 返回一个theModule的强模型
    /// </summary>
    /// <param name="tempIndex">子模块的索引</param>
    /// <returns></returns>
    private theModule.ModuleDataTable theModule(string tempIndex)
    {
        theModuleTableAdapters.ModuleTableAdapter temp1 = 
new theModuleTableAdapters.ModuleTableAdapter();
        return temp1.GetData(tempIndex);
    }
}
posted @ 2008-06-06 20:13  Atpking  阅读(1109)  评论(2编辑  收藏  举报