EntityHistoryInterceptorRegistrar.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using Abp.Dependency;
  5. using Castle.Core;
  6. namespace Abp.EntityHistory
  7. {
  8. internal static class EntityHistoryInterceptorRegistrar
  9. {
  10. public static void Initialize(IIocManager iocManager)
  11. {
  12. iocManager.IocContainer.Kernel.ComponentRegistered += (key, handler) =>
  13. {
  14. if (!iocManager.IsRegistered<IEntityHistoryConfiguration>())
  15. {
  16. return;
  17. }
  18. var entityHistoryConfiguration = iocManager.Resolve<IEntityHistoryConfiguration>();
  19. if (ShouldIntercept(entityHistoryConfiguration, handler.ComponentModel.Implementation))
  20. {
  21. handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(EntityHistoryInterceptor)));
  22. }
  23. };
  24. }
  25. private static bool ShouldIntercept(IEntityHistoryConfiguration entityHistoryConfiguration, Type type)
  26. {
  27. if (type.GetTypeInfo().IsDefined(typeof(UseCaseAttribute), true))
  28. {
  29. return true;
  30. }
  31. if (type.GetMethods().Any(m => m.IsDefined(typeof(UseCaseAttribute), true)))
  32. {
  33. return true;
  34. }
  35. return false;
  36. }
  37. }
  38. }