Running into an issue with some functionality I'm trying to develop.
Client uses a series of Business objects (entities like Order, Customer, etc) that all inherit from the MustInherit class called AbstractEntity. There is a desire to add some logging/audit functionality to this class, so that all the objects will have
it. Essentially, we want this:
--Edits Begin
--A copy of the object is written out via binary serialization
--Properties are changed
Then, there are two paths:
EndEdit is called
--insert entries to logging table
--discard the binary copy
Or
CancelEdit is called
The values in the binary copy should be restored, in effect, discarding the edits
I thought I could implement IEditiableObject on the AbstractEntity class, since the methods exposed seem to fit perfectly with what we are trying to capture, but I am having difficulty envisioning how to handle the CancelEdit method -- how can I take a
Byte() and use it to overwrite the values of the object itself? Esp. since I'm in the Abstract class, and not a concrete instance.
For example, the Customer class could have properties like FirstName, LastName, and RegisterDate. When BeginEdit is called, I make a binary copy of the object, and store it in a private property of the AbstractEntity Class.
What can I do so that when CancelEditis called, the binary copy is "restored"?
Here is the code of the AbstractEntity class
Option Strict On
Option Explicit On
Imports System.ComponentModel
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO
Public MustInherit Class AbstractEntity
Implements IEditableObject
'this class contains the common properties that all business objects will share
'this includes items like the objects state
Private _state As Integer
Private _binData As Byte()
Private _memStream As MemoryStream
Private _binFormat As BinaryFormatter
Public Enum ObjectStateEnum As Integer
UNCHANGED = 0
CHANGED = 1
CHANGING = 2
End Enum
Sub BeginEdit() Implements IEditableObject.BeginEdit
'here we will store a serialized version of the object -- before edit
'Set state to Changing
_memStream = New MemoryStream
_binFormat = New BinaryFormatter
_binFormat.Serialize(_memStream, Me)
_binData = _memStream.ToArray()
_state = ObjectStateEnum.CHANGING
End Sub
Sub CancelEdit() Implements IEditableObject.CancelEdit
'we would restore the values of our serialized copy
_memStream = New MemoryStream(_binData)
_binFormat = New BinaryFormatter
_binFormat.Deserialize(_memStream)
_state = ObjectStateEnum.UNCHANGED
End Sub
Sub EndEdit() Implements IEditableObject.EndEdit
'Accept Edit. Object has Changed.
_state = ObjectStateEnum.CHANGED
End Sub
Public Property ObjectState() As ObjectStateEnum
Get
Return CType(_state, ObjectStateEnum)
End Get
Set(ByVal value As ObjectStateEnum)
_state = value
End Set
End Property
Public ReadOnly Property HasChanges() As Boolean
Get
If _state = ObjectStateEnum.CHANGED Then
Return True
Else
Return False
End If
End Get
End Property
End Class
Code above doesn't include the EndEit implementation, or a full CancelEdit.
webdev_mu
0 Points
1 Post
Serialization Issue
Jan 16, 2009 05:28 PM|LINK
Running into an issue with some functionality I'm trying to develop.
Client uses a series of Business objects (entities like Order, Customer, etc) that all inherit from the MustInherit class called AbstractEntity. There is a desire to add some logging/audit functionality to this class, so that all the objects will have it. Essentially, we want this:
--Edits Begin
--A copy of the object is written out via binary serialization
--Properties are changed
Then, there are two paths:
EndEdit is called
--insert entries to logging table
--discard the binary copy
Or
CancelEdit is called
The values in the binary copy should be restored, in effect, discarding the edits
I thought I could implement IEditiableObject on the AbstractEntity class, since the methods exposed seem to fit perfectly with what we are trying to capture, but I am having difficulty envisioning how to handle the CancelEdit method -- how can I take a Byte() and use it to overwrite the values of the object itself? Esp. since I'm in the Abstract class, and not a concrete instance.
For example, the Customer class could have properties like FirstName, LastName, and RegisterDate. When BeginEdit is called, I make a binary copy of the object, and store it in a private property of the AbstractEntity Class.
What can I do so that when CancelEditis called, the binary copy is "restored"?
Here is the code of the AbstractEntity class
Option Strict On Option Explicit On Imports System.ComponentModel Imports System.Runtime.Serialization.Formatters.Binary Imports System.IO Public MustInherit Class AbstractEntity Implements IEditableObject 'this class contains the common properties that all business objects will share 'this includes items like the objects state Private _state As Integer Private _binData As Byte() Private _memStream As MemoryStream Private _binFormat As BinaryFormatter Public Enum ObjectStateEnum As Integer UNCHANGED = 0 CHANGED = 1 CHANGING = 2 End Enum Sub BeginEdit() Implements IEditableObject.BeginEdit 'here we will store a serialized version of the object -- before edit 'Set state to Changing _memStream = New MemoryStream _binFormat = New BinaryFormatter _binFormat.Serialize(_memStream, Me) _binData = _memStream.ToArray() _state = ObjectStateEnum.CHANGING End Sub Sub CancelEdit() Implements IEditableObject.CancelEdit 'we would restore the values of our serialized copy _memStream = New MemoryStream(_binData) _binFormat = New BinaryFormatter _binFormat.Deserialize(_memStream) _state = ObjectStateEnum.UNCHANGED End Sub Sub EndEdit() Implements IEditableObject.EndEdit 'Accept Edit. Object has Changed. _state = ObjectStateEnum.CHANGED End Sub Public Property ObjectState() As ObjectStateEnum Get Return CType(_state, ObjectStateEnum) End Get Set(ByVal value As ObjectStateEnum) _state = value End Set End Property Public ReadOnly Property HasChanges() As Boolean Get If _state = ObjectStateEnum.CHANGED Then Return True Else Return False End If End Get End Property End ClassCode above doesn't include the EndEit implementation, or a full CancelEdit.
TIA