Category: DynamicProxy

Castle Dynamic Proxy tutorial part IX: Interface proxy with target

This is part nine of my tutorial on Castle Dynamic Proxy.

As cool as interface proxies without target are, most of the time you’d have an existing implementation under interface. Still, there are many useful scenarios, where you would want to use proxies. AOP stuff comes to mind, but not only.

Let’s say you’re using a library from some vendor that among other things exposes an interface like the following along with its implementation:

namespace ThirtPartyLibrary
{
	public interface ITimeHelper
	{
		int GetHour(string dateTime);
		int GetMinute(string dateTime);
		int GetSecond(string dateTime);
	}
}

There’s also a class in the library that implements the interface:

namespace ThirtPartyLibrary
{
	public sealed class TimeHelper : ITimeHelper
	{
		public int GetHour(string dateTime)
		{
			DateTime time = DateTime.Parse( dateTime );
			return time.Hour;
		}

		public int GetMinute(string dateTime)
		{
			DateTime time = DateTime.Parse(dateTime);
			return time.Minute;
		}

		public int GetSecond(string dateTime)
		{
			DateTime time = DateTime.Parse(dateTime);
			return time.Second;
		}
	}
}

Each method accepts string representation of date and time, for example “10/11/2009 5:32:11 AM” and return UTC value for its hour, minute, or second.

Two things to note about this example. One is that the implementation has few bugs, second one – the class is sealed, so you can’t inherit from it.

First bug, and the most obvious is, it does not check for null reference, so when you pass it null string, it will throw. It also does not validate the format of the string in any way (it should rather use DateTime.TryParse method).

More subtle bug lies in the fact, that output value from each of the methods has to represent UTC time. Input value on the other hand, may represent any time zone (for example “10/11/2009 9:32:11 AM -04:30”). The implementation will produce invalid values for time zones other than UTC, but since the vendor was located in UTC, they didn’t test for it. Actually it’s also due to DateTime’ inability to represent time zone information. To fix this DateTimeOffset a.k.a. DateTime2 structure was introduced.

There’s also another bug related to globalization – the string may be in different form for different culture, for example, in Poland above Venezuelan date would look like this: “2009-10-11 09:32:11 -04:30”. However, for the sake of simplicity we will ignore that issue.

Now, let’s assume the vendor of the component won on the lottery, closed his business, and moved to Brisbane. In other words, don’t expect him to fix the bugs.

This is one example when proxy might help. To be fair, for such a trivial class better option would be to roll your own implementation of the interface from scratch. However let’s assume the TimeHelper class is much more complicated and we do care about the rest of it’s non-buggy behavior.

We identified three bugs, we’d like to fix for this class, let’s now write tests to express our requirements:

public class TimeFixTests
{
	private ITimeHelper _sut;

	public TimeFixTests()
	{
		_sut = new TimeHelper();
	}

	[Fact]
	public void GetMinute_should_return_0_for_null()
	{
		int minute = _sut.GetMinute(null);
		int second = _sut.GetSecond(null);
		int hour = _sut.GetHour(null);
		Assert.Equal(0, minute);
		Assert.Equal(0, second);
		Assert.Equal(0, hour);
	}

	[Fact]
	public void Fixed_GetHour_properly_handles_non_utc_time()
	{
		var dateTimeOffset = new DateTimeOffset(2009, 10, 11, 09, 32, 11, TimeSpan.FromHours(-4.5));
		DateTimeOffset utcTime = dateTimeOffset.ToUniversalTime();
		string noUtcTime = dateTimeOffset.ToString();
		int utcHour = _sut.GetHour(noUtcTime);
		Assert.Equal(utcTime.Hour, utcHour);
	}

	[Fact]
	public void Fixed_GetMinute_properly_handles_non_utc_time()
	{
		var dateTimeOffset = new DateTimeOffset(2009, 10, 11, 09, 32, 11, TimeSpan.FromMinutes(45));
		DateTimeOffset utcTime = dateTimeOffset.ToUniversalTime();
		string noUtcTime = dateTimeOffset.ToString();

		int utcMinute = _sut.GetMinute(noUtcTime);
		Assert.Equal(utcTime.Minute, utcMinute);
	}


	[Fact]
	public void Fixed_GetHour_hadles_entries_in_invalid_format()
	{
		int result = _sut.GetHour("BOGUS ARGUMENT");
		Assert.Equal(0, result);
	}
}

If we run the tests now, they will fail, except for the third one which surprisingly passes (this only proves you should run your tests expecting them to fail before you write the implementation that would make them pass). I was really surprised to see that, which in my opinion only shows how inconsistent DateTime is. Anyway, we still have three tests to fix, so back to our business.

Since we use the class by the interface, the fact that it’s sealed is not a problem. That’s one major difference between class proxy we worked with in initial parts of the tutorial, and interface proxy with target – interface proxy can use sealed class instance as target implementation. That’s the result of the way such proxy is implemented. DynamicProxy creates a new type that implements given interface, and which forwards the calls to given target object. The only thing both types have in common is they both implement the same interface.

We have three major issues to fix:

One is to handle calls when null is passed to the method. We do it the usual way, by writing a simple interceptor:

internal class CheckNullInterceptor : IInterceptor
{
	public void Intercept(IInvocation invocation)
	{
		if( invocation.Arguments[ 0 ] == null )
		{
			invocation.ReturnValue = 0;
			return;
		}
		invocation.Proceed();
	}
}

When a null value is passes, we want to skip the invocation of the actual method, and return right back to the caller returning 0.

