From 978f1fd10845b6d5710209b66c687943eb24df3a Mon Sep 17 00:00:00 2001 From: gdlcf88 <47396430@qq.com> Date: Sat, 9 May 2020 20:48:08 +0800 Subject: [PATCH] Redesigned PaymentServiceResolver --- .../EShop/Payments/Payments/PaymentAppService.cs | 2 +- .../EShop/Payments/EShopPaymentsDomainModule.cs | 12 +++++------- .../Payments/FreePaymentServiceProvider.cs | 4 +--- .../Payments/Payments/IPaymentServiceResolver.cs | 6 +++--- .../Payments/Payments/PaymentServiceResolver.cs | 16 ++++++++-------- .../src/EasyMall.Domain/EasyMallDomainModule.cs | 2 +- 6 files changed, 19 insertions(+), 23 deletions(-) diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentAppService.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentAppService.cs index ad53a2c9..54ed8555 100644 --- a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentAppService.cs +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Application/EasyAbp/EShop/Payments/Payments/PaymentAppService.cs @@ -32,7 +32,7 @@ namespace EasyAbp.EShop.Payments.Payments { await CheckCreatePolicyAsync(); - var providerType = await _paymentServiceResolver.GetProviderTypeOrDefaultAsync(input.PaymentMethod) ?? + var providerType = _paymentServiceResolver.GetProviderTypeOrDefault(input.PaymentMethod) ?? throw new UnknownPaymentMethodException(input.PaymentMethod); var provider = ServiceProvider.GetService(providerType) as IPaymentServiceProvider ?? diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/EShopPaymentsDomainModule.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/EShopPaymentsDomainModule.cs index 26e74df0..51d1f77f 100644 --- a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/EShopPaymentsDomainModule.cs +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/EShopPaymentsDomainModule.cs @@ -16,16 +16,14 @@ namespace EasyAbp.EShop.Payments )] public class EShopPaymentsDomainModule : AbpModule { - public override void PostConfigureServices(ServiceConfigurationContext context) - { - context.Services.TryAddTransient(); - } - - public override void OnApplicationInitialization(ApplicationInitializationContext context) + public override void OnPostApplicationInitialization(ApplicationInitializationContext context) { var resolver = context.ServiceProvider.GetService(); - resolver.TryRegisterProviderAsync(FreePaymentServiceProvider.PaymentMethod, typeof(FreePaymentServiceProvider)); + if (resolver.GetPaymentMethods().Count == 0) + { + resolver.TryRegisterProvider(FreePaymentServiceProvider.PaymentMethod, typeof(FreePaymentServiceProvider)); + } } public override void ConfigureServices(ServiceConfigurationContext context) diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/FreePaymentServiceProvider.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/FreePaymentServiceProvider.cs index e7dd6aa9..244829b6 100644 --- a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/FreePaymentServiceProvider.cs +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/FreePaymentServiceProvider.cs @@ -7,9 +7,7 @@ using Volo.Abp.Timing; namespace EasyAbp.EShop.Payments.Payments { - // [ExposeServices(typeof(IPaymentRepository))] - // [Dependency(ServiceLifetime.Transient, TryRegister = true)] - public class FreePaymentServiceProvider : IPaymentServiceProvider + public class FreePaymentServiceProvider : IPaymentServiceProvider, ITransientDependency { private readonly IClock _clock; private readonly IPaymentRepository _paymentRepository; diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/IPaymentServiceResolver.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/IPaymentServiceResolver.cs index ee68b31a..aed171d2 100644 --- a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/IPaymentServiceResolver.cs +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/IPaymentServiceResolver.cs @@ -6,10 +6,10 @@ namespace EasyAbp.EShop.Payments.Payments { public interface IPaymentServiceResolver { - Task TryRegisterProviderAsync(string paymentMethod, Type providerType); + bool TryRegisterProvider(string paymentMethod, Type providerType); - Task> GetPaymentMethodsAsync(); + List GetPaymentMethods(); - Task GetProviderTypeOrDefaultAsync(string paymentMethod); + Type GetProviderTypeOrDefault(string paymentMethod); } } \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/PaymentServiceResolver.cs b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/PaymentServiceResolver.cs index 8ead7dd1..0fdd8a89 100644 --- a/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/PaymentServiceResolver.cs +++ b/modules/EasyAbp.EShop.Payments/src/EasyAbp.EShop.Payments.Domain/EasyAbp/EShop/Payments/Payments/PaymentServiceResolver.cs @@ -18,34 +18,34 @@ namespace EasyAbp.EShop.Payments.Payments _serviceProvider = serviceProvider; } - public virtual Task TryRegisterProviderAsync(string paymentMethod, Type providerType) + public virtual bool TryRegisterProvider(string paymentMethod, Type providerType) { if (Providers.ContainsKey(paymentMethod)) { - return Task.FromResult(false); + return false; } using (var scope = _serviceProvider.CreateScope()) { if (scope.ServiceProvider.GetService(providerType) == null) { - return Task.FromResult(false); + return false; } } Providers.Add(paymentMethod, providerType); - return Task.FromResult(true); + return true; } - public virtual Task> GetPaymentMethodsAsync() + public virtual List GetPaymentMethods() { - return Task.FromResult(Providers.Keys.ToList()); + return Providers.Keys.ToList(); } - public virtual Task GetProviderTypeOrDefaultAsync(string paymentMethod) + public virtual Type GetProviderTypeOrDefault(string paymentMethod) { - return Task.FromResult(Providers.GetOrDefault(paymentMethod)); + return Providers.GetOrDefault(paymentMethod); } } } \ No newline at end of file diff --git a/samples/EasyMall/aspnet-core/src/EasyMall.Domain/EasyMallDomainModule.cs b/samples/EasyMall/aspnet-core/src/EasyMall.Domain/EasyMallDomainModule.cs index 3dca66b3..e2ef6c62 100644 --- a/samples/EasyMall/aspnet-core/src/EasyMall.Domain/EasyMallDomainModule.cs +++ b/samples/EasyMall/aspnet-core/src/EasyMall.Domain/EasyMallDomainModule.cs @@ -60,7 +60,7 @@ namespace EasyMall { var resolver = context.ServiceProvider.GetService(); - resolver.TryRegisterProviderAsync(WeChatPayPaymentServiceProvider.PaymentMethod, typeof(WeChatPayPaymentServiceProvider)); + resolver.TryRegisterProvider(WeChatPayPaymentServiceProvider.PaymentMethod, typeof(WeChatPayPaymentServiceProvider)); } } }