1.3 菜单管理 (系统开发架构与设计步步谈)

 

1.3  菜单管理

一个系统开发一段时间以后,或者用户试用一段时间以后,会发现,原来有些页面不用了,有些菜单不需要了。在我们开发过程中都会有开发环镜、测试环镜、正式环镜,如果菜单条目经常变动,保持菜单及文件在各个环镜同步是个比较麻烦的事情,特别是那些前期项目需求分析的不到位的项目,一会删掉一个,一会新增一个。对此我们的系统需要额外的辅助工具来做这件事情

通常情况下,菜单表都设计为自关联表,如下表

 

字段名

类型

Id

Varchar(20)

Parented

Varchar(20

url

Varchar(50)

isUsed

bit

 

此处的id不可用自增的形式,应为程序生成的序列号,以方便日后生成菜单脚本

一般C#程序显示菜单示例代码为,源自《ASP.NET2.0 Unleashed

 /// <summary>

    /// Get the data from the database and create the top-level

    /// menu items

    /// </summary>

    private void PopulateMenu()

    {

        DataTable menuData = GetMenuData();

        AddTopMenuItems(menuData);

    }

   

    /// <summary>

    /// Use a DataAdapter and DataTable to grab the database data

    /// </summary>

    /// <returns></returns>

    private DataTable GetMenuData()   

    {

        // Get Categories table

        string selectCommand = "SELECT CategoryId,ParentId,Name FROM Categories";

        string conString = WebConfigurationManager.ConnectionStrings["Categories"].ConnectionString;

        SqlDataAdapter dad = new SqlDataAdapter(selectCommand, conString);

        DataTable dtblCategories = new DataTable();

        dad.Fill(dtblCategories);

        return dtblCategories;

    }

 

    /// <summary>

    /// Filter the data to get only the rows that have a

    /// null ParentID (these are the top-level menu items)

    /// </summary>

    private void AddTopMenuItems(DataTable menuData)

    {

        DataView view = new DataView(menuData);

        view.RowFilter = "ParentID IS NULL";

        foreach (DataRowView row in view)

        {

            MenuItem newMenuItem = new MenuItem(row["Name"].ToString(), row["CategoryId"].ToString());

            Menu1.Items.Add(newMenuItem);

            AddChildMenuItems(menuData, newMenuItem);

        }

 

    }

 

    /// <summary>

    /// Recursively add child menu items by filtering by ParentID

    /// </summary>

    private void AddChildMenuItems(DataTable menuData, MenuItem parentMenuItem)

    {

        DataView view = new DataView(menuData);

        view.RowFilter = "ParentID=" + parentMenuItem.Value;

        foreach (DataRowView row in view)

        {

            MenuItem newMenuItem = new MenuItem(row["Name"].ToString(), row["CategoryId"].ToString());

            parentMenuItem.ChildItems.Add(newMenuItem);

            AddChildMenuItems(menuData, newMenuItem);

        }

    }

自关联表需要递归算法。

到这里实现了菜单的设计,还没有解决开始提出来的问题,保持测试环镜、正式环镜同步。实现的思路很多,我这里想说的就有两种。

一种是不用数据库设计菜单了,而是所有菜单只保存在一个XML文件中,每次变更只需要变更XML文件即可,去更新测试环镜、生产环镜也只要更新一个菜单文件即可

另一种用数据库设计菜单,如果由于设计变更,删除以前的菜单和新增了新的菜单,可以做门的开发辅助工具完成,再针对目前的菜单生成SQL脚本,用脚本来更新测试、生产环镜,如果开发辅助工具要是做的更好,可以专门在项目里做个页面来做更新菜单功能。更新的思路是删除以前的菜单,然后再完整新增菜单条目。生成脚本的存储过程可以参考我以前的日志 SQL Server 2008 生成数据脚本》

原文地址:https://www.cnblogs.com/zycblog/p/1968746.html