-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Traking changed properties on domain objects
PostPosted: Wed Jan 14, 2009 12:45 pm 
Newbie

Joined: Tue Jan 13, 2009 12:35 pm
Posts: 4
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!!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 3:42 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have a look here:

http://www.hibernate.org/195.html

It's for Hibernate, but the principle is the same for NHib.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 10:41 am 
Newbie

Joined: Tue Jan 13, 2009 12:35 pm
Posts: 4
wolli wrote:
Have a look here:

http://www.hibernate.org/195.html

It's for Hibernate, but the principle is the same for NHib.


Thank you for the link!
I used that and these as examples to get what I needed:
http://www.hibernate.org/156.html
http://www.hibernate.org/318.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.