I'd like to apply some interceptors to the Open method of a .NET DbConnection object. Since I'm proxying the object, I was expecting that even the composition-based proxy will still allow me to call the factory methods of IDbConnection, most important of which for my purposes is IDbConnection.CreateCommand. Unfortunately, when I attempt to retrieve the context in my .NET code, I'm confronted by an exception telling me that the proxy doesn't have a CreateCommand method. The message looks something like this:
Error creating context 'spring.root': Cannot find matching factory method 'CreateCommand on Type [CompositionAopProxy_18aab00c18884dce99c31f25d711df 1b].
To complicate matters further, when I examine typeof(CompositionAopProxy_18aab00c18884dce99c31f2 5d711df1b) in the immediate window, I'm able to call the GetInterfaces method and see that the proxy indeed does implement IDbConnection.
I was able to do something similar using Autofac and Castle DynamicProxy, so I'd be surprised if I couldn't do the same thing in Spring.NET.
Here's the configuration I'm using:
And the .NET code is simple:
<object id="Factory" factory-method="GetFactory"
<constructor-arg index="0" value="System.Data.SqlClient"/>
<object id="ProxiedConnection" type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop">
<property name="TargetName" value="Connection"/>
<property name="ProxyInterfaces" value="System.Data.IDbConnection"/>
<property name="CommandText" value="SELECT * FROM Composers"/>
Any help with getting this to work will be greatly appreciated!
var context = ContextRegistry.GetContext();
var command = context.GetObject("Command") as IDbCommand;