PageRenderTime 9ms CodeModel.GetById 0ms RepoModel.GetById 2ms app.codeStats 0ms

/src/LinFu.Proxy/ProxyFactoryExtensions.cs

http://github.com/philiplaureano/LinFu
C# | 95 lines | 36 code | 6 blank | 53 comment | 0 complexity | 1109e51fa733d6906253151bd60d34e5 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using LinFu.AOP.Interfaces;
  6. using LinFu.Proxy.Interfaces;
  7. namespace LinFu.Proxy
  8. {
  9. /// <summary>
  10. /// Extends the <see cref="IProxyFactory"/> class to support
  11. /// instantiating proxy types.
  12. /// </summary>
  13. public static class ProxyFactoryExtensions
  14. {
  15. /// <summary>
  16. /// Uses the <paramref name="factory"/> to create a proxy instance
  17. /// that directly derives from the <paramref name="instanceType"/>
  18. /// and implements the given <paramref name="baseInterfaces"/>.
  19. /// The <paramref name="wrapper"/> instance, in turn, will be used
  20. /// to intercept the method calls made to the proxy itself.
  21. /// </summary>
  22. /// <param name="factory">The IProxyFactory instance that will be used to generate the proxy type.</param>
  23. /// <param name="instanceType">The type that will be intercepted by the proxy.</param>
  24. /// <param name="wrapper">The <see cref="IInvokeWrapper"/> instance that will be used to intercept method calls made to the proxy.</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. /// <summary>
  35. /// Uses the <paramref name="factory"/> to create a proxy instance
  36. /// that directly derives from the <paramref name="instanceType"/>
  37. /// and implements the given <paramref name="baseInterfaces"/>.
  38. /// The <paramref name="interceptor"/> instance, in turn, will be used
  39. /// to intercept the method calls made to the proxy itself.
  40. /// </summary>
  41. /// <param name="factory">The IProxyFactory instance that will be used to generate the proxy type.</param>
  42. /// <param name="instanceType">The type that will be intercepted by the proxy.</param>
  43. /// <param name="interceptor">The <see cref="IInterceptor"/> instance that will be used to intercept method calls made to the proxy.</param>
  44. /// <param name="baseInterfaces">The additional list of interfaces that the proxy will implement.</param>
  45. /// <returns>A valid proxy instance.</returns>
  46. public static object CreateProxy(this IProxyFactory factory, Type instanceType,
  47. IInterceptor interceptor, params Type[] baseInterfaces)
  48. {
  49. var proxyType = factory.CreateProxyType(instanceType, baseInterfaces);
  50. var proxyInstance = (IProxy)Activator.CreateInstance(proxyType);
  51. proxyInstance.Interceptor = interceptor;
  52. return proxyInstance;
  53. }
  54. /// <summary>
  55. /// Uses the <paramref name="factory"/> to create a proxy instance
  56. /// that directly derives from the <typeparamref name="T"/> type
  57. /// and implements the given <paramref name="baseInterfaces"/>.
  58. /// The <paramref name="wrapper"/> instance, in turn, will be used
  59. /// to intercept the method calls made to the proxy itself.
  60. /// </summary>
  61. /// <typeparam name="T">The type that will be intercepted by the proxy.</typeparam>
  62. /// <param name="factory">The IProxyFactory instance that will be used to generate the proxy type.</param>
  63. /// <param name="wrapper">The <see cref="IInvokeWrapper"/> instance that will be used to intercept method calls made to the proxy.</param>
  64. /// <param name="baseInterfaces">The additional list of interfaces that the proxy will implement.</param>
  65. /// <returns>A valid proxy instance.</returns>
  66. public static T CreateProxy<T>(this IProxyFactory factory, IInvokeWrapper wrapper,
  67. params Type[] baseInterfaces)
  68. {
  69. return (T) factory.CreateProxy(typeof (T), wrapper, baseInterfaces);
  70. }
  71. /// <summary>
  72. /// Uses the <paramref name="factory"/> to create a proxy instance
  73. /// that directly derives from the <typeparamref name="T"/> type
  74. /// and implements the given <paramref name="baseInterfaces"/>.
  75. /// The <paramref name="interceptor"/> instance, in turn, will be used
  76. /// to intercept the method calls made to the proxy itself.
  77. /// </summary>
  78. /// <typeparam name="T">The type that will be intercepted by the proxy.</typeparam>
  79. /// <param name="factory">The IProxyFactory instance that will be used to generate the proxy type.</param>
  80. /// <param name="interceptor">The <see cref="IInterceptor"/> instance that will be used to intercept method calls made to the proxy.</param>
  81. /// <param name="baseInterfaces">The additional list of interfaces that the proxy will implement.</param>
  82. /// <returns>A valid proxy instance.</returns>
  83. public static T CreateProxy<T>(this IProxyFactory factory, IInterceptor interceptor,
  84. params Type[] baseInterfaces)
  85. {
  86. return (T) factory.CreateProxy(typeof (T), interceptor, baseInterfaces);
  87. }
  88. }
  89. }