Other two issues, are dealing with input. Since DateTime is incapable of understanding time zones, we must intercept calls to the methods that can produce wrong values when input string represents some other time zone than UTC. So far there are no time zones that differ by mere seconds, so that leaves us with two methods to fix. We might write an interceptor like this:

internal class AdjustTimeToUtcInterceptor:IInterceptor
{
	public void Intercept( IInvocation invocation )
	{
		var argument = (string)invocation.Arguments[0];
		DateTimeOffset result;
		if (DateTimeOffset.TryParse(argument, out result))
		{
			argument = result.UtcDateTime.ToString();
			invocation.Arguments[ 0 ] = argument;
		}
		try
		{
			invocation.Proceed();
		}
		catch( FormatException )
		{
			invocation.ReturnValue = 0;
		}
	}
}

We try to parse the date and time out of given string, and if we succeed, we switch it with its UTC value. If we can’t parse the string we let the underlying implementation (that is the sealed TimeHelper class) handle this. We than catch exception the implementation may throw and instead provide default return value.

We’re now only left with a way to select appropriate interceptors for intercepted methods. For that, we create a selector.

internal class TimeFixSelector : IInterceptorSelector
{
	private static readonly MethodInfo[] methodsToAdjust =
		new[]
		{
			typeof(ITimeHelper).GetMethod("GetHour"),
			typeof(ITimeHelper).GetMethod("GetMinute")
		};
	private CheckNullInterceptor _checkNull = new CheckNullInterceptor();
	private AdjustTimeToUtcInterceptor _utcAdjust = new AdjustTimeToUtcInterceptor();

	public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors)
	{
		if (!methodsToAdjust.Contains(method))
			return new IInterceptor[] { _checkNull }.Union( interceptors ).ToArray();
		return new IInterceptor[] { _checkNull, _utcAdjust }.Union( interceptors ).ToArray();
	}
}

Selector checks if method is one of known methods that are prone to time zone bug, and appends appropriate interceptors at the beginning of given interceptors array. Remember that order is important. That’s why CheckNullInterceptor is put on the first place, so that every following interceptor does not have to check if argument is null.

With all that we’re almost done. However it would probably be a good idea to encapsulate all the proxy creation logic in its own class:

public class TimeFix
{
	private ProxyGenerator _generator = new ProxyGenerator();
	private ProxyGenerationOptions _options = new ProxyGenerationOptions { Selector = new TimeFixSelector() };

	public ITimeHelper Fix(ITimeHelper item)
	{
		return (ITimeHelper)_generator.CreateInterfaceProxyWithTarget(typeof(ITimeHelper), item, _options);
	}
}

Now, to make our tests pass, we need to update the test fixture constructor:

public TimeFixTests()
{
	var fix = new TimeFix();
	_sut = fix.Fix(new TimeHelper());
}

And all the tests should now pass.

dptutorial_9_all_green

That was a simplified example, but hopefully by now you understand how dynamic proxies with target work, how they’re different from other kinds of proxies we talked about so far (class proxies, and interface proxies without target) and how and when you can utilize them. If not, or you have any questions, feel free to leave a comment.

Technorati Tags: , ,

Dynamic Proxy frameworks comparison: update

After my post comparing different dynamic proxy frameworks, many people were surprised by the poor performance of LinFu.DynamicProxy framework. In the update I noted that it was gathering the stack trace for each intercepted call, that made the framework so slow.

Philip, the author of LinFu updated the framework, removing usage of StackTrace. It made a drastic change in the performance, and my previous comparison no longer correspond to current characteristics of the framework.

As such I decided to re-run the test. Here are the results:

proxy_frameworks_rematch

All the times are roughly 10% better than the last time, which is due to interference from other processes on my machine. What is important is the relative values between frameworks. As we can see LinFu is now a lot faster – the change yielded over x20 performance improvement, making it almost as fast as spring, and twice as fast as Unity.

I still didn’t invoke proxied method. With LinFu currently the only way I’m aware of is doing it via MethodInfo.Invoke, which is relatively slow, but judging from Philip’s tweets, it will be addressed soon as well.

Technorati Tags: ,

Castle Dynamic Proxy tutorial part VIII: Interface proxy without target

This is part eight of my tutorial on Castle Dynamic Proxy.

After a short break let’s get right back to our Dynamic Proxy work.

Last time I introduced different kinds of proxies you can create with Castle Dynamic Proxy. Today, we’ll talk about interface proxy without target. As you probably know, interface by itself can not exist. It’s a contract defining what its implementer can do. As such you do need an implementer. If you create an interface proxy without target however you don’t need to provide the implementer. Dynamic Proxy creates it for you. This is pretty powerful.

There are times where you don’t want to create a new class to implement the interface, for variety of reasons. One such case might be using API that requires an interface, where a delegate would be more appropriate. Back in .NET 1.0, 1.1 times having methods accepting delegates was considered a code smell. This is understandable, if you remember that we didn’t have lambdas, anonymous delegates, or even generics back then.

Now however, it’s often more convenient to pass a lambda instead of creating manually a type. If you’re working with API that accepts interface, wishing you could pass a delegate inside, I have a good news for you – interface proxy without target may help you.

Let’s consider a simple interface:

public interface IAnsweringEngine
{
	int GetAnswer( string s );
}

and some public API that consumes it:

public interface IDeepThought
{
	void SetAnsweringEngine(IAnsweringEngine answeringEngine);
} 

