Re: Is Dirty
Dean Cleaver <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <4543A775711185429E59B18055D4167423185F@sxslakl004.xceptionsoftware.com> |
Brady,
I have done this in two ways:
1. Internally all data for my class was stored in another class (or
structure) like this:
public class Person
{
private class PersonData
{
public string Name;
public int Age;
}
private PersonData original
private personData current
public class Person(string name, int age)
{
this.original = new PersonData()
this.original.Name = name;
this.original.Age = age;
this.current = new PersonData()
this.current.Name = name;
this.current.Age = age;
}
public string Name
{
get {return this.current.Name;}
set {this.current.Name = value;}
}
public int Age
{
get {return this.current.Age;}
set {this.current.Age= value;}
}
public bool IsDirty
{
get
{
if (this.current.Name != this.original.name)
return true;
if (this.current.Name != this.original.name)
return true;
return false;
}
}
}
2. Create a string that is a sum of all properties, and store the MD5
hash of that string - then at any time, you just need to compare the
hash to the current hash to see if it's changed.
public class Person
{
private byte[] md5Hash;
public class Person(string name, int age)
{
this.name = name;
this.age = age;
StringBuilder builder = new StringBuilder();
builder.Append(this.name);
builder.Append(this.age.ToString());
this.md5Hash = new
MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(builder.T
oString()));
}
private string name;
public string Name
{
get {return this.name;}
set {this.name = value;}
}
private int age;
public int Age
{
get {return this.age;}
set {this.age= value;}
}
public bool IsDirty
{
get
{
StringBuilder builder = new StringBuilder();
builder.Append(this.name);
builder.Append(this.age.ToString());
byte[] data = new
MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(builder.T
oString()));
return !CompareArrays(data, this.md5Hash);
}
}
internal static bool CompareArrays(byte[] byte1, byte[] byte2)
{
try
{
if (byte1 == null && byte2 == null)
return true;
if (byte1 == null || byte2 == null)
return false;
if (byte1.Length != byte2.Length)
return false;
for (int i = 0; i < byte1.Length; i++)
if (byte1[i] != byte2[i])
return false;
return true;
}
catch
{
return false;
}
}
}
The first one has the benefit in that you can tell exactly what changed
- but I was using remoting, and it meant I was sending double the data
everywhere, so the second one has the benefit of sending less data over
the wire.
HTH
Dino
-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps
and controls [mailto:[email protected]] On Behalf Of
Brady Kelly
Sent: Friday, 18 May 2007 02:25
To: [email protected]
Subject: [DOTNET-WINFORMS] Is Dirty
I'm doing a quick and dirty prototype app, with one simple detail edit
screen, so I've used simple own code to transfer values between controls
and data entity, deferring properly implementing data binding at a later
stage[1]. Now I want to know if the edited entity is 'dirty'. My first
reaction is to serialise it on starting my edit, and then when closing
the form, and compare the two. How do the experts here feel about this?