| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System.Linq;
- using System.Reflection;
- using Abp.Dependency;
- using Castle.Core;
- using Castle.MicroKernel;
- namespace Abp.Domain.Uow
- {
- /// <summary>
- /// This class is used to register interceptor for needed classes for Unit Of Work mechanism.
- /// </summary>
- internal static class UnitOfWorkRegistrar
- {
- /// <summary>
- /// Initializes the registerer.
- /// </summary>
- /// <param name="iocManager">IOC manager</param>
- public static void Initialize(IIocManager iocManager)
- {
- iocManager.IocContainer.Kernel.ComponentRegistered += (key, handler) =>
- {
- var implementationType = handler.ComponentModel.Implementation.GetTypeInfo();
- HandleTypesWithUnitOfWorkAttribute(implementationType, handler);
- HandleConventionalUnitOfWorkTypes(iocManager, implementationType, handler);
- };
- }
- private static void HandleTypesWithUnitOfWorkAttribute(TypeInfo implementationType, IHandler handler)
- {
- if (IsUnitOfWorkType(implementationType) || AnyMethodHasUnitOfWork(implementationType))
- {
- handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(UnitOfWorkInterceptor)));
- }
- }
- private static void HandleConventionalUnitOfWorkTypes(IIocManager iocManager, TypeInfo implementationType, IHandler handler)
- {
- if (!iocManager.IsRegistered<IUnitOfWorkDefaultOptions>())
- {
- return;
- }
- var uowOptions = iocManager.Resolve<IUnitOfWorkDefaultOptions>();
- if (uowOptions.IsConventionalUowClass(implementationType.AsType()))
- {
- handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(UnitOfWorkInterceptor)));
- }
- }
- private static bool IsUnitOfWorkType(TypeInfo implementationType)
- {
- return UnitOfWorkHelper.HasUnitOfWorkAttribute(implementationType);
- }
- private static bool AnyMethodHasUnitOfWork(TypeInfo implementationType)
- {
- return implementationType
- .GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
- .Any(UnitOfWorkHelper.HasUnitOfWorkAttribute);
- }
- }
- }
|