473,395 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
Nov 20 '05 #1
5 4018
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.

Nov 20 '05 #2
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
Nov 20 '05 #3
Peter

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

Have a nice day. Best regards

Daniel Walzenbach
Nov 20 '05 #4
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
Nov 20 '05 #5
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
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: d.schulz81 | last post by:
Hi all, We have about 10 different domains that are linked very closely and we want to identify and keep track of every single user that surfs our websites by the use of sessions. The problem...
4
by: SQLDBA | last post by:
What would be the best practice to follow to keep track of MS SQL server changes... Stroed procs, tables, views, triggers, indexes, DTS and also jobs ect.... I am not quite sure how Source safe...
2
by: Uwe Range | last post by:
Hi to all, I am developing a database where, with time, it proved to be necessary to keep track of the changes to the data. It is fairly easy to keep track of the time when a record was changed...
1
by: strvariant | last post by:
Hi all, I am searching for a way to track when a form/report/query is changed by users. For instance: 1) A user opens a form/report 2) The user enters the Design mode of that form/report ...
3
by: Alan Wang | last post by:
Hi there, Once my application gets complicated and complicated. I found it's really hard to keep track of Session value I am using in my asp.net application. I am just wondering if anyone have...
2
by: metaperl | last post by:
I'm actually taking Microsoft's 2779 and just finished a lab where we kept track of our changes to the database. However, I'm not happy with the scripts interface because it does not tell me the...
15
by: l3vi | last post by:
I have a new system Im building that stores entries of what people are searching for on my sites. I want to be able to keep records of how many times a keyword was searched for daily, and from...
3
by: PJ6 | last post by:
What's the best way to, in the general case, track changes to an object's public fields and properties? In a lot of applicatoins I've seen bahavior consistant with simple periodic checking from a...
11
by: Andrew G. Koptyaev | last post by:
Please help. I need to keep track of how many object of a given class without introducing a non-class member variable. Which one of the following will allow me to do this? 1) Add member...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.