Hi,
I am confused and could not understand which CSLA version I am using. following is code snapet -
Imports Csla
Imports Csla.Data
Imports System.Data
Imports System.Data.SqlClient
<Serializable()> _
Public Class City
Inherits Csla.BusinessBase(Of City)
#Region " Variable Declarations "
Private mID As GuID = New Guid
Private mName As String = ""
#End Region
#Region " Business Properties and Methods "
Protected Overrides Function GetIdValue() As Object
Return mID
End Function
Public ReadOnly Property ID() As Guid
Get
Return mID
End Get
End Property
Public Property Name() As String
Get
Return mName
End Get
Set(ByVal Value As String)
If mName <> Value Then
mName = Trim(Value)
MarkDirty()
End If
End Set
End Property
Public Overrides ReadOnly Property IsValid() As Boolean
Get
ValidationRules.CheckRules()
Return MyBase.IsValid
End Get
End Property
Public Overrides ReadOnly Property IsDirty() As Boolean
Get
Return MyBase.IsDirty
End Get
End Property
#End Region
#Region " BusinessRules "
Protected Overrides Sub AddBusinessRules()
ValidationRules.AddRule(AddressOf NameValidation, "Name")
End Sub
Private Function NameValidation(ByVal target As Object, ByVal e As Validation.RuleArgs) As Boolean
If Len(Trim(mName)) = 0 Then
e.Description = "City Name required."
Return False
ElseIf Len(Trim(mName)) > 150 Then
e.Description = "City Name should be less than or equal 150 Characters."
Return False
Else
Return True
End If
End Function
#End Region
#Region " Shared Methods "
'Create new object
Public Shared Function NewCity(ByVal ID As Guid) As City
Return CType(DataPortal.Create(New Criteria(ID)), City)
End Function
Public Shared Function NewCity() As City
Return CType(DataPortal.Create(New Criteria(Guid.NewGuid)), City)
End Function
' Used for DataTransfer
Public Shared Function NewCity(ByVal ID As Guid, ByVal Name As String) As City
Return CType(DataPortal.Create(New Criteria(ID, Name)), City)
End Function
Public Shared Function NewCityChild(ByVal ID As Guid) As City
Dim child As New City
child.Create(ID)
Return child
End Function
'Used to create Child to be updated in Bulk.
Public Shared Function NewCityChild() As City
Dim child As City = New City
child.Create(New Criteria(Guid.NewGuid))
Return child
End Function
'Used to create Child to be updated in Bulk.
Public Shared Function NewCityChild(ByVal ID As Guid, ByVal Name As String) As City
Dim child As City = New City
child.Create(New Criteria(ID, Name))
Return child
End Function
'Load existing object by ID
Public Shared Function GetCity(ByVal ID As Guid) As City
Return CType(DataPortal.Fetch(New Criteria(ID)), City)
End Function
'Load existing object by Name.
Public Shared Function GetCity(ByVal Name As String) As City
Return CType(DataPortal.Fetch(New Criteria(Name)), City)
End Function
' Load existing object by ID
Public Shared Function GetCityChild(ByVal dr As SafeDataReader) As City
Dim child As City = New City
child.Fetch(dr)
Return child
End Function
'Delete object
Public Shared Sub DeleteCity(ByVal ID As Guid)
Try
DataPortal.Delete(New Criteria(ID))
Catch ex As Exception
If (InStr(ex.InnerException.InnerException.Message, "FK", CompareMethod.Text) > 0) Then
Throw New Exception("Record in use. Cannot delete record.", ex.InnerException.InnerException)
Else
Throw ex
End If
End Try
End Sub
#End Region
#Region " Constructor "
Private Sub New()
'Prevent direct instantiation
End Sub
#End Region
#Region " Criteria "
'Criteria for identifying existing object
<Serializable()> _
Private Class Criteria
Public ID As Guid
Public Name As String
Public Sub New(ByVal ID As Guid)
'This is used to create blank object with ID.
Me.ID = ID
Me.Name = ""
End Sub
Public Sub New(ByVal ID As Guid, ByVal Name As String)
'This can be used for DataTransfer.
Me.ID = ID
Me.Name = Name
End Sub
Public Sub New(ByVal Name As String)
'This can be used to get City by Name
Me.ID = Guid.Empty
Me.Name = Name
End Sub
End Class
#End Region
#Region " Data Access "
Protected Overrides Sub DataPortal_Create(ByVal Criteria As Object)
Dim crit As Criteria = CType(Criteria, Criteria)
mID = crit.ID
mName = crit.Name
End Sub
'To be accessed to create child.
Protected Sub Create(ByVal Criteria As Object)
Dim crit As Criteria = CType(Criteria, Criteria)
mID = crit.ID
mName = crit.Name
MarkAsChild()
End Sub
Protected Overrides Sub DataPortal_Fetch(ByVal Criteria As Object)
'Retrieve data from db
Dim crit As Criteria = CType(Criteria, Criteria)
Dim cn As New SqlConnection(Database.JadeASPLConnection)
Dim cm As New SqlCommand
Dim tr As SqlTransaction
cn.Open()
Try
tr = cn.BeginTransaction(IsolationLevel.ReadCommitted)
With cm
.Connection = cn
.Transaction = tr
.CommandType = CommandType.StoredProcedure
.CommandText = "CityFetch"
'We are getting the Record
.Parameters.AddWithValue("@ID", crit.ID)
Dim dr As New SafeDataReader(.ExecuteReader)
Try
If dr.Read() Then
With dr
mID = .GetGuid(0)
mName = .GetString(1)
End With
End If
Catch ex As Exception
Throw ex.GetBaseException
Finally
dr.Close()
End Try
End With
tr.Commit()
MarkOld()
Catch ex As Exception
tr.Rollback()
Throw ex.GetBaseException
Finally
cn.Close()
End Try
End Sub
Protected Overrides Sub DataPortal_Insert()
'save data into db
Dim cn As New SqlConnection(Database.JadeASPLConnection)
Dim cm As New SqlCommand
Dim tr As SqlTransaction
cn.Open()
Try
tr = cn.BeginTransaction(IsolationLevel.Serializable)
With cm
.Connection = cn
.Transaction = tr
.CommandType = CommandType.StoredProcedure
If Me.IsDeleted Then
'We are being deleted
If Not Me.IsNew Then
'we are not new so get rid of our data
.CommandText = "CityDelete"
.Parameters.AddWithValue("@ID", mID)
.ExecuteNonQuery()
End If
'Reset our status to be a New object
MarkNew()
Else
'We are not being deleted ,so insert or updare
If Me.IsNew = True Then
'we are nem, so insert
.CommandText = "CityAdd"
Else
.CommandText = "CityUpdate"
End If
.Parameters.AddWithValue("@ID", mID)
.Parameters.AddWithValue("@Name", mName)
.ExecuteNonQuery()
'make sure we're marked as an old object
MarkOld()
End If
'Update child objects
tr.Commit()
End With
Catch ex As Exception
tr.Rollback()
Throw ex.GetBaseException
Finally
cn.Close()
End Try
End Sub
Protected Overrides Sub DataPortal_Update()
'save data into db
Dim cn As New SqlConnection(Database.JadeASPLConnection)
Dim cm As New SqlCommand
Dim tr As SqlTransaction
cn.Open()
Try
tr = cn.BeginTransaction(IsolationLevel.Serializable)
With cm
.Connection = cn
.Transaction = tr
.CommandType = CommandType.StoredProcedure
If Me.IsDeleted Then
'We're being deleted
If Not Me.IsNew Then
'We're not new , so get rod of our data
.CommandText = "CityDelete"
.Parameters.AddWithValue("@ID", mID)
.ExecuteNonQuery()
End If
'Reset our status to be a new object
MarkNew()
Else
'We're not being deleted so insert or update
If Me.IsNew Then
'We're new so insert
.CommandText = "CityAdd"
Else
'We're not new ,so update
.CommandText = "CityUpdate"
End If
.Parameters.AddWithValue("@ID", mID)
.Parameters.AddWithValue("@Name", mName)
.ExecuteNonQuery()
'Make sure we're marked as an old object
MarkOld()
End If
tr.Commit()
End With
Catch ex As Exception
tr.Rollback()
Throw ex.GetBaseException
Finally
cn.Close()
End Try
End Sub
Protected Overrides Sub DataPortal_Delete(ByVal Criteria As Object)
Dim crit As Criteria = CType(Criteria, Criteria)
Dim cn As New SqlConnection(Database.JadeASPLConnection)
Dim cm As New SqlCommand
Dim tr As SqlTransaction
cn.Open()
Try
tr = cn.BeginTransaction(IsolationLevel.Serializable)
With cm
.Connection = cn
.Transaction = tr
.CommandType = CommandType.StoredProcedure
.CommandText = "CityDelete"
.Parameters.AddWithValue("@ID", crit.ID)
.ExecuteNonQuery()
End With
tr.Commit()
Catch ex As Exception
tr.Rollback()
Throw ex.GetBaseException
Finally
cn.Close()
End Try
End Sub
#End Region
End Class
In Visual Studio, select Csla.dll in References and select properties.
The properties window will show the version number of Csla.
You can also use reflection in your code to get the AssemblyVersion and AssemblyFileVersion attributes.
Copyright (c) Marimer LLC