PDA

View Full Version : Is this possible? Using Spring to return a response


johnman
04-19-2006, 04:55 PM
Hi,

Is this possible with the Spring.Net framework.

I basically want to be able to apply an attribute to a method so that it returns stubbed data if stubs are enabled (in a config file) and have it so that this code only appears in debug builds (as I wouldn't want this extra code to appear in release builds).

The reason for this is to easily provide default data without hardcoding it into the method while backend systems (or other classes even) are being developed.

Thanks,

John

Bruno Baia
04-20-2006, 12:18 AM
Hi John,

yes this is possible, but there is many ways to do it, it depends of what you want to do exactly.

First, If you want to control the return value of a method you can use a interception around advice :

public class MyAdvice : IMethodInterceptor
{
public object Invoke(IMethodInvocation invocation)
{
// call the target method if you need to...
object returnValue = invocation.Proceed();

// return stubbed data or something coherent
// with the return type of the target method
return new object();
}
}



Then, you need to specify pointcuts to define where advices will be applied during the execution of the program.
http://www.springframework.net/doc/reference/html/aop.html#aop-convenience-impls
Using an attribute to match the target method is possible with attribute pointcuts, but if you want to avoid any "technical" code, you can use Regular expression pointcuts.


-Bruno