PageRenderTime 46ms CodeModel.GetById 23ms app.highlight 7ms RepoModel.GetById 13ms app.codeStats 1ms

/src/LinFu.Proxy.Interfaces/ProxyFactoryExtensions.cs

http://github.com/philiplaureano/LinFu
C# | 160 lines | 56 code | 11 blank | 93 comment | 0 complexity | b9a9ad94a54a1263f072c1804b7a4fdd MD5 | raw file
  1using System;
  2using LinFu.AOP.Interfaces;
  3
  4namespace LinFu.Proxy.Interfaces
  5{
  6    /// <summary>
  7    ///     Extends the <see cref="IProxyFactory" /> class to support
  8    ///     instantiating proxy types.
  9    /// </summary>
 10    public static class ProxyFactoryExtensions
 11    {
 12        /// <summary>
 13        ///     Uses the <paramref name="factory" /> to create a proxy instance
 14        ///     that directly derives from the <paramref name="instanceType" />
 15        ///     and implements the given <paramref name="baseInterfaces" />.
 16        ///     The <paramref name="wrapper" /> instance, in turn, will be used
 17        ///     to intercept the method calls made to the proxy itself.
 18        /// </summary>
 19        /// <param name="factory">The IProxyFactory instance that will be used to generate the proxy type.</param>
 20        /// <param name="instanceType">The type that will be intercepted by the proxy.</param>
 21        /// <param name="wrapper">
 22        ///     The <see cref="IInvokeWrapper" /> instance that will be used to intercept method calls made to
 23        ///     the proxy.
 24        /// </param>
 25        /// <param name="baseInterfaces">The additional list of interfaces that the proxy will implement.</param>
 26        /// <returns>A valid proxy instance.</returns>
 27        public static object CreateProxy(this IProxyFactory factory, Type instanceType,
 28            IInvokeWrapper wrapper, params Type[] baseInterfaces)
 29        {
 30            // Convert the wrapper to an IInterceptor instance.
 31            var adapter = new CallAdapter(wrapper);
 32            return factory.CreateProxy(instanceType, adapter, baseInterfaces);
 33        }
 34
 35        /// <summary>
 36        ///     Uses the <paramref name="factory" /> to create a proxy instance
 37        ///     that directly derives from the <paramref name="instanceType" />
 38        ///     and implements the given <paramref name="baseInterfaces" />.
 39        ///     The <paramref name="interceptor" /> instance, in turn, will be used
 40        ///     to intercept the method calls made to the proxy itself.
 41        /// </summary>
 42        /// <param name="factory">The IProxyFactory instance that will be used to generate the proxy type.</param>
 43        /// <param name="instanceType">The type that will be intercepted by the proxy.</param>
 44        /// <param name="interceptor">
 45        ///     The <see cref="IInterceptor" /> instance that will be used to intercept method calls made to
 46        ///     the proxy.
 47        /// </param>
 48        /// <param name="baseInterfaces">The additional list of interfaces that the proxy will implement.</param>
 49        /// <returns>A valid proxy instance.</returns>
 50        public static object CreateProxy(this IProxyFactory factory, Type instanceType,
 51            IInterceptor interceptor, params Type[] baseInterfaces)
 52        {
 53            var proxyType = factory.CreateProxyType(instanceType, baseInterfaces);
 54            var proxyInstance = (IProxy) Activator.CreateInstance(proxyType);
 55
 56            proxyInstance.Interceptor = interceptor;
 57
 58            return proxyInstance;
 59        }
 60
 61        /// <summary>
 62        ///     Uses the <paramref name="factory" /> to create a proxy instance
 63        ///     that directly derives from the <typeparamref name="T" /> type
 64        ///     and implements the given <paramref name="baseInterfaces" />.
 65        ///     The <paramref name="wrapper" /> instance, in turn, will be used
 66        ///     to intercept the method calls made to the proxy itself.
 67        /// </summary>
 68        /// <typeparam name="T">The type that will be intercepted by the proxy.</typeparam>
 69        /// <param name="factory">The IProxyFactory instance that will be used to generate the proxy type.</param>
 70        /// <param name="wrapper">
 71        ///     The <see cref="IInvokeWrapper" /> instance that will be used to intercept method calls made to
 72        ///     the proxy.
 73        /// </param>
 74        /// <param name="baseInterfaces">The additional list of interfaces that the proxy will implement.</param>
 75        /// <returns>A valid proxy instance.</returns>
 76        public static T CreateProxy<T>(this IProxyFactory factory, IInvokeWrapper wrapper,
 77            params Type[] baseInterfaces)
 78        {
 79            return (T) factory.CreateProxy(typeof(T), wrapper, baseInterfaces);
 80        }
 81
 82        /// <summary>
 83        ///     Uses the <paramref name="factory" /> to create a proxy instance
 84        ///     that directly derives from the <typeparamref name="T" /> type
 85        ///     and implements the given <paramref name="baseInterfaces" />.
 86        ///     The <paramref name="interceptor" /> instance, in turn, will be used
 87        ///     to intercept the method calls made to the proxy itself.
 88        /// </summary>
 89        /// <typeparam name="T">The type that will be intercepted by the proxy.</typeparam>
 90        /// <param name="factory">The IProxyFactory instance that will be used to generate the proxy type.</param>
 91        /// <param name="interceptor">
 92        ///     The <see cref="IInterceptor" /> instance that will be used to intercept method calls made to
 93        ///     the proxy.
 94        /// </param>
 95        /// <param name="baseInterfaces">The additional list of interfaces that the proxy will implement.</param>
 96        /// <returns>A valid proxy instance.</returns>
 97        public static T CreateProxy<T>(this IProxyFactory factory, IInterceptor interceptor,
 98            params Type[] baseInterfaces)
 99        {
100            return (T) factory.CreateProxy(typeof(T), interceptor, baseInterfaces);
101        }
102
103        /// <summary>
104        ///     Uses the <paramref name="proxyFactory" /> to create a proxy instance
105        ///     that directly derives from the <typeparamref name="T" /> type
106        ///     and implements the given <paramref name="baseInterfaces" />.
107        /// </summary>
108        /// <remarks>
109        ///     The <paramref name="proxyImplementation" /> will be used to intercept method calls
110        ///     performed against the target instance.
111        /// </remarks>
112        /// <typeparam name="T">The type that will be intercepted by the proxy.</typeparam>
113        /// <param name="proxyFactory">The IProxyFactory instance that will be used to generate the proxy type.</param>
114        /// <param name="proxyImplementation">The functor that will invoked every time a method is called on the proxy.</param>
115        /// <param name="baseInterfaces">The additional list of interfaces that the proxy will implement.</param>
116        /// <returns>A valid proxy instance.</returns>
117        public static T CreateProxy<T>(this IProxyFactory proxyFactory,
118            Func<string, Type[], object[], object> proxyImplementation,
119            params Type[] baseInterfaces)
120        {
121            var targetType = typeof(T);
122            var result = CreateProxy(proxyFactory, targetType, proxyImplementation, baseInterfaces);
123
124            return (T) result;
125        }
126
127        /// <summary>
128        ///     Uses the <paramref name="proxyFactory" /> to create a proxy instance
129        ///     that directly derives from the given> type
130        ///     and implements the given <paramref name="baseInterfaces" />.
131        /// </summary>
132        /// <remarks>
133        ///     The <paramref name="proxyImplementation" /> will be used to intercept method calls
134        ///     performed against the target instance.
135        /// </remarks>
136        /// <param name="targetType">The type that will be intercepted by the proxy.</param>
137        /// <param name="proxyFactory">The IProxyFactory instance that will be used to generate the proxy type.</param>
138        /// <param name="proxyImplementation">The functor that will invoked every time a method is called on the proxy.</param>
139        /// <param name="baseInterfaces">The additional list of interfaces that the proxy will implement.</param>
140        /// <returns>A valid proxy instance.</returns>
141        public static object CreateProxy(this IProxyFactory proxyFactory, Type targetType,
142            Func<string, Type[], object[], object> proxyImplementation,
143            params Type[] baseInterfaces)
144        {
145            Func<IInvocationInfo, object> doIntercept = info =>
146            {
147                var targetMethod = info.TargetMethod;
148                var methodName = targetMethod.Name;
149                var arguments = info.Arguments;
150                var typeArguments = info.TypeArguments;
151
152                return proxyImplementation(methodName, typeArguments,
153                    arguments);
154            };
155
156            var interceptor = new FunctorAsInterceptor(doIntercept);
157            return proxyFactory.CreateProxy(targetType, interceptor, baseInterfaces);
158        }
159    }
160}