Mugen Injection [Interception]
The MugenInjection interception extension provides ability to intercept events, properties and methods call. To start working with extension, you should:
- Add reference to the MugenInjection.Core.dll and MugenInjection.Interception.dll.
- At application startup write binding with interception.
- Use this binding during application execution.
Important Note
Your methods/properties to be intercepted must be virtual! If you look at all examples in this series, all intercepted members are virtual.Types of interceptions
The MugenInjection provides two type of interception:- Intercept to a new proxy-class.
- Intercept to a new proxy-class with target.
_injector = new MugenInjector(); _injector.Bind<Alpha>().Intercept(new InterceptorProcess()).InSingletonScope(); //Getting proxy of alpha. var alpha = _injector.Get<Alpha>();
_injector = new MugenInjector(); _injector.Bind<Alpha>().ToSelf().InSingletonScope().InterceptAsTarget(new InterceptorProcess()); //Getting proxy of alpha with target of Alpha. var alpha = _injector.Get<Alpha>();
Interception handlers
The MugenInjection provides two type of interception handler:- ISimpleInterceptorProcess - intercepts the call to one action related to methods, properties, and events.
- IInterceptorProcess - intercepts the call to action related to methods, properties, and events.
/// <summary> /// Intercepts the call to action related to methods, properties, and events. /// </summary> public interface ISimpleInterceptorProcess { /// <summary> /// Gets the priority. /// </summary> int Priority { get; } /// <summary> /// Intercepts all actions. /// </summary> /// <param name="interceptor">The specified <see cref="IInterceptor"/>.</param> void Intercept(IInterceptor interceptor); }
/// <summary> /// Intercepts the call to action related to methods, properties, and events. /// </summary> public interface IInterceptorProcess { /// <summary> /// Gets the priority. /// </summary> int Priority { get; } /// <summary> /// Intercepts the method using the specified <see cref="IMethodInterceptor"/>. /// </summary> /// <param name="methodInterceptor">The specified <see cref="IMethodInterceptor"/>.</param> void InterceptMethod(IMethodInterceptor methodInterceptor); /// <summary> /// Intercepts the get property method using the specified <see cref="IPropertyGetInterceptor"/>. /// </summary> /// <param name="propertyGetInterceptor">The specified <see cref="IPropertyGetInterceptor"/></param> void InterceptGetProperty(IPropertyGetInterceptor propertyGetInterceptor); /// <summary> /// Intercepts the set property method using the specified <see cref="IPropertySetInterceptor"/>. /// </summary> /// <param name="propertySetInterceptor">The specified <see cref="IPropertySetInterceptor"/>.</param> void InterceptSetProperty(IPropertySetInterceptor propertySetInterceptor); /// <summary> /// Intercepts the add event method using the specified <see cref="IEventAddInterceptor"/>. /// </summary> /// <param name="eventAddInterceptor">The specified <see cref="IEventAddInterceptor"/>.</param> void InterceptAddEvent(IEventAddInterceptor eventAddInterceptor); /// <summary> /// Intercepts the remove event method using the specified <see cref="IEventRemoveInterceptor"/>. /// </summary> /// <param name="eventRemoveInterceptor">The specified <see cref="IEventRemoveInterceptor"/>.</param> void InterceptRemoveEvent(IEventRemoveInterceptor eventRemoveInterceptor); }
Fluent syntax to add interceptors
The MugenInjector also provides the opportunity to register interception handlers when you register binding.This code shows how to add handlers to intercept, 'get-property', 'set-property' and 'method call':
public class Alpha { public virtual string Property { get; set; } public virtual void TestMethod() { } } _injector = new MugenInjector(); _injector.Bind<Alpha>().Intercept() .InterceptGetProperty(alpha => alpha.Property, (target, value, interceptor) => { //target-contains the target, if any. //value-contains the func to get original value, if any. //interceptor-represents the interceptor to intercept property-get. Console.WriteLine("Get property call"); return "test"; }) .InterceptSetProperty(alpha => alpha.Property, (target, value, interceptor) => { //target-contains the target, if any. //value-contains the set value. //interceptor-represents the interceptor to intercept property-set. Console.WriteLine("Set property call"); return "setProperty"; }) .InterceptInvokeMethod((context, alpha) => alpha.TestMethod(), (target, original, interceptor) => { //target-contains the target, if any. //original-contains the delegate to invoke original method. //interceptor-represents the interceptor to intercept method-call. Console.WriteLine("Method call"); }); var a = _injector.Get<Alpha>(); //property equals to "test" var property = a.Property; a.Property = "value"; //property equals to "test" property = a.Property; a.TestMethod();