CSLA.Net学习(1)——第一个小程序
CSLA是什么東西啊!項(xiàng)目需要,需要學(xué)習(xí)一下!
目前應(yīng)用CSLA主要是為了驗(yàn)證數(shù)據(jù),數(shù)據(jù)庫開發(fā)的需要,要把程序結(jié)構(gòu)分分層:數(shù)據(jù)實(shí)體Models、通用數(shù)據(jù)庫操作Helper、數(shù)據(jù)操作DAL、業(yè)務(wù)邏輯BIL、系統(tǒng)界面UI;
應(yīng)用CSLA開發(fā)的第一個測試程序,Csla版本為4.3.10.0,好像和3.X版本的區(qū)別還是蠻大的:
運(yùn)行結(jié)果:
?
首先需要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){}} }這里發(fā)現(xiàn)一個問題:Csla實(shí)現(xiàn)數(shù)據(jù)datagridview中的驗(yàn)證,如果是數(shù)據(jù)類型錯誤,DataGridView會彈出錯誤對話框,這時單元格沒有失去焦點(diǎn),也就是說此時的驗(yàn)證是在UI層的,沒有發(fā)生在BIL層,沒有通過Csla來實(shí)現(xiàn)驗(yàn)證。而只有失去焦點(diǎn),數(shù)據(jù)存入DrillList類對象時才進(jìn)行業(yè)務(wù)邏輯的驗(yàn)證。所有我將DataGridView的Data_Error事件處理了一下,這樣就不會彈出對話框了,只是讓光標(biāo)無法離開單元格。
原來我的單元格驗(yàn)證都是在DataGridView中進(jìn)行的!這樣的壞處是沒有辦法與界面分離。但是數(shù)據(jù)綁定的機(jī)制確實(shí)很復(fù)雜!!
接下來要和數(shù)據(jù)庫結(jié)合起來開發(fā)!
轉(zhuǎn)載于:https://www.cnblogs.com/yhlx125/archive/2012/05/08/2480972.html
總結(jié)
以上是生活随笔為你收集整理的CSLA.Net学习(1)——第一个小程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C 语言中赋值表达式的返回的逻辑值
- 下一篇: asp.net网站安全常见问题与防范