CSLA.Net学习(1)——第一个小程序

CSLA是什么东西啊!项目需要,需要学习一下!

目前应用CSLA主要是为了验证数据,数据库开发的需要,要把程序结构分分层:数据实体Models、通用数据库操作Helper、数据操作DAL、业务逻辑BIL、系统界面UI;

应用CSLA开发的第一个测试程序,Csla版本为4.3.10.0,好像和3.X版本的区别还是蛮大的:

运行结果:

CSLA.Net学习(1)——第一个小程序_第1张图片

 

首先需要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中进行的!这样的坏处是没有办法与界面分离。但是数据绑定的机制确实很复杂!!

接下来要和数据库结合起来开发!

你可能感兴趣的:(.net)