Monday, March 21, 2011

WPF Lazy Proxy Revisited for NHibernate 3.2

When hooking up to PropertyChanged events, some systems require that the same object that a handler was registered with be the same object that throws the event. With NHibernate proxying, this can be a problem. The external viewer normally hooks up to the proxy object, but then the actual implementation object is the one throwing the property changed event, using "this" as a reference, which is naturally different from the proxy. This behavior occurs because the proxying is not entirely transparent. Even though the user hooks up to the proxy object and expects events from the proxy object, the real object throws the events, messing with handlers that may be stored in dictionaries mapped by the sender. See my first post for other details on this same problem.

The original solution to this problem modified the Castle bytecode provider. Now, with NHibernate 3.2, there is a default provider built-in. Here is the new code with the fix for the PropertyChanged event. Using this code fixes problems with WPF and libraries like Obtics.

No comments: