PageRenderTime 5ms CodeModel.GetById 1ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.IoC/Interceptors/AroundInvokeAdapter.cs

http://github.com/philiplaureano/LinFu
C# | 61 lines | 31 code | 8 blank | 22 comment | 0 complexity | 63e9046084b326980234aa8cbed6ebd6 MD5 | raw file
 1using System;
 2using System.Reflection;
 3using LinFu.AOP.Interfaces;
 4using LinFu.IoC.Configuration.Interfaces;
 5
 6namespace LinFu.IoC.Interceptors
 7{
 8    /// <summary>
 9    ///     Adapts a <see cref="IAroundInvoke" /> instance into an <see cref="IInterceptor" />.
10    /// </summary>
11    internal class AroundInvokeAdapter : BaseInterceptor
12    {
13        private readonly Func<object> _getTarget;
14        private readonly IAroundInvoke _wrapper;
15
16        /// <summary>
17        ///     Initializes the <see cref="AroundInvokeAdapter" /> class.
18        /// </summary>
19        /// <param name="getTarget">The functor responsible for obtaining the target instance.</param>
20        /// <param name="methodInvoke">The method invoker.</param>
21        /// <param name="aroundInvoke">The target <see cref="IAroundInvoke" /> instance.</param>
22        internal AroundInvokeAdapter(Func<object> getTarget, IMethodInvoke<MethodInfo> methodInvoke,
23            IAroundInvoke aroundInvoke)
24            : base(methodInvoke)
25        {
26            _wrapper = aroundInvoke;
27            _getTarget = getTarget;
28        }
29
30        /// <summary>
31        ///     Converts the call to <see cref="IInterceptor.Intercept" /> to an
32        ///     <see cref="IAroundInvoke" /> method call.
33        /// </summary>
34        /// <param name="info">The <see cref="IInvocationInfo" /> that describes the context of the method call.</param>
35        /// <returns>The return value from the target method.</returns>
36        public override object Intercept(IInvocationInfo info)
37        {
38            object result = null;
39
40            // Signal the beginning of the method call
41            _wrapper.BeforeInvoke(info);
42
43            // Call the target method
44            result = base.Intercept(info);
45
46            // Postprocess the results
47            _wrapper.AfterInvoke(info, result);
48
49            return result;
50        }
51
52        /// <summary>
53        ///     Gets the target object instance.
54        /// </summary>
55        /// <param name="info">The <see cref="IInvocationInfo" /> instance that describes the current execution context.</param>
56        protected override object GetTarget(IInvocationInfo info)
57        {
58            return _getTarget();
59        }
60    }
61}