蘭萍
摘 要: 通過樹型菜單,用戶可以對資源的結(jié)構(gòu)、類型有直觀的了解,但目前很多B/S系統(tǒng)的菜單還是通過代碼生成的。為此,基于.NET技術(shù),通過開源項目PetaPoco實現(xiàn)代碼,設(shè)計并實現(xiàn)了動態(tài)導(dǎo)航菜單系統(tǒng);介紹了數(shù)據(jù)庫設(shè)計、算法思想。該技術(shù)避免了靜態(tài)目錄樹下因數(shù)據(jù)源經(jīng)常性改變而頻繁修改程序代碼的弊端,導(dǎo)航菜單通過數(shù)據(jù)庫進行控制,提高了系統(tǒng)的安全性,使管理變得更加靈活、高效。
關(guān)鍵詞: 動態(tài)導(dǎo)航菜單; PetaPoco; 數(shù)據(jù)庫設(shè)計; 算法
中圖分類號:TP301 文獻標(biāo)志碼:A 文章編號:1006-8228(2015)02-42-03
Design and implementation of dynamic navigation menu based on .NET
Lan Ping
(Computer department, Shanghai information technology college, Shanghai 200331)
Abstract: Through the menu tree, the user can understand structure and types of the resources. Although many current B/S system menus are generated by code. A dynamic navigation menu based on ASP.NET is introduced. The code is mainly realized through the open source project PetaPoco. The database design and algorithm are introduced. The techniques have avoided the complex modification program code resulting from the change of data source under static catalog tree. The navigation menu is controlled by database which improves security and makes the management more flexible and efficient.
Key words: dynamic navigation menu; PetaPoco; database design; algorithm
0 引言
樹型菜單使用比較廣泛,比如我們熟悉的資源管理器就是典型的樹型菜單。它在頁面當(dāng)中可以起到導(dǎo)航器的作用。通過樹型菜單,用戶可以對資源的結(jié)構(gòu)、類型有一個直觀的了解[1]。網(wǎng)站的開發(fā)技術(shù)多種多樣的,比如ASP.NET、JSP、PHP等,但無論采用何種技術(shù),一般而言導(dǎo)航菜單都是網(wǎng)頁中必不可少的一部分。導(dǎo)航菜單通常是將相應(yīng)的代碼寫入頁面中的某一個區(qū)域中來實現(xiàn),但是這樣的網(wǎng)站缺乏可維護性[2]。為了解決這個問題,本文介紹了基于ASP.NET技術(shù)的動態(tài)導(dǎo)航菜單的設(shè)計與實現(xiàn)方法。
1 通過Nuget添加petaPoco的引用
1.1 PetaPoco的介紹及添加
Nuget是一個.NET平臺下的開源的項目,它是Visual Studio的擴展。在使用Visual Studio開發(fā)基于.NET Framework的應(yīng)用時,Nuget能把在項目中添加、移除和更新引用的工作變得更加快捷方便。Nuget是ASP.NET Gallery的一員。NuGet是免費、開源的包管理開發(fā)工具,專注于在.NET應(yīng)用開發(fā)過程中,簡單地合并第三方的組件庫[3]。
PetaPoco是一款適用于.Net和Mono的微小、快速、單文件的微型對象關(guān)系映射器(ORM, Object Relational Mapper)。與那些功能完備的ORM(如NHibernate或Entity Framework)不同的是,PetaPoco更注重易用性和性能,而非豐富的功能。使用PetaPoco只需要引入一個C#文件,就可以使用強類型的 POCO(Plain Old CLR Object),并支持使用T4模板生成的類等等。
PetaPoco最讓人感興趣的功能包括:
⑴ 可與SQL Server、SQL Server CE、MySQL、PostgreSQL以及Oracle數(shù)據(jù)庫協(xié)同工作;
⑵ 包含針對Insert/Delete/Update/Save以及IsNew的多個輔助方法;
⑶ 支持簡單事務(wù);
⑷ 對于翻頁請求會自動計算總記錄數(shù),并獲取特定分頁;
⑸ 支持參數(shù)替換,能夠從對象屬性中抓取命名參數(shù)(named parameters);
⑹ 包括一個消耗資源很少的SQL Builder類;
⑺ 部分記錄更新;
⑻ 包括T4 Templates,可以用于基于數(shù)據(jù)庫結(jié)構(gòu)生成POCO類。
由于PetaPoco是開源項目,因此添加條件來處理如空間數(shù)據(jù)等特定情況會很容易。
某項目采用微軟的Visual Studio 2012開發(fā)平臺,為上海一家園林公司開發(fā),新建項目(不是新建網(wǎng)站)-->asp.net web窗體應(yīng)用程序,自動添加很多包。
在項目名上單擊右鍵,在彈出式菜單中單擊“管理nuget程序包”,添加“petapoco-a tiny ormish thing for your pocos”包。
1.2 web.config的配置
在web.config文件中加入
database=XingLu_IMS;"providerName="System.Data.SqlClient"/>
1.3 DataBase.tt的參數(shù)設(shè)置
打開DataBase.tt,修改ConnectionStringName,以及Namespace和RepoName。
ConnectionStringName="DefaultConnection";
Namespace="YC.PetaPoco";
RepoName="DBUtils";
保存之后VS會自動執(zhí)行模板文件,并生成實體類文件: Database.cs。如果沒有生成成功,把VS關(guān)閉重新打開即可。
2 數(shù)據(jù)庫設(shè)計
DBMS采用微軟的SQL Server 2012,表名為Qs_Xl_modules,Qs_Xl_modules數(shù)據(jù)庫表的設(shè)計[4]如表1所示。
3 算法思想
⑴ 執(zhí)行SQL查詢語句返回Parentid字段為0(為0表示菜單項是一級導(dǎo)航菜單)的數(shù)據(jù)集,并按Displayorder字段升序排列;
⑵ 利用for語句循環(huán)添加Parentid字段為0的菜單項,每一項為表格
"img/caidan_2.jpg" id="menu" runat="server"> |
部分核心動態(tài)代碼[6]:
……
using YC.PetaPoco; //導(dǎo)入該命名空間
……
public void Show(string str)
{ List
* from Qs_Xl_modules where Parentid=0 and ShowState=1 and Class='"+ str+"' order by Displayorder");
string content="
?; | ?; |
?; | "+module.Modulename+" |
+(i==0 ? "":" style=\"display:none;\"")+">
href=\""+list1[j].Linkurl+"\" class=\"menu-two2\">
?;?;
16\" height=\"15\" />"+list1[j].Modulename+"