From bec0f6bec2674351ac8cd6fdf3fa49f21dddd235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 27 Jul 2017 17:19:09 +0300 Subject: [PATCH] Refactor namings. --- .../AbpRegistrationBuilderExtensions.cs | 4 ++-- ...dArgs.cs => IOnServiceRegistredContext.cs} | 2 +- ...edArgs.cs => OnServiceRegistredContext.cs} | 4 ++-- ...eCollectionRegistrationActionExtensions.cs | 20 +++++++++---------- src/Volo.Abp/Volo/Abp/AbpKernelModule.cs | 17 ++++------------ .../Volo/Abp/Uow/UnitOfWorkAttribute.cs | 15 ++++++++++++++ .../Volo/Abp/Uow/UnitOfWorkInterceptor.cs | 16 ++++++++++++++- .../AutoRegistrationHelper.cs | 6 +++--- .../IServiceExposingArgs.cs | 2 +- .../OnServiceExposingArgs.cs | 4 ++-- .../ServiceExposingActionList.cs | 2 +- .../ServiceRegistrationActionList.cs | 2 +- .../DynamicProxy/AbpInterceptionTestBase.cs | 2 +- 13 files changed, 58 insertions(+), 38 deletions(-) rename src/Volo.Abp/Microsoft/Extensions/DependencyInjection/{IOnServiceRegistredArgs.cs => IOnServiceRegistredContext.cs} (82%) rename src/Volo.Abp/Microsoft/Extensions/DependencyInjection/{OnServiceRegistredArgs.cs => OnServiceRegistredContext.cs} (76%) create mode 100644 src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkAttribute.cs diff --git a/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index be674e82dd..b9a41dda9d 100644 --- a/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -21,9 +21,9 @@ namespace Autofac.Builder return registrationBuilder; } - var serviceRegistredArgs = new OnServiceRegistredArgs(serviceType); + var serviceRegistredArgs = new OnServiceRegistredContext(serviceType); - foreach (var registrationAction in services.GetServiceRegistrationActionList()) + foreach (var registrationAction in services.GetRegistrationActionList()) { registrationAction.Invoke(serviceRegistredArgs); } diff --git a/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/IOnServiceRegistredArgs.cs b/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/IOnServiceRegistredContext.cs similarity index 82% rename from src/Volo.Abp/Microsoft/Extensions/DependencyInjection/IOnServiceRegistredArgs.cs rename to src/Volo.Abp/Microsoft/Extensions/DependencyInjection/IOnServiceRegistredContext.cs index 9ec89ea509..778260e1da 100644 --- a/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/IOnServiceRegistredArgs.cs +++ b/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/IOnServiceRegistredContext.cs @@ -4,7 +4,7 @@ using Volo.Abp.DynamicProxy; namespace Microsoft.Extensions.DependencyInjection { - public interface IOnServiceRegistredArgs + public interface IOnServiceRegistredContext { ITypeList Interceptors { get; } diff --git a/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/OnServiceRegistredArgs.cs b/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/OnServiceRegistredContext.cs similarity index 76% rename from src/Volo.Abp/Microsoft/Extensions/DependencyInjection/OnServiceRegistredArgs.cs rename to src/Volo.Abp/Microsoft/Extensions/DependencyInjection/OnServiceRegistredContext.cs index 39217bb83c..f6f3c97d5a 100644 --- a/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/OnServiceRegistredArgs.cs +++ b/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/OnServiceRegistredContext.cs @@ -6,13 +6,13 @@ using Volo.Abp.DynamicProxy; namespace Microsoft.Extensions.DependencyInjection { - public class OnServiceRegistredArgs : IOnServiceRegistredArgs + public class OnServiceRegistredContext : IOnServiceRegistredContext { public virtual ITypeList Interceptors { get; } public virtual Type ImplementationType { get; } - public OnServiceRegistredArgs([NotNull] Type implementationType) + public OnServiceRegistredContext([NotNull] Type implementationType) { ImplementationType = Check.NotNull(implementationType, nameof(implementationType)); diff --git a/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationActionExtensions.cs b/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationActionExtensions.cs index ace3558005..923b4c6c5f 100644 --- a/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationActionExtensions.cs +++ b/src/Volo.Abp/Microsoft/Extensions/DependencyInjection/ServiceCollectionRegistrationActionExtensions.cs @@ -5,17 +5,17 @@ namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionRegistrationActionExtensions { - public static void OnServiceRegistred(this IServiceCollection services, Action registrationAction) + public static void OnRegistred(this IServiceCollection services, Action registrationAction) { - GetOrCreateServiceActionList(services).Add(registrationAction); + GetOrCreateRegistrationActionList(services).Add(registrationAction); } - public static ServiceRegistrationActionList GetServiceRegistrationActionList(this IServiceCollection services) + public static ServiceRegistrationActionList GetRegistrationActionList(this IServiceCollection services) { - return GetOrCreateServiceActionList(services); + return GetOrCreateRegistrationActionList(services); } - private static ServiceRegistrationActionList GetOrCreateServiceActionList(IServiceCollection services) + private static ServiceRegistrationActionList GetOrCreateRegistrationActionList(IServiceCollection services) { var actionList = services.GetSingletonInstanceOrNull>()?.Value; if (actionList == null) @@ -27,17 +27,17 @@ namespace Microsoft.Extensions.DependencyInjection return actionList; } - public static void OnServiceExposing(this IServiceCollection services, Action exposeAction) + public static void OnExposing(this IServiceCollection services, Action exposeAction) { - GetOrCreateOnServiceExposingList(services).Add(exposeAction); + GetOrCreateExposingList(services).Add(exposeAction); } - public static ServiceExposingActionList GetServiceExposingActionList(this IServiceCollection services) + public static ServiceExposingActionList GetExposingActionList(this IServiceCollection services) { - return GetOrCreateOnServiceExposingList(services); + return GetOrCreateExposingList(services); } - private static ServiceExposingActionList GetOrCreateOnServiceExposingList(IServiceCollection services) + private static ServiceExposingActionList GetOrCreateExposingList(IServiceCollection services) { var actionList = services.GetSingletonInstanceOrNull>()?.Value; if (actionList == null) diff --git a/src/Volo.Abp/Volo/Abp/AbpKernelModule.cs b/src/Volo.Abp/Volo/Abp/AbpKernelModule.cs index b28e07ec87..39c39416c8 100644 --- a/src/Volo.Abp/Volo/Abp/AbpKernelModule.cs +++ b/src/Volo.Abp/Volo/Abp/AbpKernelModule.cs @@ -1,7 +1,5 @@ using System; -using System.Reflection; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.Application.Services; using Volo.Abp.Modularity; using Volo.Abp.ObjectMapping; using Volo.Abp.Reflection; @@ -14,17 +12,10 @@ namespace Volo.Abp { public override void PreConfigureServices(IServiceCollection services) { - //TODO: Move these to dedicated class(es) - - services.OnServiceRegistred(registration => - { - if (typeof(IApplicationService).GetTypeInfo().IsAssignableFrom(registration.ImplementationType)) - { - registration.Interceptors.Add(); - } - }); - - services.OnServiceExposing(context => + services.OnRegistred(UnitOfWorkInterceptorRegistrar.RegisterIfNeeded); + + //TODO: Move to a dedicated class + services.OnExposing(context => { context.ExposedTypes.AddRange( ReflectionHelper.GetImplementedGenericTypes( diff --git a/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkAttribute.cs b/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkAttribute.cs new file mode 100644 index 0000000000..1425d7c263 --- /dev/null +++ b/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkAttribute.cs @@ -0,0 +1,15 @@ +using System; + +namespace Volo.Abp.Uow +{ + /// + /// Used to indicate that declaring method (or all methods of the class) is atomic and should be considered as a unit of work (UOW). + /// + /// + /// This attribute has no effect if there is already a unit of work before calling this method. It uses the ambient UOW in this case. + /// + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Interface)] + public class UnitOfWorkAttribute : Attribute + { + } +} diff --git a/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs b/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs index b486d84e09..5270243ad4 100644 --- a/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs +++ b/src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkInterceptor.cs @@ -1,9 +1,23 @@ -using System.Threading.Tasks; +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Application.Services; using Volo.Abp.DynamicProxy; using Volo.DependencyInjection; namespace Volo.Abp.Uow { + public static class UnitOfWorkInterceptorRegistrar + { + public static void RegisterIfNeeded(IOnServiceRegistredContext context) + { + if (typeof(IApplicationService).GetTypeInfo().IsAssignableFrom(context.ImplementationType)) + { + context.Interceptors.Add(); + } + } + } + public class UnitOfWorkInterceptor : AbpInterceptor, ITransientDependency { private readonly IUnitOfWorkManager _unitOfWorkManager; diff --git a/src/Volo.Abp/Volo/DependencyInjection/AutoRegistrationHelper.cs b/src/Volo.Abp/Volo/DependencyInjection/AutoRegistrationHelper.cs index f3762c8147..01228981e9 100644 --- a/src/Volo.Abp/Volo/DependencyInjection/AutoRegistrationHelper.cs +++ b/src/Volo.Abp/Volo/DependencyInjection/AutoRegistrationHelper.cs @@ -48,11 +48,11 @@ namespace Volo.DependencyInjection } } - var exposeActions = services.GetServiceExposingActionList(); + var exposeActions = services.GetExposingActionList(); if (exposeActions.Any()) { - var args = new OnServiceExposingArgs(type, serviceTypes); - foreach (var action in services.GetServiceExposingActionList()) + var args = new OnServiceExposingContext(type, serviceTypes); + foreach (var action in services.GetExposingActionList()) { action(args); } diff --git a/src/Volo.Abp/Volo/DependencyInjection/IServiceExposingArgs.cs b/src/Volo.Abp/Volo/DependencyInjection/IServiceExposingArgs.cs index 224f6ef0c5..82db03a8e6 100644 --- a/src/Volo.Abp/Volo/DependencyInjection/IServiceExposingArgs.cs +++ b/src/Volo.Abp/Volo/DependencyInjection/IServiceExposingArgs.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; namespace Volo.DependencyInjection { - public interface IOnServiceExposingArgs + public interface IOnServiceExposingContext { Type ImplementationType { get; } diff --git a/src/Volo.Abp/Volo/DependencyInjection/OnServiceExposingArgs.cs b/src/Volo.Abp/Volo/DependencyInjection/OnServiceExposingArgs.cs index c422576833..df4badd262 100644 --- a/src/Volo.Abp/Volo/DependencyInjection/OnServiceExposingArgs.cs +++ b/src/Volo.Abp/Volo/DependencyInjection/OnServiceExposingArgs.cs @@ -4,13 +4,13 @@ using JetBrains.Annotations; namespace Volo.DependencyInjection { - public class OnServiceExposingArgs : IOnServiceExposingArgs + public class OnServiceExposingContext : IOnServiceExposingContext { public Type ImplementationType { get; } public List ExposedTypes { get; } - public OnServiceExposingArgs([NotNull] Type implementationType, List exposedTypes) + public OnServiceExposingContext([NotNull] Type implementationType, List exposedTypes) { ImplementationType = Check.NotNull(implementationType, nameof(implementationType)); ExposedTypes = Check.NotNull(exposedTypes, nameof(exposedTypes)); diff --git a/src/Volo.Abp/Volo/DependencyInjection/ServiceExposingActionList.cs b/src/Volo.Abp/Volo/DependencyInjection/ServiceExposingActionList.cs index 97bc0622a7..dd9289ee25 100644 --- a/src/Volo.Abp/Volo/DependencyInjection/ServiceExposingActionList.cs +++ b/src/Volo.Abp/Volo/DependencyInjection/ServiceExposingActionList.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; namespace Volo.DependencyInjection { - public class ServiceExposingActionList : List> + public class ServiceExposingActionList : List> { } diff --git a/src/Volo.Abp/Volo/DependencyInjection/ServiceRegistrationActionList.cs b/src/Volo.Abp/Volo/DependencyInjection/ServiceRegistrationActionList.cs index beac0e8069..e98e0fd69e 100644 --- a/src/Volo.Abp/Volo/DependencyInjection/ServiceRegistrationActionList.cs +++ b/src/Volo.Abp/Volo/DependencyInjection/ServiceRegistrationActionList.cs @@ -4,7 +4,7 @@ using Microsoft.Extensions.DependencyInjection; namespace Volo.DependencyInjection { - public class ServiceRegistrationActionList : List> + public class ServiceRegistrationActionList : List> { } diff --git a/test/Volo.Abp.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs b/test/Volo.Abp.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs index 9d94a9e27a..c9c23338f9 100644 --- a/test/Volo.Abp.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs +++ b/test/Volo.Abp.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs @@ -20,7 +20,7 @@ namespace Volo.Abp.DynamicProxy services.AddTransient(); services.AddTransient(); - services.OnServiceRegistred(registration => + services.OnRegistred(registration => { if (typeof(SimpleInterceptionTargetClass) == registration.ImplementationType) {