設計時支持
當在Web頁面中設計好表格布局之后,運用Enterpris Solution提供的框架,以可視化的方式綁定數據。
首先,請在配置文件中添加如下的程序集引用,以方便框架運用反射找到當前項目所引用的實體層。
<appSettings> <add key="Assembly" value="E:\Solution\Enterprise Solution\Build\Benin.BusinessLogic.dll"/> </appSettings>
然后打開Visual Studio,在設計時面板中,選擇一個控件。
如上圖所示,選擇控件的DataBindingString屬性,在屬性面板中打開它。如果已經綁定了屬性,它會自動高亮顯示已經綁定的實體及其屬性。這個屬性的代碼設計,是這樣的
[Category(CategoryName.OPTIONS)] [DefaultValue("")] [Description("Data Binding")] [Editor(typeof(QueryBindingTypeDialogEditor), typeof(UITypeEditor))] public virtual string DataBindingString { get { object obj = XState["DataBindingString"]; return obj != null ? obj.ToString() : ""; } set { XState["DataBindingString"] = value; } }
如代碼所示,它提供了一個自定義的屬性編輯器,也就是上圖中我們看到的Query Builder,綁定屬性。
返回所需要綁定屬性的關鍵代碼如下所示,它讀取實體層程序集並返回用戶所選擇的屬性值
string path = "";
IWebApplication webApp = (IWebApplication)provider.GetService(typeof(IWebApplication)); Configuration config = webApp.OpenWebConfiguration(true); AppSettingsSection app = config.AppSettings; path = app.Settings["Assembly"].Value;
這幾句代碼的含義,從當前Web項目中打開Web.config配置文件,並找到文章開頭設置的實體層程序集。
代碼生成
基於模板的代碼生成器,例如Code Smith,給代碼生成帶來了極大的便利。Enterprise Solution相關的代碼生成,均以Code Smith模板完成。熟悉ASP.NET的語法,應該可以很快熟悉Code Smith的語法並對它的生產力感到滿意。
在最新版本的Code Smith 6.5中,支持.NET 3.x/4.0語法。還可以運用它的SDK,把代碼生成功能集成到自己的開發工具中。比如,我想制作一個批量代碼生成的工具,直接調用Code Smith的模板文件:
public void RunTemplate(string templateFile,string connectionString,string tableName,string targetFile) { CodeTemplateCompiler compiler = new CodeTemplateCompiler(templateFile); compiler.Compile(); if (compiler.Errors.Count == 0) { CodeTemplate template = compiler.CreateInstance(); DatabaseSchema database = new DatabaseSchema(new SqlSchemaProvider(), connectionString); TableSchema tableSchema = database.Tables[tableName]; //如果都是字符串,應該要考慮bool,int,object Dictionary<string, object> dic = new Dictionary<string, object>(); string[] paramterValues = rtfParameter.Lines; foreach (string parm in paramterValues) { if (!String.IsNullOrEmpty(parm)) { string[] values = Regex.Split(parm, "="); string key = values[0]; object para =values[1].ToString().Trim(); if (string.IsNullOrEmpty(values[1])) para = tableSchema; dic.Add(values[0], para); } } PropertyInfo[] pis = template.GetProperties(); foreach (PropertyInfo pi in pis) { object val=null; if(dic.TryGetValue(pi.Name,out val)) template.SetProperty(pi, val); } if(File.Exists(targetFile)) File.Delete(targetFile); using (StreamWriter writer = new StreamWriter(targetFile)) { template.Render(writer); writer.Flush(); } } }
這個方法可以為你的代碼生成帶來諸多方便。比如以下面的參數調用之,則可以產生多個代碼文件
RunTempate("Interface.cst","server=(local);uid=sa;pwd=holiday;database=TS","Employee","IEmployeeManager.cs) RunTempate("Manager.cst","server=(local);uid=sa;pwd=holiday;database=TS","Employee","EmployeeManager.cs);
每行產生一個代碼文件,接口層和數據訪問層立即產生完成。
數據字典
對於不變的數據字典,比如帳戶的借方和貸方,程序員每天的工作分類項:Bug或Enhacement。對於這類不變的數據字典,Enterprise Solution以下面的方式處理它們。
先來看界面層的代碼,看看最終的開發人員,需要以何種方式應用數據字典。以客戶組別為例子
<ext:DropDownList ID="ddlCustomerGroup" Label="Customer Group" runat="server"> </ext:DropDownList>
后台代碼如下所示,也相當的簡練
ddlCustomerGroup.InitializeValueListFromEnum(typeof(CustomeGroup));
在此,我並沒有直接擴展DropDownList的代碼,而是加入擴展方法,它的實現方法如下所示
public static class DataBindingHelper { public static void InitializeValueListFromEnum(this ExtAspNet.DropDownList dropDownList, Type enumType) { dropDownList.Items.Clear(); foreach (KeyValuePair<string, string> item in StringEnum.GetValueStringList(enumType)) { ExtAspNet.ListItem listitem = new ExtAspNet.ListItem(item.Value, item.Key); dropDownList.Items.Add(listitem); } } }
代碼的意圖相對簡單,依據參數的枚舉值分別產生ListItem達到生成數據字典項的目的。枚舉CustomerGroup的定義
public enum CustomeGroup { [StringValue("Partner")] [DisplayText("Partner")] Partner, [StringValue("Supplier")] [DisplayText("Supplier")] Supplier }
StringValue和DisplayText分別用於ListItem的ValueItem和TextItem,用於顯示和實際的值。
對於需要從數據庫中取出的數據字典,Enterprise Solution框架暫未提供很方便的方法來完成綁定。依照基本的方法,讀取字段值,綁定到DropDownList中,過程也不復雜。
ExtAspNet控件擴展
ExtAspNet是一套優秀的控件,用它可以快速開發基於ExtJs的應用。雖然功能相當完善,但在細微處,仍需要稍微做一些調整,以滿足框架的應用需要。
每一個需要綁定屬性的控件,均加入了下面的屬性,以方便綁定實體的屬性。
[Category(CategoryName.OPTIONS)] [DefaultValue("")] [Description("Data Binding")] [Editor(typeof(QueryBindingTypeDialogEditor), typeof(UITypeEditor))] public virtual string DataBindingString { get { object obj = XState["DataBindingString"]; return obj != null ? obj.ToString() : ""; } set { XState["DataBindingString"] = value; } }
其次,我需要TextBox的文本標簽是右對齊的,而不是左對齊,於是加入了下面的屬性。
//fieldLabel居右對齊的問題 OB.AddProperty("labelStyle", "text-align:right");
對於頁面中的每個選項卡頁面,我需要在它關閉時,彈出提示確認窗口,於是修改代碼
NODES.mainTabStrip.addTab({ 'id': tabID, 'url': url, 'title': title, 'closable': true, listeners: { 'beforeclose': conrirmTab } });
加了一個beforeclose確認,它的方法如下所示
function conrirmTab(e) { Ext.MessageBox.show({ title: 'Confirm', msg: 'Are you sure want to close <b>' + e.title + '</b> ?', buttons: Ext.MessageBox.YESNO, icon: Ext.MessageBox.QUESTION, fn: function (btn, text) { if (btn == 'yes') { NODES.mainTabStrip.remove(e); } } }); return false; }
更精確的,我們應該加在那些有數據修改的頁面,如果頁面被修改過,則彈出確認提示,否則可以直接關閉選項卡。
再來看看兩個比較實用的腳本,關閉當前選項卡和關閉所有選項卡
//關閉活動的選項卡 function closeActivePanel() { var tabpanel = Ext.getCmp('<%= mainTabStrip.ClientID %>'); var _activeTab = tabpanel.getActiveTab(); if (_activeTab.x_iframe_url == "/Management/admin/default.aspx") return; tabpanel.remove(_activeTab); } //關閉所有選項卡 function closeAllPanel(){ var tabpanel = Ext.getCmp('<%= mainTabStrip.ClientID %>'); var _activeTab = undefined; for(var i=0;i< tabpanel.items.length;i++) { _activeTab = tabpanel.items.items[i]; if (_activeTab.x_iframe_url!=undefined && _activeTab.x_iframe_url == "/Management/admin/default.aspx") continue; tabpanel.remove(_activeTab); i = 0; } tabpanel.doLayout(); }
如果需要在后台代碼中調用,可以這樣調用之
string tab = "closeAllPanel();"; PageContext.RegisterStartupScript(tab);
還有一些控制項,有的還未找到實現方法,一並列舉如下
1 控制TextBox的輸入長度。比如maxLength=6,只允許輸入6個字符,第7個字符無法輸入。當前控件仍然可以輸入,只是會有驗證提示錯誤。
2 只允許大寫字母輸入。有的控件,比如用戶名,在C#.NET里面,MIS和mis的字母相同,但是比較equal時,如果不設置忽略大小寫,則它們的值不等,但是在數據庫里面,它們是相同的值。為避免這種情況,我需要設置控件只允許輸入大寫字母,用戶如果輸入的是小寫,則轉化自動為大寫。
3 對於Tab.cs #267行,這一行代碼會有異常。如果前台JS打開多個選項卡,在頁面刷新時,會拋出IndexOutOfRange異常,它的代碼如下所示
// 對於非激活Tab,其中的Iframe需要延遲加載 if (this != tabStrip.Tabs[tabStrip.ActiveTabIndex]) { OB.RemoveProperty("html"); OB.RemoveProperty("x_iframe_loaded"); OB.AddProperty("x_iframe_loaded", false); }
ActiveTabIndex的值是2或3,但是當前只有一個tab選項卡,於是拋出索引越界異常。
4 PageRefresh時,會關閉前台頁面打開的選項卡。需要用cookie記住已經打開的選項卡,在PageRefresh后,再打開被迫關閉的選項卡,以減少用戶困擾。
5 對ascx用戶控件的支持。新版本的ExtAspNet已經內置了此功能。如果仍使用的是3.1.8版本,需要這樣
foreach (UserGroupMenuTypeEntity usermenuType in menu.UserGroupMenuTypes) { function c = LoadControl("function.ascx") as function; c.LineBreakPlaceHolder.Text =GetTranslation(usermenuType.Description.Replace("&", "")); c.DataList1.DataSource = usermenuType.UserGroupMenuDetails; c.DataList1.DataBind(); ph.Controls.Add(c); Literal liter = new Literal(); liter.Text = "<br />"; ph.Controls.Add(liter); }
加載用戶自定義控件。因為Web不同於WinForms,Web的類型定義並不包含資源,圖片,控件,還必須引用頁面ASPX/ASCX文件,C/S類型的繼承則包含類型的方法,及其界面控件,相對方便很多。
6 Excel導入導出的支持。官方的例子中已經包含EXCEL導出功能,但沒有導入功能,仍然需要提供Web方式下的EXCEL數據導入功能。