PageRenderTime 62ms CodeModel.GetById 30ms app.highlight 3ms RepoModel.GetById 27ms app.codeStats 0ms

/src/LinFu.AOP.Interfaces/SimpleMethodReplacementProvider.cs

http://github.com/philiplaureano/LinFu
C# | 60 lines | 23 code | 6 blank | 31 comment | 2 complexity | 4585f477ba438ebe3faf75168b9f308f MD5 | raw file
 1using System;
 2
 3namespace LinFu.AOP.Interfaces
 4{
 5    /// <summary>
 6    ///     Represents the simplest possible <see cref="IMethodReplacementProvider" /> implementation
 7    ///     that will allow the user to use the original method body implementation as part
 8    ///     of the interceptor call.
 9    /// </summary>
10    public class SimpleMethodReplacementProvider : BaseMethodReplacementProvider
11    {
12        /// <summary>
13        ///     Initializes a new instance of the <see cref="SimpleMethodReplacementProvider" /> class.
14        /// </summary>
15        /// <param name="replacement">The method body replacement interceptor.</param>
16        public SimpleMethodReplacementProvider(IInterceptor replacement)
17        {
18            MethodReplacement = replacement;
19        }
20
21        /// <summary>
22        ///     Gets or sets the value indicating the Predicate that will determine whether or not
23        ///     the method should be intercepted.
24        /// </summary>
25        /// <value>The interceptor predicate.</value>
26        public Func<IInvocationInfo, bool> MethodReplacementPredicate { get; set; }
27
28        /// <summary>
29        ///     Gets or sets the value indicating the actual <see cref="IInterceptor" />
30        ///     instance that will provide the method body implementations.
31        /// </summary>
32        /// <value>The interceptor that will swap the method bodies at runtime.</value>
33        public IInterceptor MethodReplacement { get; set; }
34
35        /// <summary>
36        ///     Determines whether or not a particular method body should be replaced at runtime.
37        /// </summary>
38        /// <param name="host">The host instance that contains the target method.</param>
39        /// <param name="context">The context surrounding the method call.</param>
40        /// <returns>Returns <c>true</c> if the method body should be swapped; otherwise, it will return <c>false</c>.</returns>
41        protected override bool ShouldReplace(object host, IInvocationInfo context)
42        {
43            if (MethodReplacementPredicate == null)
44                return true;
45
46            return MethodReplacementPredicate(context);
47        }
48
49        /// <summary>
50        ///     Gets the method replacement for a given <see cref="IInvocationInfo">invocation context</see>.
51        /// </summary>
52        /// <param name="host">The host instance that contains the target method.</param>
53        /// <param name="context">The context surrounding the method call.</param>
54        /// <returns>The interceptor that will swap the method bodies at runtime.</returns>
55        protected override IInterceptor GetReplacement(object host, IInvocationInfo context)
56        {
57            return MethodReplacement;
58        }
59    }
60}