View Full Version : Create a Proxy without Target !
sn00py
03-07-2006, 05:22 PM
Hi, is it possible to create a proxy without Target.
See the Sample :
public class ConsoleLoggingAroundAdvice : IMethodInterceptor
{
private remoteClass rclass = new remoteClass();
public object Invoke(IMethodInvocation invocation)
{
//Call a remote procedure
rclass.DoSomething();
}
}
public interface Iwork
{
void DoSomething();
}
class C1
{
[STAThread]
static void Main(string[] args)
{
Type type = Type.GetType("AOPProxyFactory.Iwork,AOPProxyFactory");
ProxyFactory factory = new ProxyFactory();
factory.AddInterface(type);
factory.AddAdvice(new ConsoleLoggingAroundAdvice() );
IWork operateur = (IWork) factory.GetProxy();//throw an exception
operateur.DoSomething();
}
}
Cdt,
sn00py
03-09-2006, 09:40 AM
I use Castle.Dynamic proxy like this :
ProxyGenerator gen = new ProxyGenerator();
//Create Castle.DynamicProxy
Type [] types = new Type[typeList.Count];
typeList.CopyTo(types,0);
object proxy = gen.CreateProxy(types,new CastleEmptyProxyInterceptor(),new object());
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory .Target = proxy ;
I intercept the method and i call my remote procédure ...
Mark Pollack
03-13-2006, 03:05 PM
Hi,
Yes, you do need to specify the target. There are a few ways to do that, the AOP QuickStart Application (http://www.springframework.net/doc/reference/html/aop-quickstart.html) code provided in the distribution shows the most common way which is to provide an instance of the target object to the constructor of ProxyFactory();
ProxyFactory factory = new ProxyFactory(new MyImplementationOfIWork());
...
This usage will proxy all interfaces exposed by the provided target and create a SingletonTargetSource from the provided target instance. The use of SingletonTargetSource means that the proxy will always delegate to the same target object instance for all interface methods that are proxied.
More advanced configuration would let you specify the interfaces to proxy explicitly. This gives you more control over what gets proxied. Assume that a class, MyImplementationOfManyIWorkInterfaces had implemented three interfaces, IWork, IWorkAdvanced, and IWorkAdmin. To only proxy the methods of the IWorkAdvanced interface you would do the following.
ProxyFactory factory = new ProxyFactory();
factory.AddInterface(typeof(IWorkAdvanced));
factory.Target = new MyImplementationOfManyIWorkInterfaces();
Even more advanced options let you specify the type of TargetSource to use. Spring provides three implementations aside from SingletonTargetSource, they are HotSwappableTargetSource (http://www.springframework.net/doc/reference/html/aop.html#aop-ts-swap), SimplePoolTargetSource (http://www.springframework.net/doc/reference/html/aop.html#aop-ts-pool), and PrototypeTargetSource (http://www.springframework.net/doc/reference/html/aop.html#aop-ts-prototype). The links will provide you with more information on their features.
Cheers,
Mark
Aleks Seovic
03-13-2006, 08:38 PM
I'm not sure what your use case is, but Spring.AOP requires a target. If all you want to do is create a proxy, you can use one of the base classes from Spring.Proxy namespace and implement proxy yourself any way you want, but that's not really an AOP-related feature.
Regards,
Aleks
Bruno Baia
06-12-2006, 11:42 PM
Hi,
I'm working on Spring.Aop atm, the TargetSource is never null, by default TargetSource == EmptyTargetSource.empty.
It's possible to do this since i added support for TransparentProxy in AOP Proxies (SPRNET-293 (http://opensource.atlassian.com/projects/spring/browse/SPRNET-293)), at least that's what i thought...
I just needed to correct a big mistake i did, and now it should work.
before fix :
if (targetType.IsAssignableFrom(intf))
after fix :
if (intf.IsAssignableFrom(targetType))
I know.... :D
Here is the test code (not added for now, incoming in the newCompositionAopProxyTests test class)
public void InterceptorIsInvokedWithNoTarget()
{
int age = 25;
DynamicMock mock = new DynamicMock(typeof(IMethodInterceptor));
IMethodInterceptor mi = (IMethodInterceptor) mock.Object;
AdvisedSupport advisedSupport = new AdvisedSupport(new Type[] { typeof(ITestObject) });
advisedSupport.AddAdvice(mi);
IAopProxy aop = CreateAopProxy(advisedSupport);
mi.Invoke(null);
mock.SetValue("Invoke", age);
ITestObject to = (ITestObject)aop.GetProxy();
Assert.IsTrue(to.Age == age, "Correct return value");
mock.Verify();
}
Maybe not a trivial sample, but your code should work too.
The JIRA issue : SPRNET-332 (http://opensource.atlassian.com/projects/spring/browse/SPRNET-332)
Cheers,
Bruno
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.