I made an attempt, but it doesn't work (and seems ugly) (this is
different than what I describe in earlier post, because I don't know
how to do that):

Define an interface that your domain classes will implement:

public interface IProxyableNotifyPropertyChanged :
INotifyPropertyChanged
{
        object Sender { get; }
}

In your class use it like this:

public class MyClass : IProxyableNotifyPropertyChanged
{
     ....

        public virtual event PropertyChangedEventHandler PropertyChanged;

        public virtual object Sender
        {
                get { return this; }
        }

        private void SendPropertyChanged( string propertyName )
        {
                if( PropertyChanged != null )
                {
                        PropertyChanged( Sender, new
PropertyChangedEventArgs( propertyName ) );
                }
        }
}


The proxy factory-related code (it's for NH 2.0.x, adapted from
Ayende's code from:
https://svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate.Test/ProxyInterface/CustomProxyFixture.cs
)

public class CustomProxyFactoryFactory : IProxyFactoryFactory
{
        public IProxyFactory BuildProxyFactory()
        {
                return new DataBindingProxyFactory();
        }
}

public class DataBindingProxyFactory : CastleProxyFactory
{
        public override INHibernateProxy GetProxy( object id,
ISessionImplementor session )
        {
                try
                {
                        CastleLazyInitializer initializer =
                                new DataBindingInterceptor( EntityName, 
PersistentClass, id,
                       GetIdentifierMethod, SetIdentifierMethod,
                       ComponentIdType, session );

                        object generatedProxy = null;
                        if ( IsClassProxy )
                        {
                                generatedProxy = 
DefaultProxyGenerator.CreateClassProxy(
                                        PersistentClass, Interfaces, 
ProxyGenerationOptions.Default,
initializer );
                        }
                        else
                        {
                                generatedProxy =
DefaultProxyGenerator.CreateInterfaceProxyWithoutTarget(
                                        Interfaces[0], Interfaces, initializer 
);
                        }

                        initializer._constructed = true;
                        return (INHibernateProxy) generatedProxy;
                }
                catch( Exception e )
                {
                        throw new HibernateException( "Creating a proxy 
instance failed",
e );
                }
        }
}


public class DataBindingInterceptor : CastleLazyInitializer
{
        public DataBindingInterceptor(string entityName, System.Type
persistentClass, object id,
                MethodInfo getIdentifierMethod, MethodInfo setIdentifierMethod,
IAbstractComponentType componentIdType, ISessionImplementor session)
                : base(entityName, persistentClass, id, getIdentifierMethod,
setIdentifierMethod, componentIdType, session)
        {
        }

        public override void Intercept(IInvocation invocation)
        {
                if (invocation.Method.DeclaringType ==
typeof(IProxyableNotifyPropertyChanged)
                        && invocation.Method.Name == "get_Sender" )
                {
                        invocation.ReturnValue = invocation.InvocationTarget;
                        return;
                }
                base.Intercept(invocation);
        }
}

You tell NHibernate to use your proxy factory like this:
        
configuration.Properties[NHibernate.Cfg.Environment.ProxyFactoryFactoryClass]
=
                typeof(CustomProxyFactoryFactory).AssemblyQualifiedName;


It doesn't work, because DataBindingInterceptor.Intercept() is never
called with "get_Sender" method, don't know why, looks like the
generated proxy does not provide an override for that property, so the
CLR calls the property on the actual object and not on a proxy.

Maybe someone with more experience in this area will be able to help.


On 7 Paź, 21:06, Rob <[EMAIL PROTECTED]> wrote:
> I have just spent a significant amount of time debugging
> PropertyChanged events that weren't being triggered and was happy to
> find this post which describes my problem exactly.  The workaround of
> not lazy-loading objects is not an option for us so we're stuck with
> not having the ability to properly use INotifyPropertyChanged in our
> code.  The solution of having an interceptor is beyond what we trust
> ourselves to do in NHibernate.  Has anyone attempted this?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to