Now, considering your answering engine is very simple one-liner, you might wish to not create a new class in code, but rather pass a lambda in. This however will not work.

IDeepThought d = GetSuperComputer();
d.SetAnsweringEngine( ( string s ) => s.Length ); 

With what we learned however so far we can make it work. All we need to do, is to build a proxy for that interface, with our delegate providing the implementation. See the following test:

[Fact]
public void Should_be_able_to_wrap_interface_with_one_method()
{
	Func<string, int> length = s => s.Length;
	var wrapped = DelegateWrapper.WrapAs<IAnsweringEngine>( length );
	Assert.NotNull( wrapped );
	var i = wrapped.GetAnswer( "Answer to Life the Universe and Everything" );
	Assert.Equal( 42, i );
} 

Notice we don’t have any manually created implementer of IAnsweringEngine here. So how does it work?

When implementing our DelegateWrapper we need to remember one thing about interface proxies without target. They are just a shells for us to fill. Dynamic Proxy does create a class that implements the interface, but it does not know what logic to put into it.

As such it is our responsibility to provide that logic and fill the shell via interceptors.

Our interceptors don’t have to be complicated. They just get a delegate and when invoked, call the delegate.

internal class MethodInterceptor : IInterceptor
{
	private readonly Delegate _impl; 

	public MethodInterceptor( Delegate @delegate )
	{
		this._impl = @delegate;
	} 

	public void Intercept( IInvocation invocation )
	{
		var result = this._impl.DynamicInvoke( invocation.Arguments );
		invocation.ReturnValue = result;
	}
}

As you can see the interceptor implementation is trivial. We could make it faster, but its not the goal of this tutorial.

Notice also that we do not call invocation.Proceed(). Since there’s no real implementation to proceed to, this would be illegal.

If we did this we would get a NotImplementedException with the following message: “This is a DynamicProxy2 error: the interceptor attempted to ‘Proceed’ for a method without a target, for example, an interface method or an abstract method“.

Keep in mind that interceptors create a pipeline (see the picture in the 2nd part of the tutorial). As such only the last interceptor must not call Proceed, all others should.

Now we can move, and implement DelegateWrapper. It’s as trivial as MethodInterceptor.

public class DelegateWrapper
{
	public static T WrapAs<T>(Delegate impl) where T : class
	{
		var generator = new ProxyGenerator();
		var proxy = generator.CreateInterfaceProxyWithoutTarget( typeof( T ), new MethodInterceptor( impl ) );
		return (T) proxy;
	}
}

That’s all, roughly a dozen lines of meaningful code to make the test pass. If you’re scratching your head wondering why I put where T : class generic constraint, it’s because class in this context actually means ‘reference type’. This may not be the best example in the world, but hopefully by now you see the potential capabilities interface proxies without target give you.

Just for kicks, we might want to extend the DelegateWrapper to handle interfaces with more than one method. Let’s write a test for that.

[Fact]
public void Should_be_able_to_write_interface_with_two_methods()
{
	Func<string, string, bool> compare = ( s1, s2 ) => s1.Length.Equals( s2.Length );
	Func<string, int> getHashCode = s => s.Length.GetHashCode();
	var comparer = DelegateWrapper.WrapAs<IEqualityComparer<string>>( compare, getHashCode );
	var stringByLength = new Dictionary<string, string>( comparer )
	{
		{ "four", "some string" },
		{ "five!", "some other string" }
	};
	Assert.Equal( 2, stringByLength.Count );
	var atFive = stringByLength["12345"];
	Assert.Equal( "some other string", atFive );
}  

We now need some way of deciding which delegate should be bound to which method. InterceptorSelector to the rescue!

public static TInterface WrapAs<TInterface>(Delegate d1, Delegate d2)
{
	var generator = new ProxyGenerator();
	var options = new ProxyGenerationOptions { Selector = new DelegateSelector() };
	var proxy = generator.CreateInterfaceProxyWithoutTarget(
		typeof( TInterface ),
		new Type[0],
		options,
		new MethodInterceptor( d1 ),
		new MethodInterceptor( d2 ) );
	return (TInterface) proxy;
}

Here’s one very simple implementation of such selector:

internal class DelegateSelector : IInterceptorSelector
{
  public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors)
  {
	  foreach (var interceptor in interceptors)
	  {
		  var methodInterceptor = interceptor as MethodInterceptor;
		  if (methodInterceptor == null)
			  continue;
		  var d = methodInterceptor.Delegate;
		  if (IsEquivalent(d, method))
			  return new[] { interceptor };
	  }
	  throw new ArgumentException();
  } 

  private static bool IsEquivalent(Delegate d, MethodInfo method)
  {
	  var dm = d.Method;
	  if (!method.ReturnType.IsAssignableFrom(dm.ReturnType))
		  return false;
	  var parameters = method.GetParameters();
	  var dp = dm.GetParameters();
	  if (parameters.Length != dp.Length)
		  return false;
	  for (int i = 0; i < parameters.Length; i++)
	  {
		  //BUG: does not take into account modifiers (like out, ref...)
		  if (!parameters[i].ParameterType.IsAssignableFrom(dp[i].ParameterType))
			  return false;
	  }
	  return true;
  }
} 

Notice that I also extended MethodInterceptor class to expose its delegate as a property.

On a side note, if you still don’t see how powerful interface proxies without target are, think about WCF proxies – that is exactly what they are.

Technorati Tags: , ,

Comparing execution speed of .NET dynamic proxy frameworks

