Hi,
Without understanding most of the inner technical details of
nHibernate, it would seem as if I've gotten Unity Interception to work
as the LazyInitializer.
Code for the LazyInitializer:
class LazyInitializer: BasicLazyInitializer, IInterceptionBehavior
{
public bool _constructed;
public LazyInitializer(string entityName, Type persistentClass,
object id, MethodInfo getIdentifierMethod, MethodInfo
setIdentifierMethod, IAbstractComponentType componentIdType,
ISessionImplementor session)
: base(entityName, persistentClass, id, getIdentifierMethod,
setIdentifierMethod, componentIdType, session)
{
}
public IMethodReturn Invoke(IMethodInvocation input,
GetNextInterceptionBehaviorDelegate getNext)
{
var result = input.CreateMethodReturn(null);
if (_constructed)
{
var args = new object[input.Arguments.Count];
input.Arguments.CopyTo(args, 0);
result.ReturnValue = base.Invoke((MethodInfo)input.MethodBase,
args, input.Target);
if (result.ReturnValue == InvokeImplementation)
{
//var input2 = new
VirtualMethodInvocation(GetImplementation(), input.MethodBase,
input.Arguments);
result =
input.CreateMethodReturn(input.MethodBase.Invoke(GetImplementation(),
args));
}
}
return result;
}
public IEnumerable<Type> GetRequiredInterfaces()
{
return new[] {typeof (INHibernateProxy)};
}
public bool WillExecute
{
get { return true; }
}
Code for the Proxy Factory:
class UnityProxyFactory: AbstractProxyFactory
{
public override INHibernateProxy GetProxy(object id,
ISessionImplementor session)
{
INHibernateProxy proxy;
try
{
LazyInitializer initializer = new LazyInitializer(EntityName,
PersistentClass, id, GetIdentifierMethod, SetIdentifierMethod,
ComponentIdType, session);
object obj2 = IsClassProxy
?
Intercept.NewInstanceWithAdditionalInterfaces(PersistentClass, new
VirtualMethodInterceptor(),
new[] {initializer}, Interfaces)
:
Intercept.NewInstanceWithAdditionalInterfaces(Interfaces[0], new
VirtualMethodInterceptor(),
new[] {initializer}, Interfaces);
initializer._constructed = true;
proxy = (INHibernateProxy)obj2;
}
catch (Exception exception)
{
//log.Error("Creating a proxy instance failed", exception);
throw new HibernateException("Creating a proxy instance
failed", exception);
}
return proxy;
}
}
Any comments / ideas? What sort of unit tests is required to make sure
that Unity Interception is actually working correctly?
Regards,
Dawid Mostert