CSLA.Net学习(1)——第一个小程序
CSLA是什么東西啊!項目需要,需要學習一下!
目前應用CSLA主要是為了驗證數據,數據庫開發的需要,要把程序結構分分層:數據實體Models、通用數據庫操作Helper、數據操作DAL、業務邏輯BIL、系統界面UI;
應用CSLA開發的第一個測試程序,Csla版本為4.3.10.0,好像和3.X版本的區別還是蠻大的:
運行結果:
?
首先需要CSLA的類庫:Csla.dll,Csla.Windows.dll。
包括Drill類,DrillList類和窗體類From1。
Drill類:
Drill類 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.ComponentModel; 6 using System.Linq; 7 using Csla; 8 using Csla.Data; 9 using Csla.Security; 10 namespace Models 11 { 12 [Serializable] 13 public class Drill :BusinessBase<Drill> 14 { 15 #region Business Methods 16 17 private string aa; 18 private static PropertyInfo<int> HoleIDProperty = 19 RegisterProperty<int>(p => p.HoleID, "鉆孔編號"); 20 public int HoleID 21 { 22 get { return GetProperty(HoleIDProperty); } 23 set { SetProperty(HoleIDProperty, value); } 24 } 25 26 private static PropertyInfo<int> LayerIDProperty = 27 RegisterProperty<int>(p => p.LayerID, "分層編號"); 28 public int LayerID 29 { 30 get { return GetProperty(LayerIDProperty); } 31 set { SetProperty(LayerIDProperty, value); } 32 } 33 34 35 private static PropertyInfo<double> TopHeightProperty = 36 RegisterProperty<double>(p => p.TopHeight, "頂板高度"); 37 public double TopHeight 38 { 39 get { return GetProperty(TopHeightProperty); } 40 set { SetProperty(TopHeightProperty, value); } 41 } 42 43 44 public override string ToString() 45 { 46 return HoleID.ToString(); 47 } 48 49 #endregion 50 51 #region Business Rules 52 53 protected override void AddBusinessRules() 54 { 55 base.AddBusinessRules(); 56 57 BusinessRules.AddRule(new Csla.Rules.CommonRules.MinValue<int>(HoleIDProperty, 1)); 58 //BusinessRules.AddRule(new Csla.Rules.CommonRules.MinValue<int>(LayerIDProperty, 0)); 59 BusinessRules.AddRule(new Csla.Rules.CommonRules.MinValue<double>(TopHeightProperty, 0)); 60 BusinessRules.AddRule(new Csla.Rules.CommonRules.RegExMatch(LayerIDProperty, "^[0-9]*[1-9][0-9]*$", "要求類型為整形")); 61 } 62 63 #endregion 64 65 #region Factory Methods 66 67 public static Drill NewOrder() 68 { 69 return DataPortal.Create<Drill>(); 70 //return new Drill(); 71 } 72 73 public static Drill GetOrder(int id) 74 { 75 return DataPortal.Fetch<Drill>(id); 76 } 77 78 public static void DeleteOrder(int id) 79 { 80 DataPortal.Delete<Drill>(id); 81 } 82 83 public Drill() 84 { 85 MarkAsChild(); 86 } 87 public Drill(int holeid ,int layerid):this() 88 { 89 using (BypassPropertyChecks) 90 { 91 this.HoleID = holeid; 92 this.LayerID = layerid; 93 } 94 } 95 #endregion 96 protected override void AcceptChangesComplete() 97 { 98 System.Diagnostics.Debug.WriteLine(string.Format("Acc: {0} ({1}, {2})", HoleID, CurrentEditLevel, CurrentEditLevelAdded)); 99 base.AcceptChangesComplete(); 100 } 101 102 protected override void UndoChangesComplete() 103 { 104 System.Diagnostics.Debug.WriteLine(string.Format("Und: {0} ({1}, {2})", HoleID, CurrentEditLevel, CurrentEditLevelAdded)); 105 base.UndoChangesComplete(); 106 } 107 108 protected override void CopyStateComplete() 109 { 110 System.Diagnostics.Debug.WriteLine(string.Format("Beg: {0} ({1}, {2})", HoleID, CurrentEditLevel, CurrentEditLevelAdded)); 111 base.CopyStateComplete(); 112 } 113 #region Data Access 114 115 public int CurrentEditLevel 116 { 117 get 118 { 119 return EditLevel; 120 } 121 } 122 123 public int CurrentEditLevelAdded 124 { 125 get 126 { 127 Csla.Core.IEditableBusinessObject ebo = (Csla.Core.IEditableBusinessObject)this; 128 return ebo.EditLevelAdded; 129 } 130 } 131 #endregion 132 } 133 }DrillList類:
DrillList類 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Csla; namespace Models {class DrillList :BusinessBindingListBase<DrillList, Drill>{} }Form1窗體:
From1窗體 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Models; namespace MineGeologyV10._1._0 {public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){DrillList list = new DrillList();list.Add(new Drill(1, 1));list.Add(new Drill(2, 2));list.Add(new Drill(3, 3));list.Add(new Drill(4, 4));list.BeginEdit();this.bindingSource1.DataSource = list;this.bindingSource1.ListChanged += new ListChangedEventHandler(bindingSource1_ListChanged);this.dataGridView1.DataSource = bindingSource1;}void bindingSource1_ListChanged(object sender, ListChangedEventArgs e){System.Diagnostics.Debug.WriteLine(string.Format("{0}: {1}, {2}", e.ListChangedType.ToString(), e.NewIndex, e.OldIndex));}private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e){}} }這里發現一個問題:Csla實現數據datagridview中的驗證,如果是數據類型錯誤,DataGridView會彈出錯誤對話框,這時單元格沒有失去焦點,也就是說此時的驗證是在UI層的,沒有發生在BIL層,沒有通過Csla來實現驗證。而只有失去焦點,數據存入DrillList類對象時才進行業務邏輯的驗證。所有我將DataGridView的Data_Error事件處理了一下,這樣就不會彈出對話框了,只是讓光標無法離開單元格。
原來我的單元格驗證都是在DataGridView中進行的!這樣的壞處是沒有辦法與界面分離。但是數據綁定的機制確實很復雜!!
接下來要和數據庫結合起來開發!
轉載于:https://www.cnblogs.com/yhlx125/archive/2012/05/08/2480972.html
總結
以上是生活随笔為你收集整理的CSLA.Net学习(1)——第一个小程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C 语言中赋值表达式的返回的逻辑值
- 下一篇: 获得AP发票状态