A recent comment on Twitter from Tim Barcz started me thinking about alternative proxy frameworks (alternative to Castle Dynamic Proxy that is). Recently LinFu Dynamic Proxy started gaining some popularity, after it was included as one of standard bytecode providers in NHibernate.

Disclaimer:

To make things clear. I’m a long time user of Castle Dynamic proxy and I may be biased towards it. I also happen to know how to use it, contrary to all the other frameworks, that’s why the following test may not be realizing their full potential. If you spot a non-optimal use of any of the frameworks in the following test, let me know and I’ll update it.

Having said that, I put together a small sample test project, where I generate a one-interceptor proxy for a simple interface with each framework and call a method on this proxy 100,000 times.

The interface is as simple as it can be:

public interface IMyInterface1
{
    string Method1(int i);
}

Each interceptor looks roughly the same, and it only sets a return value for the method:

internal class SpringInterceptor : AopAlliance.Intercept.IMethodInterceptor
{
    public object Invoke(AopAlliance.Intercept.IMethodInvocation invocation)
    {
        //Debug.WriteLine("Hey, I intercepted the call! - Spring");
        return 5.ToString();
    }
}

I tested four frameworks. Castle Dynamic Proxy 2.1 RC1, LinFu Dynamic Proxy 1.01, Spring.NET 1.2, which is a big framework in itself, but it has some pretty powerful proxying capabilities, and Microsoft Unity 1.2 which has now some proxying capabilities as well.

Here’s the result of the test on my 4 year old, 2GHz one-core PC:

proxy_frameworks_1

The first pass was a warm up, to let the jitter kick in, so the really important stuff, is the second pass.

Castle Dynamic Proxy was the fastest one, which is not really a surprise for me. It’s designed to be fast and lightweight, and also has some pretty powerful capabilities that allow you to customize the proxy type being created so that you don’t waste cycles intercepting methods you don’t want to intercept, or calling interceptors you don’t need called for a particular method. (for an in-depth overview of Castle Dynamic Proxy see my tutorial series).

The second one was Spring.NET which took over 3 times longer. This is however still pretty fast, and considering that it’s a pretty extensible and configurable framework as well, I’m sure in real life scenario you can squeeze very good performance out of it.

Third one, Unity was an order of magnitude slower than Castle. This is not a surprise considering how bloated and over engineered it is. To be fair however, this may as well be due to strong tie between Unity IoC container and proxy framework. The proxy framework does not seem to be operable outside of the container, so the performance hit, may be partially due to some container overhead.

LinFu was two orders of magnitude slower than Castle. This was really surprising. Or at least until I noticed that it gathers a call stack for each and every invocation, and as I mentioned, this can (as clearly visible) be a performance overkill. I looked for a way to turn it off, but it seems there isn’t any. Also the only way of calling the intercepted method is via MethodInfo.Invoke, which is also a lot slower than direct invocation (or invocation via a delegate).

Note that this is an isolated case of a single performance test. I thought it’s the most important one, but it’s just my opinion. I also didn’t talk about capabilities, which should be your major concern when selecting a framework. – Can it support my scenario is the ultimate test. Frankly, the fastest two, are also the most mature and powerful.

You can check the complete code here.

UPDATE:

Here’s a screenshot from a profiler showing the LinFu’s proxy call tree. As you can clearly see, the major performance hit (over 90% of the time!) is in StackTrace constructor. It also obtains a MethodInfo dynamically instead of caching it, like other frameworks do, which is an expensive operation as well.

LinFu

UPDATE 2:

LinFu has been updated and it no longer collects stack trace information: I re-ran the test and you can see the results here.

 

Castle Dynamic Proxy 2.1 RC1 has been released

Castle logo

Jono just released a RC1 of next version of Castle Dynamic Proxy – 2.1. As compared to version v2.0, which was released as part of RC3 of Castle, it contains numerous bugfixes, and few new features (support for IInterceptorSelector, mixins and few others) as well as improved documentation (something that is going to improve much, by the time final 2.1 is released).

This release is the first one since the split of monolithic Castle bundle into independent projects.

You can get the binaries here.

Castle Dynamic Proxy tutorial part VII: Kinds of proxy objects

This is part seven of my tutorial on Castle Dynamic Proxy.

So far all we have been doing with our simple freezable library is creating a proxies for classes. Not only that, we also introduced one artificial limitation – proxied classes had to have default, parameterless constructor. You can see it by writing the following test:

[Fact]
public void Freezable_should_be_able_to_call_nonDefault_constructor()
{
    var dog = Freezable.MakeFreezable<Dog>("Rex");
    Assert.Equal("Rex", dog.Name);
}
 
public class Dog
{
    public Dog(string name)
    {
        Name = name;
    }
 
    public virtual string Name { get; set; }
}

This test however will not compile.

dptutorial_7_no_default_constructor_error

If we tried to proxy a class with no such constructor, we would get a compilation error.

The compile time error would be a result of the new() generic type constraint we put in MakeFreezable method. It is important to understand that it has nothing to do with Castle Dynamic Proxy. If we removed the constraint we would be able to compile the code, and then get the exception from Dynamic Proxy at runtime: “System.MissingMethodException : Constructor on type ‘DogProxyc319d54e871741668ed468410d1b1013’ not found”.

There is however an overload of CreateClassProxy method that allows you to pass parameters to proxied type constructor. All we need to do, is to refactor the MakeFreezable method.

