zoukankan      html  css  js  c++  java
  • ABP拦截器之UnitOfWorkRegistrar(一)

      ABP中UnitOfWorkRegistrar拦截器是整个ABP中非常关键的一个部分,这个部分在整个业务系统中也是用的最多的一个部分,这篇文章的主要思路并不是写如何使用ABP中的UnitOfWork,重点在于分析整个ABP框架中是如何实现工作单元的,如果想了解如何快速使用ABP中的UnitOfWork,请读下面的文章,这个是ABP的官方文档。

      整个过程的分析还是按照之前系列拦截器中的顺序进行说明,首先是从AbpBootstrapper中的构造函数开始说起。

    /// <summary>
            /// Creates a new <see cref="AbpBootstrapper"/> instance.
            /// </summary>
            /// <param name="startupModule">Startup module of the application which depends on other used modules. Should be derived from <see cref="AbpModule"/>.</param>
            /// <param name="optionsAction">An action to set options</param>
            private AbpBootstrapper([NotNull] Type startupModule, [CanBeNull] Action<AbpBootstrapperOptions> optionsAction = null)
            {
                Check.NotNull(startupModule, nameof(startupModule));
    
                var options = new AbpBootstrapperOptions();
                optionsAction?.Invoke(options);
    
                if (!typeof(AbpModule).GetTypeInfo().IsAssignableFrom(startupModule))
                {
                    throw new ArgumentException($"{nameof(startupModule)} should be derived from {nameof(AbpModule)}.");
                }
    
                StartupModule = startupModule;
    
                IocManager = options.IocManager;
                PlugInSources = options.PlugInSources;
    
                _logger = NullLogger.Instance;
    
                if (!options.DisableAllInterceptors)
                {
                    AddInterceptorRegistrars();
                }
            }
    

      如果我们没有在初始化的时候配置DisableAllInterceptors=true的情况下,默认是开启所有的拦截器的,然后在AddInterceptorRegistrars()方法中会执行UnitOfWorkRegistrar.Initialize(IocManager)方法,从而开始了整个UnitOfWorkRegistrar的初始化操作,那么让我们一起进入到UnitOfWorkRegistrar这个静态类中去分析整个过程吧!  

    /// <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);
            }
        }
    

      就像这个类最开始的说明一样,这个类的作用是为了用于需要Unit Of Work机制的类注册拦截器的,在这个类的Initialize方法中,首先会注入整个ABP系统中唯一的IIocManager,然后就是订阅唯一的IocContainer这个容器的ComponentRegistered事件,在订阅事件中首先是获取当前触发此事件的类型信息,接下来就是执行HandleTypesWithUnitOfWorkAttribute这个方法,这个方法中主要是判断当前方法是否定义了UnitOfWork这个自定义属性,如果当前方法定义了这个自定义UnitOfWork属性的话,那么就注册UnitOfWorkInterceptor这个拦截器,后面一个HandleConventionalUnitOfWorkTypes这个方法我们可以看看里面的一些关键过程,这里面核心的是调用了Abp.Domain.Uow命名空间下面的静态类UnitOfWorkDefaultOptionsExtensions中的IsConventionalUowClass方法,在这个方法中又会去调用UnitOfWorkDefaultOptions : IUnitOfWorkDefaultOptions这个类中的 public List<Func<Type, bool>> ConventionalUowSelectors { get; }这个委托List,从而确定当前类型是否需要注入UnitOfWorkInterceptor这个拦截器,执行到这里最关键的就是ConventionalUowSelectors 这个委托的List到底默认添加了哪些类型的委托?我们来看看这个类的初始化过程。  

     public UnitOfWorkDefaultOptions()
            {
                _filters = new List<DataFilterConfiguration>();
                IsTransactional = true;
                Scope = TransactionScopeOption.Required;
    
                IsTransactionScopeAvailable = true;
    
                ConventionalUowSelectors = new List<Func<Type, bool>>
                {
                    type => typeof(IRepository).IsAssignableFrom(type) ||
                            typeof(IApplicationService).IsAssignableFrom(type)
                };
            }
    

      看完了这个你应该明白了,ABP中会默认为继承自IRepository或者是IApplicationService的两种类型添加UnitOfWork特性,看完了这个你肯定在想如果自己也需要添加自定义的规则那该怎么办?这里ABP中强大的Configuration就起大作用了,这里只需要在继承自AbpModule类的PreInitialize方法中加入下面的代码就可以了。  

    Configuration.UnitOfWork.ConventionalUowSelectors.Add(type => ...);
    

      至此所有支持UnitOfWork特性的类型及方法我们就都理解了,至少我们知道ABP中默认为哪些方法或类型添加UnitOfWork特性已经自己如何自定义一些规则使我们的代码能够使用这个特性。

      后面一个部分就是深入理解UnitOfWorkInterceptor这个拦截器了,还是和前面一样,我们来看看这个类中到底写了些什么?

    /// <summary>
        /// This interceptor is used to manage database connection and transactions.
        /// </summary>
        internal class UnitOfWorkInterceptor : IInterceptor
        {
            private readonly IUnitOfWorkManager _unitOfWorkManager;
            private readonly IUnitOfWorkDefaultOptions _unitOfWorkOptions;
    
            public UnitOfWorkInterceptor(IUnitOfWorkManager unitOfWorkManager, IUnitOfWorkDefaultOptions unitOfWorkOptions)
            {
                _unitOfWorkManager = unitOfWorkManager;
                _unitOfWorkOptions = unitOfWorkOptions;
            }
    
            /// <summary>
            /// Intercepts a method.
            /// </summary>
            /// <param name="invocation">Method invocation arguments</param>
            public void Intercept(IInvocation invocation)
            {
                MethodInfo method;
                try
                {
                    method = invocation.MethodInvocationTarget;
                }
                catch
                {
                    method = invocation.GetConcreteMethod();
                }
    
                var unitOfWorkAttr = _unitOfWorkOptions.GetUnitOfWorkAttributeOrNull(method);
                if (unitOfWorkAttr == null || unitOfWorkAttr.IsDisabled)
                {
                    //No need to a uow
                    invocation.Proceed();
                    return;
                }
    
                //No current uow, run a new one
                PerformUow(invocation, unitOfWorkAttr.CreateOptions());
            }
    
            private void PerformUow(IInvocation invocation, UnitOfWorkOptions options)
            {
                if (invocation.Method.IsAsync())
                {
                    PerformAsyncUow(invocation, options);
                }
                else
                {
                    PerformSyncUow(invocation, options);
                }
            }
    
            private void PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options)
            {
                using (var uow = _unitOfWorkManager.Begin(options))
                {
                    invocation.Proceed();
                    uow.Complete();
                }
            }
    
            private void PerformAsyncUow(IInvocation invocation, UnitOfWorkOptions options)
            {
                var uow = _unitOfWorkManager.Begin(options);
    
                try
                {
                    invocation.Proceed();
                }
                catch
                {
                    uow.Dispose();
                    throw;
                }
    
                if (invocation.Method.ReturnType == typeof(Task))
                {
                    invocation.ReturnValue = InternalAsyncHelper.AwaitTaskWithPostActionAndFinally(
                        (Task) invocation.ReturnValue,
                        async () => await uow.CompleteAsync(),
                        exception => uow.Dispose()
                    );
                }
                else //Task<TResult>
                {
                    invocation.ReturnValue = InternalAsyncHelper.CallAwaitTaskWithPostActionAndFinallyAndGetResult(
                        invocation.Method.ReturnType.GenericTypeArguments[0],
                        invocation.ReturnValue,
                        async () => await uow.CompleteAsync(),
                        exception => uow.Dispose()
                    );
                }
            }
        }
    

      首先也是看这个类的注释:This interceptor is used to manage database connection and transactions.顾名思义就是这个拦截器是为了管理数据库的连接和事物操作的,在这个类的构造函数中默认注入了两个关键的类型,IUnitOfWorkManager和IUnitOfWorkDefaultOptions这两个一个用于管理UnitOfWork,另一个用于配置UnitOfWork,在我们的系统中,当我们调用带UnitOfWork特性的方法时,首先就会将该方法挂起,然后执行对应的拦截器的Intercept(IInvocation invocation)这个方法,在这个方法中首先会判断当前执行方法是否定义过自定义的UnitOfWork属性或者是符合特定规则的类型,关于这个类型上面也已经提到过,这里不再赘述仅仅看看其内部实现原理。

    public static UnitOfWorkAttribute GetUnitOfWorkAttributeOrNull(this IUnitOfWorkDefaultOptions unitOfWorkDefaultOptions, MethodInfo methodInfo)
            {
                var attrs = methodInfo.GetCustomAttributes(true).OfType<UnitOfWorkAttribute>().ToArray();
                if (attrs.Length > 0)
                {
                    return attrs[0];
                }
    
                attrs = methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(true).OfType<UnitOfWorkAttribute>().ToArray();
                if (attrs.Length > 0)
                {
                    return attrs[0];
                }
    
                if (unitOfWorkDefaultOptions.IsConventionalUowClass(methodInfo.DeclaringType))
                {
                    return new UnitOfWorkAttribute(); //Default
                }
    
                return null;
            }
    

      通过这个方法我们就能够获取当前方法的自定义的UnitOfWorkAttribute属性,在获取了这个属性后,我们还需要判断该UnitOfWorkAttribute中的IsDisabled是否为true,如果为true的话那么就让该方法执行不再执行后面的一些操作了,所以这里ABP也给我们提供了一种不使用UnitOfWork特性的方法那就是定义UnitOfWorkAttribute的IsDisable属性,具体怎么使用呢?请看下面的示例。

    [UnitOfWork(IsDisabled = true)]
    public virtual void RemoveFriendship(RemoveFriendshipInput input)
    {
        _friendshipRepository.Delete(input.Id);
    }
    

      在执行完这些判断的过程以后就是最关键的PerformUow这个方法了,在这个方法中会根据当前方法是否是异步方法分为两个过程,这里我仅仅使用同步的方法来进行说明这个过程到底是怎么样的?

    private void PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options)
            {
                using (var uow = _unitOfWorkManager.Begin(options))
                {
                    invocation.Proceed();
                    uow.Complete();
                }
            }
    

      这个方法有没有非常熟悉,这个就是我们需要将我们执行的方法包装在一个Begin和一个Complete方法中,从而来完成整个工作单元的过程,在ABP中的官方文档中也提到了这种加入工作单元的方式,这里也可以看看这个示例。

    public class MyService
    {
        private readonly IUnitOfWorkManager _unitOfWorkManager;
        private readonly IPersonRepository _personRepository;
        private readonly IStatisticsRepository _statisticsRepository;
    
        public MyService(IUnitOfWorkManager unitOfWorkManager, IPersonRepository personRepository, IStatisticsRepository statisticsRepository)
        {
            _unitOfWorkManager = unitOfWorkManager;
            _personRepository = personRepository;
            _statisticsRepository = statisticsRepository;
        }
    
        public void CreatePerson(CreatePersonInput input)
        {
            var person = new Person { Name = input.Name, EmailAddress = input.EmailAddress };
    
            using (var unitOfWork = _unitOfWorkManager.Begin())
            {
                _personRepository.Insert(person);
                _statisticsRepository.IncrementPeopleCount();
    
                unitOfWork.Complete();
            }
        }
    }
    

      这个就是采用注入IUnitOfWorkManager的方式来完成工作单元的操作的,但是个人还是建议没有特别的目的还是自定义UnitOfWork属性比较好。在下一篇我会深入到UnitOfWorkManager中去看这个Begin和Complete方法到底做了些什么?需要关注请点击这里

      最后,点击这里返回整个ABP系列的主目录。

  • 相关阅读:
    luogu P1630 求和(枚举暴力)
    luogu P3414 SAC#1
    luogu P1869 愚蠢的组合数(质因数+瞎搞)
    luogu P1586 四方定理(背包)
    luogu P3795 钟氏映射(递推)
    2017.8.15 [Haoi2016]字符合并 区间dp+状压dp
    [NOI2002] 荒岛野人 扩展欧几里得算法
    [Noi2002]Savage 扩展欧几里得
    bzoj 1778: [Usaco2010 Hol]Dotp 驱逐猪猡
    bzoj 3505: [Cqoi2014]数三角形
  • 原文地址:https://www.cnblogs.com/seekdream/p/9898836.html
Copyright © 2011-2022 走看看