RobinS said:
I will post an example this weekend, assuming you're still interested. I
have an example in VB; I changed computers and have to track it down...
RobinS.
This class shows how to implement iEditable. You also need to implement
iCloneable. I use my own method (CloneMe) that calls the Clone method; this
is a pattern I use because not all of my classes are shallow, and sometimes
I have to use a different method to do my cloning.
If you are looking to bind a list of business objects to a DataGridView, I
know how to do that. When you do that, it uses the iEditable methods
automatically.
When the user edits any of the data, you call BeginEdit.
If the user rolls it back, you call CancelEdit.
When the user hits Save, you call EndEdit.
If you bind through a binding source, IIRC it calls these methods properly.
-----------------------------
Imports System.ComponentModel
Public Class Product
Implements IEditableObject, ICloneable
Private _editing As Boolean
Private Shared _loading As Boolean
#Region " Constructors"
'you must have a public constructor for the dgv to add a record
Public Sub New()
If Not _loading Then
Me.SetDefaults()
End If
End Sub
Private Sub SetDefaults()
ProductID = 0
ProductName = String.Empty
End Sub
#End Region
#Region " Public Properties"
Private _lastProduct As Product
''' <summary>
''' this is the info for the Product before the user started editing it
''' when they start editing, it clones the Product instance
''' </summary>
Public Property lastProduct() As Product
Get
Return _lastProduct
End Get
Set(ByVal value As Product)
_lastProduct = value
End Set
End Property
Private _originalProduct As Product
''' <summary>
''' when you create a new instance, save a copy of the original.
''' This would be used (for examle) on the Grid screen. If they modify
one,
''' and then delete it, you need the original primary key in order to do
the actual delete.
''' </summary>
Public Property originalProduct() As Product
Get
Return _originalProduct
End Get
Set(ByVal value As Product)
_originalProduct = value
End Set
End Property
Private _ProductID As Integer
''' <summary>
''' Product ID
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property ProductID() As Integer
Get
Return _ProductID
End Get
Private Set(ByVal value As Integer)
_ProductID = value
End Set
End Property
Private _ProductName As String
''' <summary>
''' Name of the product.
''' </summary>
''' <value>ProductName</value>
''' <returns>ProductName</returns>
''' <remarks></remarks>
Public Property ProductName() As String
Get
Return _ProductName
End Get
Set(ByVal value As String)
If _ProductName <> value Then
Dim propertyName As String = "ProductName"
ValidationInstance.ValidateClear(propertyName)
ValidationInstance.ValidateRequired(propertyName, value)
ValidationInstance.ValidateLength(propertyName, value, 24)
Me.DataStateChanged(EntityStateEnum.Modified)
_ProductName = value
End If
End Set
End Property
#End Region
#Region " Public Methods"
'methods Create, Save, Delete removed for brevity
#End Region
#Region "iEditable"
Public Sub BeginEdit() Implements
System.ComponentModel.IEditableObject.BeginEdit
If Not _editing Then
_loading = True 'set this so it won't initialize it because
you're going to clone it anyway.
lastProduct = Me.CloneMe()
End If
_editing = True
End Sub
'roll back the changes by setting the current property values back to
the original
'also need to roll back the Views information
Public Sub CancelEdit() Implements
System.ComponentModel.IEditableObject.CancelEdit
If _editing Then
'You can't do this because [me] is read-only. So you have to set
each and every stinking property.
'this = (Plan)(this.LastPlan.Clone());
ProductID = lastProduct.ProductID
ProductName = lastProduct.ProductName
End If
_editing = False
End Sub
''' <summary>
''' accept the current values by setting the editing flag back to false
''' this happens when they go to a different row or close the screen
''' </summary>
''' <remarks></remarks>
Public Sub EndEdit() Implements
System.ComponentModel.IEditableObject.EndEdit
_editing = False
End Sub
#End Region
#Region "iCloneable"
Public Function CloneMe() As Product
Dim prod As Product = New Product()
prod = DirectCast(Me.Clone, Product)
Return prod
End Function
Public Function Clone() As Object Implements System.ICloneable.Clone
Return Me.MemberwiseClone()
End Function
#End Region
End Class