public static TFreezable MakeFreezable<TFreezable>(params object[] ctorArguments) where TFreezable : class
{
    var freezableInterceptor = new FreezableInterceptor();
    var options = new ProxyGenerationOptions(new FreezableProxyGenerationHook()) { Selector = _selector };
    var proxy = _generator.CreateClassProxy(typeof(TFreezable), new Type[0], options, ctorArguments,
                                            new CallLoggingInterceptor(), freezableInterceptor);
    return proxy as TFreezable;
}

We remove the new() constraint, since it obviously is not needed. We also add an array parameter to the method that we can use to pass arguments to the proxied type constructor. We can use params C# keyword to make the calling of the method a little bit more convenient.

Note that, since we’re passing the arguments as untyped array of objects, you can still get a runtime error if you pass incorrect arguments. This solution is also not refactoring friendly, so If you refactor your constructor and for example reorder its parameters, the parameters passed to MakeFreezable will not be reordered. You might want to change this by using lambda expressions as I described here a while ago. With that you get refactoring support and strong typing.

Other than class proxies, Proxy generator class is able to create three different kinds of interface proxies:

dptutorial_7_proxy_kinds

  • Proxy with target. This one is very easy to explain. We want to proxy an interface. Since interface can’t exist on its own, we need a class that implements it. The instance of this class is the target. It’s very similar to the class proxy we’ve been talking about.
  • Proxy without target. This one is tricky. You don’t supply target implementation for the interface. Dynamic Proxy generates it for you at runtime, using interceptors to provide behavior for its methods.
  • Proxy with target interface. This one is quite similar to proxy with target. You do have to provide a target implementation for the interface. The difference is, you can swap it later, and make the method be called on some other object.

We will cover each kind of interface proxy in forthcoming parts of the tutorial, so that hopefully this will all become crystal clear.

If you have any suggestions or questions, feel free to leave a comment.

Technorati Tags: ,

Castle Dynamic Proxy tutorial part VI: handling non-virtual methods

This is part six of my tutorial on Castle Dynamic Proxy.

We talked about how Dynamic Proxy proxying mechanism works, but so far we only briefly spoke about its limitations. The main limitation is the fact, that it can not intercept non-virtual calls. This may be an issue of varying importance.

In case of libraries such as NHibernate, which uses DynamicProxy for lazy loading, when your member is not virtual, you can’t lazy load it. This may be a drawback for performance, but your program should be able to work nonetheless.
On the other hand, for libraries like Rhino.Mocks, or MoQ, that use DynamicProxy for creating mocks and stubs, not being able to intercept a call, means you can’t mock its behavior and the response. This is a critical issue.
Similarly in case of our sample Freezable library, if a setter is not virtual, we can’t intercept calls to it, and that means we can not warrant that the state of the object will not change.

When we talked about ProxyGenerationHook, I mentioned that it has a NonVirtualMemberNotification method, that gets called when proxy generator encounters method it can not proxy.
In case of freezable library, we took simplistic assumption that state of the object can only change via property setters. In this case if just any method we encounter is non-virtual we don’t care. Since we assume that it can’t change the state of our object anyway, we may as well let it be non-virtual and still deliver on our promise.
However, if we encounter a non-virtual property setter, we should throw an exception to indicate that we deny taking any responsibility for this objects immutability.

Enough theory – let’s write some tests to get a cleaner picture of what we want to achieve.

[Fact] 
public void Freezable_should_freeze_classes_with_nonVirtual_methods() 
{ 
    var pet = Freezable.MakeFreezable<WithNonVirtualMethod>(); 
    pet.Name = "Rex"; 
    pet.NonVirtualMethod(); 
} 
 
[Fact] 
public void Freezable_should_throw_when_trying_to_freeze_classes_with_nonVirtual_setters() 
{ 
    var exception = Assert.Throws<InvalidOperationException>( () => 
        Freezable.MakeFreezable<WithNonVirtualSetter>() ); 
    Assert.Equal( 
        "Property NonVirtualProperty is not virtual. Can't freeze classes with non-virtual properties.", 
        exception.Message ); 
} 

If we run the tests the first one will pass, the second one will fail. Is that a surprise? We don’t have any checking code in place, so when we encounter a non virtual member, we do nothing. that’s why we effectively ignored the NonVirtualMethod in the first test (which is the behavior we want), but we also ignored the non-virtual property setter in the second test, which is a no-no.

To make them pass, we need to actually implement the NonVirtualMemberNotification method, to throw an exception when it encounters a property setter. There’s no specific exception type we’re obligated to throw, so we’ll just throw InvalidOperationException as specified in our test.

Here’s the implementation:

public void NonVirtualMemberNotification(Type type, MemberInfo memberInfo) 
{ 
    var method = memberInfo as MethodInfo; 
    if(method!=null) 
    { 
        this.ValidateNotSetter( method ); 
    } 
} 
 
private void ValidateNotSetter(MethodInfo method) 
{ 
    if( method.IsSpecialName && IsSetterName( method.Name ) ) 
        throw new InvalidOperationException( 
            string.Format( 
                "Property {0} is not virtual. Can't freeze classes with non-virtual properties.", 
                method.Name.Substring( "set_".Length ) 
                ) 
            ); 
} 

Similar to ShouldInterceptMethod method MarshalByRefObject and Object classes are special cases, and by default DynamicProxy will just ignore them.

With this change, all our tests now pass.

