AspectAttribute.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Reflection;
  3. namespace Abp.Aspects
  4. {
  5. //THIS NAMESPACE IS WORK-IN-PROGRESS
  6. internal abstract class AspectAttribute : Attribute
  7. {
  8. public Type InterceptorType { get; set; }
  9. protected AspectAttribute(Type interceptorType)
  10. {
  11. InterceptorType = interceptorType;
  12. }
  13. }
  14. internal interface IAbpInterceptionContext
  15. {
  16. object Target { get; }
  17. MethodInfo Method { get; }
  18. object[] Arguments { get; }
  19. object ReturnValue { get; }
  20. bool Handled { get; set; }
  21. }
  22. internal interface IAbpBeforeExecutionInterceptionContext : IAbpInterceptionContext
  23. {
  24. }
  25. internal interface IAbpAfterExecutionInterceptionContext : IAbpInterceptionContext
  26. {
  27. Exception Exception { get; }
  28. }
  29. internal interface IAbpInterceptor<TAspect>
  30. {
  31. TAspect Aspect { get; set; }
  32. void BeforeExecution(IAbpBeforeExecutionInterceptionContext context);
  33. void AfterExecution(IAbpAfterExecutionInterceptionContext context);
  34. }
  35. internal abstract class AbpInterceptorBase<TAspect> : IAbpInterceptor<TAspect>
  36. {
  37. public TAspect Aspect { get; set; }
  38. public virtual void BeforeExecution(IAbpBeforeExecutionInterceptionContext context)
  39. {
  40. }
  41. public virtual void AfterExecution(IAbpAfterExecutionInterceptionContext context)
  42. {
  43. }
  44. }
  45. internal class Test_Aspects
  46. {
  47. internal class MyAspectAttribute : AspectAttribute
  48. {
  49. public int TestValue { get; set; }
  50. public MyAspectAttribute()
  51. : base(typeof(MyInterceptor))
  52. {
  53. }
  54. }
  55. internal class MyInterceptor : AbpInterceptorBase<MyAspectAttribute>
  56. {
  57. public override void BeforeExecution(IAbpBeforeExecutionInterceptionContext context)
  58. {
  59. Aspect.TestValue++;
  60. }
  61. public override void AfterExecution(IAbpAfterExecutionInterceptionContext context)
  62. {
  63. Aspect.TestValue++;
  64. }
  65. }
  66. public class MyService
  67. {
  68. [MyAspect(TestValue = 41)] //Usage!
  69. public void DoIt()
  70. {
  71. }
  72. }
  73. public class MyClient
  74. {
  75. private readonly MyService _service;
  76. public MyClient(MyService service)
  77. {
  78. _service = service;
  79. }
  80. public void Test()
  81. {
  82. _service.DoIt();
  83. }
  84. }
  85. }
  86. }