/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

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