Other than the two methods we already discussed, IProxyGenerationHook has one more method: MethodsInspected. It gets called, after all methods on proxied type have been inspected, so it is the last method to be called by proxy generator. It is useful in cases when you hold some precious resources needed by two other methods.
For example if our hook implementation asked some external service (like a WCF service or database) about what to do with non-virtual methods, MethodsInspected would be the place to close the connection and dispose of all resources that are no longer needed.

With this part we basically covered almost all of basics of Dynamic Proxy. In the next part we’ll discuss other kinds of proxies you can create with Dynamic Proxy (yes, there are a few). Then we’ll talk about more advanced scenarios, like mixins.

I’m still open for feedback, so if you feel I missed some important topic or should have expand on something let me know in the comments.

The code, as always, is here.

Technorati Tags: , ,

Castle Dynamic Proxy tutorial part V: InterceptorSelector, fine grained control over proxying

This is part five of my tutorial on Castle Dynamic Proxy.

Our Freezable library is starting to work quite well. However, there are still few glitches we need to polish. First of all, take a look at following screenshot from our sample application.

dptutorial_5_app_before

We have the support for interception and logging in place, but currently it’s an all or nothing scenario. We started with intercepting every single method. Then we decided to lower the overhead, and intercept only property setters. Neither of these solutions is perfect.
The first one allowed us to be more flexible. We could log all methods, ensure state immutability of the others, and if we had more interceptors, specific to some subset of methods, we could use them too. The downside of that approach was, that each interceptor was called for each method.
To remedy that, we used ProxyGenerationHook to choose only the methods we wanted to intercept. That cut it, but only partially. We still have every interceptor being called for each and every of those methods we decided to intercept.
What we’d really want, is a more fine grained control, that allows us to not only say which methods we want to intercept, but also with which interceptors.
First to do that, we need to introduce some changes to out project. For now, Logging interceptor was the one that kept count of all its invocation. Since we no longer want to have each interceptor called each time a method is intercepted, we will factor out the Count property to a new interface IHasCount, and make all interceptors implement it.
Also we will change the GetInterceptedMethodsCountFor to return a count for specific interceptor.

With that we can now specify our new requirements with a new test.

[Fact]
public void Freezable_should_log_getters_and_setters()
{
    var pet = Freezable.MakeFreezable<Pet>();
    pet.Age = 4;
    var age = pet.Age;
    int logsCount = GetInterceptedMethodsCountFor<CallLoggingInterceptor>( pet );
    int freezeCount = GetInterceptedMethodsCountFor<FreezableInterceptor>( pet );
    Assert.Equal( 2, logsCount );
    Assert.Equal( 1, freezeCount );
}

As expected, if we run the test now, it will fail. So how do we make it pass?
ProxyGenerationOptions class, has a property Selector of type IInterceptorSelector, that we’ll use to achieve that. The interface contains just one method:
dptutorial_5_iinterceptorSelector
The method gets called for each method being intercepted. It receives information about proxied type, the method, and an array of all interceptors registered with the proxy. It is expected to act upon this information and return these interceptors it wishes to be used for the method. Also, while ProxyGenerationHook’s ShouldInterceptMethod method gets called once, when generating proxy type, InterceptorSelector’s SelectInterceptors method is called for each instance of that type, just before the first call to that method.

So, what we need is to inspect the given method to see if it is a property setter and if it is not, not return the FreezableInterceptor.

public class FreezableInterceptorSelector : IInterceptorSelector
{
    public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors)
    {
        if (IsSetter(method))
            return interceptors;
        return interceptors.Where( i => !( i is FreezableInterceptor ) ).ToArray();
    }
 
    private bool IsSetter( MethodInfo method )
    {
        return method.IsSpecialName && method.Name.StartsWith( "set_", StringComparison.Ordinal );
    }
}

We also need to change Freezable.MakeFreezable method to actually make use of our new FreezableInterceptor.

private static readonly IInterceptorSelector _selector = new FreezableInterceptorSelector();
 
public static TFreezable MakeFreezable<TFreezable>() where TFreezable : class, new()
{
    var freezableInterceptor = new FreezableInterceptor();
    var options = new ProxyGenerationOptions(new FreezableProxyGenerationHook()) { Selector = _selector };
    var proxy = _generator.CreateClassProxy(typeof(TFreezable), options, new CallLoggingInterceptor(), freezableInterceptor);
    return proxy as TFreezable;
}

If we run the tests however, they will all pass, except for the new one. That is because we still select for interception only property setters. We need to update our FreezableProxyGenerationHook. To demonstrate how selector and proxy generation hook work together we’ll instruct the hook to not intercept methods, only properties. Here’s the updated code:

public bool ShouldInterceptMethod(Type type, MethodInfo memberInfo)
{
    return memberInfo.IsSpecialName &&
           ( IsSetterName( memberInfo.Name ) ||
             IsGetterName( memberInfo.Name ) );
}
 
private bool IsGetterName( string name )
{
    return name.StartsWith( "get_", StringComparison.Ordinal );
}
 
private bool IsSetterName( string name )
{
    return name.StartsWith("set_", StringComparison.Ordinal);
}

And the test to check if this actually works:

[Fact]
public void Freezable_should_not_intercept_methods()
{
 
    var pet = Freezable.MakeFreezable<Pet>();
    pet.ToString();
    int logsCount = GetInterceptedMethodsCountFor<CallLoggingInterceptor>(pet);
    int freezeCount = GetInterceptedMethodsCountFor<FreezableInterceptor>(pet);
 
    // base implementation of ToString calls each property getter, that we intercept
    // so there will be 3 calls if method is not intercepter, otherwise 4.
    Assert.Equal(3, logsCount);
    Assert.Equal(0, freezeCount);
}

