How to keep track of changes of an object???

D

Daniel Walzenbach

Hi,



I need to track all changes made to an object. Consider the following class:



Public Class Dog



Private _Name As System.String

Private _Weight As System.Byte



Public Property Name() As System.String

Get

Return _Name

End Get

Set(ByVal Value As System.String)

_Name = Value

End Set

End Property



Public Property Weight() As System.Byte

Get

Return _Weight

End Get

Set(ByVal Value As System.Byte)

_Weight = Value

End Set

End Property



Public Sub New(ByVal Name As System.String)

_Name = Name

End Sub



End Class



For the sake of understanding let's create a dog called "Fido" and - as it is still a puppy - assign a weight of 5 (let this be kg, pound, what ever.) to it.



Dim myDog As New Dog("Fido")

myDog.Weight = 5

' Fido get's saved to a database



As Fido gains more weight with the ages the following changes are made.



' Fido get's loaded from a database

myDog.Weight = 10



I now want to be able to track all the changes made to the Fido-Object during its lifetime. I thought about creating an XML file like the following using the properties of my class to save every change to Fido:



<?xml version="1.0" encoding="utf-8" ?>

<LogFile>

<Dog>

<Name>Fido</Name>

<Weight>5</Weight>

</Dog>

</LogFile>



<?xml version="1.0" encoding="utf-8" ?>

<LogFile>

<Dog>

<Weight>10</Weight>

</Dog>

</LogFile>



I now could store all the changes as an ntext (for they might get really long depending on the underlying object) in a database and display them in a DataGrid (or any other display of my choice) if it is possible to merge all the XML-Files into one big one?? Does anybody know how this merge can be done? I also assume that I have to use the system.Xml.XmlDataDocument class to create the XML-File but I am not sure how.

I also am absolutely not positive if this is a good approach to track changes of an object. What would be a better approach to track changes of an object on your opinion?



Thank you a huge lot in advance.

Daniel Walzenbach
 
P

Peter Huang

Hi Daniel,

Thanks for posting in the community.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to track a property's
change of one object and log it onto one xml file.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I think you can try to serialize the Name as an attribute also declare the
timestamp to know when the weight change.

The Log File will look like below.
<?xml version="1.0" encoding="utf-8" ?>
- <LogFile xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <Dog Name="Fido" TimeStamp="2004-02-12T16:12:59.7086582+08:00">
<Weight>20</Weight>
</Dog>
</LogFile>


[The class1.vb file used to (de)serialize the file]

Option Strict Off
Option Explicit On

Imports System.Xml.Serialization
Imports System.IO

<System.Xml.Serialization.XmlRootAttribute([Namespace]:="",
IsNullable:=False)> _
Public Class LogFile
<System.Xml.Serialization.XmlElementAttribute("Dog",
Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Items() As Dog
End Class

Public Class Dog
Public Event Weight_Changed()
'
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSch
emaForm.Unqualified)> _
Private _Weight As Byte

<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSch
emaForm.Unqualified)> _
Public Property Weight() As Byte
Get
Return _Weight
End Get
Set(ByVal Value As Byte)
If Not Value = _Weight Then
_Weight = Value
RaiseEvent Weight_Changed()
End If
End Set
End Property
' <System.Xml.Serialization.XmlAttributeAttribute()> _
Private _Name As String
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Private _TimeStamp As DateTime
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Property TimeStamp() As DateTime
Get
Return _TimeStamp
End Get
Set(ByVal Value As DateTime)
_TimeStamp = Value
End Set
End Property
Public Sub New()

End Sub
Public Sub New(ByVal nm As String)
_Name = nm
End Sub
End Class

[The Main file]
Option Strict Off
Option Explicit On

Imports System.Xml.Serialization
Imports System.IO
Imports System.Text

Module Module1
Dim ds As DataSet
Dim lf As LogFile
Sub Main()
' At the first time create the file
'CreateLogFile()
Dim dt As DateTime

'Deserialize an object
Dim x As XmlSerializer = New XmlSerializer(GetType(LogFile))
Dim reader As TextReader = New StreamReader("C:\LogFile.xml")
lf = x.Deserialize(reader)
ds = New DataSet
ds.ReadXml("C:\LogFile.xml")
AddHandler lf.Items(0).Weight_Changed, AddressOf Dog_Weight_Changed
lf.Items(0).Weight = 20
'Change the Weight will be log to file
lf.Items(0).Weight = 30
'You also can write to a file
ds.WriteXml(Console.Out)
End Sub

Private Sub CreateLogFile()
Dim dg As Dog
Dim lb As New LogFile
dg = New Dog("Fido")
dg.TimeStamp = Now
dg.Weight = 20
lb.Items = New Dog() {dg}
Dim x As XmlSerializer = New XmlSerializer(GetType(LogFile))
Dim writer As TextWriter = New StreamWriter("C:\LogFile.xml")
x.Serialize(writer, lb)
writer.Close()
End Sub

Private Sub Dog_Weight_Changed()
Dim x As XmlSerializer = New XmlSerializer(GetType(LogFile))
'Dim writer As TextWriter = New StreamWriter("Hello.xml")
Dim sm As MemoryStream = New IO.MemoryStream
x.Serialize(sm, lf)
sm.Position = 0
Dim reader As Xml.XmlTextReader = New Xml.XmlTextReader(sm)
ds.ReadXml(reader)
sm.Close()
End Sub
End Module

Did this works for you?
If you have any concern on this issue,please post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Cor

Hi Peter,

No critique, you got enough the last days,

Try to avoid Option Strict Off in your messages. The regulars from this
newsgroup (from which are you of course also one) try that.

No answer necessary, only to make you attend on it.

Cor
 
G

Guest

Peter

thank you. I like this approach. It gives me a nice base to build onto..

Have a nice day. Best regards

Daniel Walzenbach
 
G

Guest

Cor,

why don’t you just email Peter privately instead of criticizing him in front of the whole newsgroup? @ least as you seem to know him. And comments like “No critique, you got enough the last days†aren’t really productive…

Think about you’d feel if roles where changed… Just my $.02.

Daniel Walzenbach
 
C

Cor

Hi Daniel,

I do not see anything wrong to attend regulars on some things that we are
used to do.

If they attend me on that, I only say, thank you.

And posting unasked emails is something against Netiquete from a newsgroup.

I do not do that.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top