EditableRootListBase
Old forum URL: forums.lhotka.net/forums/t/2972.aspx
dantes posted on Friday, June 01, 2007
Hi... im new with CSLA 2.1...
im using the EditableRootListBase using DataGridView... when i was adding a new item... i could add the first one... but when i add another item... i cant add the second item... why is that???
RockfordLhotka replied on Friday, June 01, 2007
Do your objects have unique id values. Specifically, does GetIdValue() always return a value that is unique within the list. This is mandatory for all CSLA lists (BusinessListBase or EditableRootListBase).dantes replied on Sunday, June 03, 2007
hi... tnx for replying...
yeah my businessbase has a userid...
in your book on CSLA 2.1 it says that the code for the EditableRootListBase would just be simple coz it is already automatic... i may not have understood it that clearly...
the factory methods i implemented are the following:
AddNewCore()
GetList()
New()
are there any more methods i need to implement...??? tnx...
dantes replied on Sunday, June 03, 2007
hi... ive dug deeper on it... on the first time i add an item... after going through the process of adding an empty object... then on the data grid view control i typped in the needed data... and after selecting another grid... the EditableRootListBase calls the Protected Overrides Sub DataPortal_Insert() on the businessbase BO...
but on the second time i add another entry... it still goes through the same process of adding an empty object... then i add the needed data on that grid... and select another grid... it wont call the Protected Overrides Sub DataPortal_Insert()...
tnx for the help...
RockfordLhotka replied on Monday, June 04, 2007
Here's basic list/root code that works fine
Public Class List
Inherits Csla.EditableRootListBase(Of Root)
Public Shared Function GetList() As List
Return New List
End Function
Private Sub New()
AllowNew = True
End Sub
Protected Overrides Function AddNewCore() As Object
Dim item As Root = Root.NewRoot
Add(item)
Return item
End Function
End Class
Public Class Root
Inherits Csla.BusinessBase(Of Root)
Private _id As Integer
Public Property Id() As Integer
<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
Get
CanReadProperty(True)
Return _id
End Get
<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
Set(ByVal value As Integer)
CanWriteProperty(True)
If Not _id.Equals(value) Then
_id = value
PropertyHasChanged()
End If
End Set
End Property
Private _data As String=""
Public Property Data() As String
<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
Get
CanReadProperty(True)
Return _data
End Get
<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
Set(ByVal value As String)
CanWriteProperty(True)
If Not _data.Equals(value) Then
_data = value
PropertyHasChanged()
End If
End Set
End Property
Protected Overrides Function GetIdValue() As Object
Return _id
End Function
Public Shared Function NewRoot() As Root
Return New Root
End Function
Private Shared _lastId As Integer
Private Sub New()
_id = System.Threading.Interlocked.Increment(_lastId)
End Sub
Protected Overrides Sub DataPortal_Insert()
Debug.WriteLine("Insert " & _id.ToString)
MarkOld()
End Sub
Protected Overrides Sub DataPortal_Update()
Debug.WriteLine("Update " & _id.ToString)
MarkOld()
End Sub
Protected Overrides Sub DataPortal_DeleteSelf()
Debug.WriteLine("DeleteSelf " & _id.ToString)
MarkNew()
End Sub
End Class
dantes replied on Tuesday, June 05, 2007
Hi here is my code... for the EditableRootListBase
Imports Csla
Imports Csla.Data
Imports System.Data.sqlClient
Imports System.Configuration
Imports System.ComponentModel
<Serializable()> _
Public Class PalmHouseUserList
Inherits EditableRootListBase(Of PalmHouseUser)
Dim mActivelySaving As Boolean = False
#Region " Authorization Rules "
#End Region
#Region " Factory Methods "
Protected Overrides Function AddNewCore() As Object
Dim item As PalmHouseUser = PalmHouseUser.NewPalmHouseUser
Add(item)
Return item
End Function
Public Shared Function GetList() As PalmHouseUserList
Return DataPortal.Fetch(Of PalmHouseUserList)()
End Function
Private Sub New()
Me.AllowEdit = True
Me.AllowNew = True
Me.AllowRemove = True
End Sub
'Public Overloads Sub SaveItem(ByVal item As PalmHouseUser)
' SaveItem(IndexOf(item))
'End Sub
#End Region
#Region " Data Access "
Private Overloads Sub DataPortal_Fetch()
Me.RaiseListChangedEvents = False
' load data reader from database
Using sqlconn As New SqlConnection(connectionString)
sqlconn.Open()
Using sqlcomm As SqlCommand = sqlconn.CreateCommand
sqlcomm.CommandType = CommandType.StoredProcedure
sqlcomm.CommandText = "spPalmHouseGetAll"
'sqlcomm.Parameters.AddWithValue("@boarderId", crit.UserId)
Using dr As New SafeDataReader(sqlcomm.ExecuteReader())
While dr.Read
Add(PalmHouseUser.GetPalmHouseUser(dr))
End While
dr.Close()
End Using
End Using
sqlconn.Close()
End Using
Me.RaiseListChangedEvents = True
End Sub
#End Region
End Classdantes replied on Tuesday, June 05, 2007
here is my code for the businessbase
Imports Csla
Imports Csla.Data
Imports System.Data.sqlClient
Imports System.Configuration
<Serializable()> _
Public Class PalmHouseUser
Inherits BusinessBase(Of PalmHouseUser)
#Region " Business Methods "
Private _phUserId As Integer = 0
Private _phName As String = String.Empty
Private _phGroupNumber As Integer = 0
Public Property UserId() As Integer
<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
Get
CanReadProperty(True)
Return _phUserId
End Get
<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
Set(ByVal value As Integer)
CanWriteProperty(True)
If _phUserId <> value Then
_phUserId = value
PropertyHasChanged()
End If
End Set
End Property
Public Property Name() As String
<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
Get
CanReadProperty(True)
Return _phName
End Get
<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
Set(ByVal value As String)
CanWriteProperty(True)
If _phName <> value Then
_phName = value
PropertyHasChanged()
End If
End Set
End Property
Public Property GroupNumber() As Integer
<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
Get
CanReadProperty(True)
Return _phGroupNumber
End Get
<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
Set(ByVal value As Integer)
CanWriteProperty(True)
If _phGroupNumber <> value Then
_phGroupNumber = value
PropertyHasChanged()
End If
End Set
End Property
' 06. Must implement GetIdValue()
Protected Overrides Function GetIdValue() As Object
Return _phUserId
End Function
'TODO OPTIONAL:
' Uncomment the condition below for IsDirty() and IsValid() if you are implementing a base object with a
' child
Public Overrides ReadOnly Property IsDirty() As Boolean
Get
Return MyBase.IsDirty ' OrElse childList.IsDirty
End Get
End Property
Public Overrides ReadOnly Property IsValid() As Boolean
Get
Return MyBase.IsValid ' AndAlso childList.IsValid
End Get
End Property
#End Region
#Region " Validation Rules "
Protected Overrides Sub AddBusinessRules()
' TODO: add validation rules (if any)
'ValidationRules.AddRule(AddressOf Nothing, "")
End Sub
#End Region
#Region " Authorization Rules "
Protected Overrides Sub AddAuthorizationRules()
' TODO: add authorization rules (if any)
'AuthorizationRules.AllowWrite("", "")
End Sub
Public Shared Function CanAddObject() As Boolean
'Return ApplicationContext.User.IsInRole("Manager")
Return True
End Function
Public Shared Function CanGetObject() As Boolean
'Return ApplicationContext.User.IsInRole("Manager")
Return True
End Function
Public Shared Function CanEditObject() As Boolean
'Return ApplicationContext.User.IsInRole("Manager")
Return True
End Function
Public Shared Function CanDeleteObject() As Boolean
'Return ApplicationContext.User.IsInRole("Manager")
Return True
End Function
#End Region
#Region " Factory Methods "
' TODO:
' 07. You can implement add/modify the shared factory methods depending on your case
Public Shared Function NewPalmHouseUser() As PalmHouseUser
'If Not CanAddObject() Then
' Throw New System.Security.SecurityException("User not authorized to add")
'End If
Return DataPortal.Create(Of PalmHouseUser)()
End Function
Public Shared Function GetPalmHouseUser(ByVal id As Integer) As PalmHouseUser
'If Not CanGetObject() Then
' Throw New System.Security.SecurityException("User not authorized to view")
'End If
'Return CType(DataPortal.Fetch(New Criteria(id)), PalmHouseUser)
Return DataPortal.Fetch(Of PalmHouseUser)(New Criteria(id))
End Function
Public Shared Function GetPalmHouseUser(ByVal dr As SafeDataReader) As PalmHouseUser
'If Not CanGetObject() Then
' Throw New System.Security.SecurityException("User not authorized to view")
'End If
Return DataPortal.Fetch(Of PalmHouseUser)(dr)
End Function
Public Shared Sub DeletePalmHouseUser(ByVal id As Integer)
'If Not CanDeleteObject() Then
' Throw New System.Security.SecurityException("User not authorized to delete")
'End If
DataPortal.Delete(New Criteria(id))
End Sub
Public Overrides Function Save() As PalmHouseUser
If IsDeleted AndAlso Not CanDeleteObject() Then
'Throw New System.Security.SecurityException("User not authorized to remove")
MsgBox("not authorized")
ElseIf IsNew AndAlso Not CanAddObject() Then
'Throw New System.Security.SecurityException("User not authorized to add")
MsgBox("not authorized")
ElseIf Not CanEditObject() Then
'Throw New System.Security.SecurityException("User not authorized to update")
MsgBox("not authorized")
End If
Return MyBase.Save
End Function
Private Sub New()
' require use of factory methods
End Sub
#End Region
#Region " Data Access "
' TODO:
' 08. You can implement add/modify the Criteria class depending on your case.
<Serializable()> _
Private Class Criteria
Private idCrit As Integer
Public ReadOnly Property UserId() As Integer
Get
Return idCrit
End Get
End Property
Public Sub New(ByVal id As Integer)
idCrit = id
End Sub
Public Sub New()
End Sub
End Class
'TODO:
' 09. Implment the necessary DataPortal_XYZ methods
Private Overloads Sub DataPortal_Create(ByVal criteria As Criteria)
' load default values
_phUserId = 0
_phName = ""
_phGroupNumber = 0
End Sub
Private Overloads Sub DataPortal_Create()
' load default values
_phUserId = 0
_phName = ""
_phGroupNumber = 0
End Sub
Protected Overloads Sub DataPortal_Fetch(ByVal dr As SafeDataReader)
_phUserId = dr.GetInt32("boarderId")
_phName = dr.GetString("boarderName")
_phGroupNumber = dr.GetInt32("groupNumber")
End Sub
Protected Overrides Sub DataPortal_Insert()
' insert values
'gsantos: Don't put try catch here unless its a custom error to be thrown. The catching of error
'will be done on the topmost caller.
Using sqlconn As New SqlConnection("Data Source=alpha4;Initial Catalog=DM;Integrated Security=True")
sqlconn.Open()
Using sqlcomm As SqlCommand = sqlconn.CreateCommand
sqlcomm.CommandType = CommandType.StoredProcedure
sqlcomm.CommandText = "spPalmHouseInsert"
'cm.Parameters.AddWithValue("@tsID", criteria.Id)
sqlcomm.Parameters.AddWithValue("@boarderName", _phName)
sqlcomm.Parameters.AddWithValue("@groupNumber", _phGroupNumber)
sqlcomm.Parameters.AddWithValue("@numHoursStay", 0)
sqlcomm.ExecuteNonQuery()
End Using
sqlconn.Close()
End Using
End Sub
Protected Overrides Sub DataPortal_Update()
Using sqlconn As New SqlConnection("Data Source=alpha4;Initial Catalog=DM;Integrated Security=True")
sqlconn.Open()
Using sqlcomm As SqlCommand = sqlconn.CreateCommand
sqlcomm.CommandType = CommandType.StoredProcedure
sqlcomm.CommandText = "spPalmHouseEdit"
'cm.Parameters.AddWithValue("@tsID", criteria.Id)
sqlcomm.Parameters.AddWithValue("@boarderId", _phUserId)
sqlcomm.Parameters.AddWithValue("@boarderName", _phName)
sqlcomm.Parameters.AddWithValue("@groupNumber", _phGroupNumber)
sqlcomm.Parameters.AddWithValue("@numHoursStay", 0)
sqlcomm.ExecuteNonQuery()
End Using
sqlconn.Close()
End Using
End Sub
Protected Overrides Sub DataPortal_DeleteSelf()
DataPortal_Delete(New Criteria(_phUserId))
End Sub
Private Overloads Sub DataPortal_Delete(ByVal criteria As Criteria)
' delete values
Dim crit As Criteria = CType(criteria, Criteria)
Using sqlconn As New SqlConnection("Data Source=alpha4;Initial Catalog=DM;Integrated Security=True")
sqlconn.Open()
Using sqlcomm As SqlCommand = sqlconn.CreateCommand
sqlcomm.CommandType = CommandType.StoredProcedure
sqlcomm.CommandText = "spPalmHouseDelete"
'cm.Parameters.AddWithValue("@tsID", criteria.Id)
sqlcomm.Parameters.AddWithValue("@boarderId", crit.UserId)
sqlcomm.ExecuteNonQuery()
End Using
sqlconn.Close()
End Using
End Sub
#End Region
End Class
Copyright (c) Marimer LLC