There’s one not immediately obvious thing about that test. We check if we can intercept ToString method. In this particular case we get to decide, if we want that method intercepted or not. However if you set a breakpoint in the ShouldIntercept method, you’ll notice, that we don’t get asked that question for any other virtual method inherited from System.Object (GetHashCode, Equals).
That is because DynamicProxy not allows you to intercept System.Object’s (or System.MarshalByRefObject’s) methods, unless they’re overridden in the proxy type (or in any of its base types). Since Pet class does override ToString, we get the opportunity to intercept this method, whereas all other methods are not overridden so we can’t proxy them.

Here, for comparision is the screenshot from our application after introducing those changes:

dptutorial_5_app_after

The code is here.

Technorati Tags: , ,

Castle Dynamic Proxy tutorial part IV: breaking hard dependencies

This is part four of my tutorial on Castle Dynamic Proxy.

In the last part of the tutorial we created a method GetInterceptedMethodsCountFor that I promised I’ll talk about soon. While we’re at it, we’re going to fix another design flaw of our Freezable class.

To do its work, it holds a hard reference to each and every freezable object it creates. This is obviously not a big deal if you create only a handful of freezable objects that are we want to be alive for the entire time the application is running.

private static readonly IDictionary<object, IFreezable> _freezables = new Dictionary<object, IFreezable>();

However, in most cases the objects we create are transient, and we create a lot of them. Looking at it from this perspective – we have a memory leak. Freezable class holds a reference to the objects, so garbage collector can not collect them and reclaim the memory they occupy, even though we may not use these objects ever again.

So how do we fix it? For now we’ll use evil hack I introduced in GetInterceptedMethodsCountFor method in the last part. Ultimately, we’re arrive at much nicer solution when we talk about mixins. But let’s not get ahead of ourselves.

If you took a look at the code from the previous part, the method is implemented as follows:

private int GetInterceptedMethodsCountFor(object freezable)
{
    Assert.True(Freezable.IsFreezable(freezable));
 
    var hack = freezable as IProxyTargetAccessor;
    Assert.NotNull(hack);
    var loggingInterceptor = hack.GetInterceptors().
                                 Where(i => i is CallLoggingInterceptor).
                                 Single() as CallLoggingInterceptor;
    return loggingInterceptor.Count;
}

The evil hack is based on the fact, that each proxy generated by Castle Dynamic Proxy framework implements an IProxyTargetAccessor interface. If you take a look at how it looks, it has two getter methods:

dptutorial_4_IProxyTargetAccessor

DynProxyGetTarget returns the proxies object. In our case the proxied object is the proxy itself, as we can prove with the following test

[Fact]
public void DynProxyGetTarget_should_return_proxy_itself()
{
    var pet = Freezable.MakeFreezable<Pet>();
    var hack = pet as IProxyTargetAccessor;
    Assert.NotNull(hack);
    Assert.Same(pet, hack.DynProxyGetTarget());
}

This is logical if you consider how DynamicProxy creates class proxies. It does so by creating a subclass of proxied type. The whole interception magic happens in overridden virtual methods and the methods of actual intercepted type are called via base.MyMethod(args). That’s why IProxyGenerationHook contains a method that allows you to act upon methods that are not virtual and hence can not be intercepted. We’ll use this feature in forthcoming part of the tutorial.

The other method, GetInterceptors, returns the interceptors associated with given proxy. We can use this method to obtain the proxy interceptors without keeping any hard reference to it.

So out task for this part of the tutorial is to break the need for hard reference the Freezable class holds to freezable objects. We state the requirement with the following test:

[Fact]
public void Freezable_should_not_hold_any_reference_to_created_objects()
{
    var pet = Freezable.MakeFreezable<Pet>();
    var petWeakReference = new WeakReference(pet, false);
    pet = null;
    GC.Collect();
    Assert.False(petWeakReference.IsAlive, "Object should have been collected");
}

If you run this test now, it will fail (go on, see for yourself, don’t get my word on it).

Now let’s factor out the dependency with out new found tool. First we refactor IsFreezable method, to the following.

public static bool IsFreezable(object obj)
{
    if (obj == null)
        return false;
    var hack = obj as IProxyTargetAccessor;
    if (hack == null)
        return false;
    return hack.GetInterceptors().Count(i => i is IFreezable) > 0;
}

If we run out tests, the old tests will pass (great, we didn’t break anything), but the new test, will still fail, as we have two more methods to refactor. Let’s now go to the IsFrozen method. I’m going to cheat here however. While implementing the method, I noticed that there’s a portion of code that each method in the class will require, so I factored it out, to another method. With that, here’s changed code:

public static bool IsFreezable(object obj)
{
    return AsFreezable(obj) != null;
}
 
private static IFreezable AsFreezable(object target)
{
    if (target == null)
        return null;
    var hack = target as IProxyTargetAccessor;
    if (hack == null)
        return null;
    return hack.GetInterceptors().FirstOrDefault(i => i is FreezableInterceptor) as IFreezable;
}
 
public static bool IsFrozen(object obj)
{
    var freezable = AsFreezable(obj);
    return freezable != null && freezable.IsFrozen;
}

We added the AsFreezable method that returns either IFreezable implementation associated with given object, or null, if there isn’t any.

We have two more methods to refactor:

public static void Freeze(object freezable)
{
    var interceptor = AsFreezable(freezable);
    if (interceptor == null)
        throw new NotFreezableObjectException(freezable);
    interceptor.Freeze();
}

