Kamil Kliczbor @ asptip.net

8Nov/130

NHibernate Internals: Exploring NHibernate Proxy Part 3

NhLogoWhite64_med

Introduction

This is the next post from the quesi-series of notes about NHibernate Proxies.

What is ProxyDummy?

In the first post I introduced generated class for MyProxyImpl class implementing IMyProxy interface. One of the interesting things here is ProxyDummy class the proxy is directly inheriting from:

public class INHibernateProxyProxy : ProxyDummy, INHibernateProxy, IMyProxy, ISerializable, IProxy

This class has no members. It is a some kind of marker class. I search for usages in the solution but there is neither strongly typed reference nor run-time.

public class ProxyDummy
{
	/* No Implementation */
}

However CreateUncachedProxyType of the ProxyFactory reveals some dark side power: we find short comment describing why we inherit from the ProxyDummy class. And this is something that is self-explanatory =].

// Use the proxy dummy as the base type 
// since we're not inheriting from any class type
System.Type parentType = baseType;
if (baseType.IsInterface)
{
	parentType = typeof (ProxyDummy);
	interfaces.Add(baseType);
}

I believe this is something that is somehow a legacy code. Why do we need to have a concrete class in a proxy? Maybe this was intended to use in the future?

What is IProxy and INHibernateProxy?

The first interface - IProxy is a part of the LinFu project and it has only internal meaning for the LinFu.DynamicProxy namespace. The interface is defined as follows:

namespace NHibernate.Proxy.DynamicProxy
{
	public interface IProxy
	{
		IInterceptor Interceptor { get; set; }
	}
}

As we can see there is only one member defined there - IInterceptor type that is more attractive for further investigations.

Now is the turn for INHibernateProxy interface:

namespace NHibernate.Proxy
{
	/// <summary>
	/// A marker interface so NHibernate can know if it is dealing with
	/// an object that is a Proxy. 
	/// </summary>
	/// <remarks>
	/// <para>
	/// This interface should not be implemented by anything other than
	/// the Dynamically generated Proxy.  If it is implemented by a class then
	/// NHibernate will think that class is a Proxy and will not work.
	/// </para> 
	/// <para>
	/// It has to be public scope because
	/// the Proxies are created in a separate DLL than NHibernate. 
	/// </para> 
	/// </remarks>
	public interface INHibernateProxy
	{
		/// <summary> Get the underlying lazy initialization handler. </summary>
		ILazyInitializer HibernateLazyInitializer { get;}
	}
}

As we see in the summary of the INHibernateProxy interface - it is a marker that is used by NHibernate  to recognize if it is dealing with a proxy instance. The IProxy works only for LinFu.DynamicProxy context. INHibernateProxy interface is being used through the whole NHibernate project. It is important to generate proxy type with this interface implemented to let NHibernate use this class as a proxy. Dynamically generated class had it implemented with IProxy:

[Serializable]
public class INHibernateProxyProxy : ProxyDummy, INHibernateProxy, IMyProxy, ISerializable, IProxy

This is end for now.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.