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.