Freeze implementation is still very simple. The MakeFreezable<TFreezable>() method only puts newly created objects into the dictionary, so we can safely remove it from that method. We can now also delete the dictionary field, as it’s not used anymore.

If you run the tests now, you’ll see that they all pass, including the newly added one.

dptutorial_4_tests_passed

The code, including tests, is here.

Final words

Even though we fixed the memory leak issue, it still is not the best solution. That said, IProxyTargetAccessor is a useful interface and it’s good to know that its there when you need it, but most of the time you don’t need it and in almost every case there’s a better way to accomplish your goal, without using the interface.

It’s mostly intended for use in low level, framework infrastructure code, and if used anywhere else you should treat it as a warning sign.

Technorati Tags: , ,

Castle Dynamic Proxy tutorial part III: Selecting which methods to intercept

This is part three of my tutorial on Castle Dynamic Proxy.

We’ll start by updating our CallLoggingInterceptor class, so that we can use it in tests. What we need from it, is to enhance its functionality, so that it not only logs (to the Console) the calls, but also keeps a count of calls. To do that we add a property called Count, which gets incremented each time a method is called. It’s a trivial code, so I’ won’t show it here.

With that, we can now write our first test. We want property setters to fire tests for object freezability. Getters are safe in this regard and test always passes, so it’s an unnecessary overhead. We want our getters to not get intercepted, so let’s write a test for that.

[Fact]
public void Freezable_should_not_intercept_property_getters()
{
    var pet = Freezable.MakeFreezable<Pet>();
    Freezable.Freeze(pet);
    var notUsed = pet.Age; //should not intercept
    var interceptedMethodsCount = GetInterceptedMethodsCountFor(pet);
    Assert.Equal(0, interceptedMethodsCount);
}

Let us not concern ourselves with GetInterceptedMethodsCountFor method just yet. For now let’s only say that it just does what its name suggests. We’ll inspect its implementation later. Now, we run the test, and – as expected, it fails.

dptutorial_3_getter_failed_test

So how do we make it pass? What we need is some kind of mechanism that will enable us to choose, which methods we want to intercept, and which we don’t. Fortunately Dynamic Proxy enables that. It exposes a hook that enables us to plug in into its proxy generation pipeline, in form of… IProxyGenerationHook interface (you wouldn’t guess, would you?).

dptutorial_3_IProxyGenerationHook

Two really interesting methods are NonVirtualMemberNotification, that enables us to act, when user wants to proxy a type that has some non-virtual methods i.e. methods we can’t intercept. We’ll concentrate on that method in the next part of the tutorial.

What we need right now is ShouldInterceptMethod method, that enables us to decide whether we want to intercept a method or not. We know all we need, so let’s add a new class to our project, and implement the method, leaving two other methods empty just for now.

public class FreezableProxyGenerationHook:IProxyGenerationHook
{
    public bool ShouldInterceptMethod(Type type, MethodInfo memberInfo)
    {
        return !memberInfo.Name.StartsWith("get_", StringComparison.Ordinal);
    }
 
    public void NonVirtualMemberNotification(Type type, MemberInfo memberInfo)
    {
    }
 
    public void MethodsInspected()
    {
    }
}

OK, we have our proxy generation hook, but how do we actually hook into the pipeline? To do this we need to provide our hook to proxy generation method, so a good place to look is CreateClassProxy method on our generator. Indeed it has quite a few overloads.

dptutorial_3_CreateClassProxyOverloads

Some of them take additional parameter of type ProxyGenerationOptions, and this is exactly what we need. We will examine ProxyGenerationOptions class in details as we move through this tutorial, because it’s a very important class in advanced proxy related scenarios. Basically, as its name implies it’s a container for various options related to proxy generation process. As such it’s also used to pass out proxy generation hook to the proxy generator. We can pass our FreezableProxyGenerationHook as a constructor argument to ProxyGenerationOptions. We also need to cast generated object back to its type,as we’re using a non-generic version of CreateClassProxy now.

public static TFreezable MakeFreezable<TFreezable>() where TFreezable : class, new()
{
    var freezableInterceptor = new FreezableInterceptor();
    var options = new ProxyGenerationOptions(new FreezableProxyGenerationHook());
    var proxy = _generator.CreateClassProxy(typeof(TFreezable), options, new CallLoggingInterceptor(), freezableInterceptor);
    _freezables.Add(proxy, freezableInterceptor);
    return proxy as TFreezable;
}

With this change we can now see if our test passes…

dptutorial_3_getter_passed_test

…and it does, along with all other. Great. Now, our implementation takes care of property getters but how about all the other methods? Well they get intercepted too, which is not the desired behavior. To fix that, we need to slightly alter our ShouldInterceptMethod method to intercept only property setters

public bool ShouldInterceptMethod(Type type, MethodInfo memberInfo)
{
    return memberInfo.Name.StartsWith("set_", StringComparison.Ordinal);
}

(I did this in a test first manner, but I won’t bore you with screenshots of failed/passed tests. The tests however are in the attached code).

If we run the program now, we’ll see that only setters get intercepted.

dptutorial_3_program

In the next part of the tutorial we’ll take care of cases when we can’t ensure objects state is never changed, and we’ll discuss the implementation of GetInterceptedMethodsCountFor method from our tests.

In the meantime, if you have any questions use the “leave comment” feature of this blog, or ping me via GTalk, email or Skype.

Code is here.

Technorati Tags: , ,