Hibernate version: 2.1.0.1001
I'm needing a way to track property changes on my domain objects before updates.
So say for a given set of objects:
Code:
public class Contact
{
private int id;
public int Id
{
get { return id; }
}
public Company Company { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Company
{
private int id;
public int Id
{
get { return id; }
}
public virtual string Name { get; set; }
private IList<Contact> contacts = new List<Contact>();
public virtual IList<Contact> Contacts
{
get { return contacts; }
set { contacts = value; }
}
public void Add(Contact contact)
{
if (Contacts.Contains(contact) != false)
return;
Contacts.Add(contact);
contact.Company = this;
}
public void Remove(Contact contact)
{
if (!Contacts.Contains(contact))
return;
Contacts.Remove(contact);
contact.Company = null;
}
}
An example log entry would look like:
Code:
Company company = new Company {Name = "TEST"};
Contact contact = new Contact{FirstName = "Tommy", LastName = "Tester"};
company.Add(contact);
Repository.Save(company);
//Log would be:
//Company (13) created
//Contact (9) created
company.Name = "Test Co.";
Repository.Save(company);
//Log would be:
//Company (13) Name changed from "TEST" to "Test Co."
company.Remove(contact);
company.Add(new Contact{FirstName = "Blah", LastName = "Blah"});
Repository.Save(company);
//Log would be:
//Company (13) removed Contact (9)
//Company (13) added Contact (10)
I know NHibernate can determine what changed when dynamic-update is used but I can't find a way to plug into that.
Implementing INotifyPropertyChanged on all of the domain objects is way too much work since there are ~100 of them.
I saw Ayende's post
http://ayende.com/Blog/archive/2007/04/17/Advance-Extending-NHibernate-Proxies.aspx on extending the Proxy but I couldn't figure out how to get it to work, and I believe this requires you to set the proxy in the config for all of your domain objects.
My thoughts were maybe it's possible with an Interceptor but I'm afraid I will trigger lazy loads if I run through checking the values for changes.
Can anyone point me in the right direction? I would really appreciate it!!