Here's a "basic" CSLA .NET 2.0 editable root class. It pretty much illustrates all the things you can do in a class under the upcoming version of the framework. In particular note the way validation rules are handled and all the transactional options in the DataPortal_XYZ methods (presumably you'd pick one for your app :) ).
A CustomerTypes class is also included at the bottom, illustrating how to implement a name-value list that works with data binding.
Imports CSLA
<Serializable()> _
Public Class Customer
Inherits BusinessBase(Of Customer)
#Region " Business Methods "
Private mID As Integer
Private mLastName As String = ""
Private mFirstName As String = ""
Private mLastActivity As SmartDate
Private mType As Integer
Private mCity As String = ""
Private Shared mCustomerTypes As CustomerTypes
Public Property City() As String
Get
If CanReadProperty() Then
Return mCity
Else
Throw New System.Security.SecurityException( _
"Property read not allowed")
End If
End Get
Set(ByVal value As String)
If CanWriteProperty() Then
If Not mCity.Equals(value) Then
mCity = value
PropertyHasChanged()
"Property write not allowed")
End Set
End Property
Public Shared ReadOnly Property CustomerTypes() As CustomerTypes
If mCustomerTypes Is Nothing Then
mCustomerTypes = Library.CustomerTypes.GetCustomerTypes
Return mCustomerTypes
Public ReadOnly Property ID() As Integer
Return mID
Public Property LastName() As String
Return mLastName
If mLastName <> value Then
mLastName = value.ToUpper
Public Property FirstName() As String
Return mFirstName
If mFirstName <> value Then
mFirstName = value
Public Property LastActivity() As String
Return mLastActivity.Text
If mLastActivity <> value Then
mLastActivity.Text = value
Public Property CustomerType() As Integer
Return mType
Set(ByVal value As Integer)
If mType <> value Then
mType = value
Public ReadOnly Property CustomerTypeText() As String
Return CustomerTypes.Value(mType)
#End Region
#Region " Business Rules "
Protected Overrides Sub AddBusinessRules()
AddRule(AddressOf Validation.CommonRules.StringRequired, _
"FirstName")
"LastName")
End Sub
#Region " Object ID Value "
Protected Overrides Function GetIdValue() As Object
End Function
#Region " Constructors "
Private Sub New()
' don't allow a Guest to see or change city data
AuthorizationRules.DenyRead("City", "Guest")
AuthorizationRules.DenyWrite("City", "Guest")
#Region " Factory Methods "
Public Shared Function NewCustomer() As Customer
Return DataPortal.Create(Of Customer)(Nothing)
Public Shared Function GetCustomer(ByVal id As Integer) As Customer
Return DataPortal.Fetch(Of Customer)(New Criteria(id))
Public Shared Sub DeleteCustomer(ByVal id As Integer)
DataPortal.Delete(New Criteria(id))
#Region " Criteria "
Private Class Criteria
Public ID As Integer
Public Sub New(ByVal id As Integer)
Me.ID = id
End Class
#Region " Data Access "
' Forced to run on client
Protected Overrides Sub DataPortal_Create(ByVal criteria As Object)
' get default values from db
' we get here via DataPortal.Create()
mID = New System.Random().Next(100, 999)
mType = CustomerTypes.DefaultKey
CheckRules()
' Runs on client or server based on DataPortal config
' get data from db
' we get here via DataPortal.Fetch()
Dim crit As Criteria = DirectCast(criteria, Criteria)
mID = crit.ID
mType = CustomerTypes.Key("Hybrid")
mLastName = "Lhotka"
mFirstName = "Rocky"
' COM+ transactions
Protected Overrides Sub DataPortal_Insert()
' insert data into db
' we get here via obj.Save()
' System.Transactions namespace
' update data in db
Using tr As New System.Transactions.TransactionScope
' do updates
tr.Complete()
End Using
' ADO.NET transactions
' do deferred delete of self
Using cn As New SqlClient.SqlConnection
Using tr As SqlClient.SqlTransaction = cn.BeginTransaction
' do delete
' No transactions
' do immediate delete based on criteria
' we get here via DataPortal.Delete()
======================================================
Public Class CustomerTypes
Inherits NameValueListBase(Of Integer, String)
Public Function DefaultKey() As Integer
Return 1
Public Function DefaultValue() As String
Return Value(DefaultKey)
Public Shared Function GetCustomerTypes() As CustomerTypes
Return DataPortal.Fetch(Of CustomerTypes) _
(New Criteria(GetType(CustomerTypes)))
Protected Overrides Sub DataPortal_Fetch(ByVal criteria As Object)
Me.IsReadOnly = False
Add(New NameValuePair(1, "Domestic"))
Add(New NameValuePair(2, "International"))
Add(New NameValuePair(3, "Hybrid"))
Me.IsReadOnly = True
Powered by: newtelligence dasBlog 2.0.7226.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2012, Marimer LLC
E-mail