-
Notifications
You must be signed in to change notification settings - Fork 2
AOP Proxy Factories
The Spring.FluentContext provides also a simple API for AOP Proxy Factories creation.
Please see following resources to get more details about concept and standard usage in code:
- 13.5. Using the ProxyFactoryObject to create AOP proxies
- 13.7. Creating AOP Proxies Programatically with the ProxyFactory
The following example is presenting AOP Proxy Factory creation with fluent context:
class DisplayCommand : ICommand { /*...*/ }
// Configuration
ctx.RegisterDefault<DisplayCommand>();
ctx.RegisterDefault<RepeatingInterceptor>();
ctx.RegisterDefault<DelayingInterceptor>();
ctx.RegisterDefaultProxyFactory<ICommand>()
.TargetingDefault<DisplayCommand>()
.InterceptedByDefault<DelayingInterceptor>()
.InterceptedByDefault<RepeatingInterceptor>();
//Usage
ICommand command = ctx.GetObject<ICommand>();
command.Execute("text");
Example above shows creation of proxy factory for ICommand
interface that is targeting object of DisplayCommand
type, and registering two interceptors.
The ctx.GetObject<ICommand>()
is requesting instance of interface type, and as a result it will get proxy object for DisplayCommand
instance. Each interface method call like Execute("text")
would be intercepted (together with method arguments) by registered interceptors in their registration order.
It is possible to specify if proxy factory should return a new proxy each time it is requested, or always the same proxy. The ReturningSingleton(), ReturningPrototypes() methods are changing this behavior, where singletons are used by default.
To get new instance of target object each time, both proxy and target object have to be configured as prototypes:
ctx.RegisterDefault<DisplayCommand>()
.AsPrototype(); //every request for DisplayCommand should return new instance
ctx.RegisterDefaultProxyFactory<ICommand>()
.TargetingDefault<DisplayCommand>()
.ReturningPrototypes() //every request for ICommand should return new instance of proxy
.InterceptedByDefault<DelayingInterceptor>()
.InterceptedByDefault<RepeatingInterceptor>();
Continue reading: 15. General definition binding