Browse Source

Fix UOW OnCompleted usage with `IAbpApplication`

pull/213/head
gdlcf88 4 years ago
parent
commit
41aad42052
  1. 14
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandler.cs
  2. 10
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSaleResults/CreateFlashSaleResultEventHandler.cs

14
plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandler.cs

@ -2,7 +2,9 @@
using EasyAbp.EShop.Plugins.FlashSales.FlashSaleResults; using EasyAbp.EShop.Plugins.FlashSales.FlashSaleResults;
using EasyAbp.EShop.Plugins.FlashSales.FlashSaleResults.Dtos; using EasyAbp.EShop.Plugins.FlashSales.FlashSaleResults.Dtos;
using EasyAbp.Eshop.Products.Products; using EasyAbp.Eshop.Products.Products;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Volo.Abp;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed; using Volo.Abp.EventBus.Distributed;
using Volo.Abp.ObjectMapping; using Volo.Abp.ObjectMapping;
@ -17,6 +19,7 @@ public class FlashSaleOrderCreationResultEventHandler : IDistributedEventHandler
protected IFlashSaleInventoryManager FlashSaleInventoryManager { get; } protected IFlashSaleInventoryManager FlashSaleInventoryManager { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; } protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected IObjectMapper ObjectMapper { get; } protected IObjectMapper ObjectMapper { get; }
protected IAbpApplication AbpApplication { get; }
protected IFlashSaleCurrentResultCache FlashSaleCurrentResultCache { get; } protected IFlashSaleCurrentResultCache FlashSaleCurrentResultCache { get; }
protected IFlashSaleResultRepository FlashSaleResultRepository { get; } protected IFlashSaleResultRepository FlashSaleResultRepository { get; }
@ -25,6 +28,7 @@ public class FlashSaleOrderCreationResultEventHandler : IDistributedEventHandler
IFlashSaleInventoryManager flashSaleInventoryManager, IFlashSaleInventoryManager flashSaleInventoryManager,
IUnitOfWorkManager unitOfWorkManager, IUnitOfWorkManager unitOfWorkManager,
IObjectMapper objectMapper, IObjectMapper objectMapper,
IAbpApplication abpApplication,
IFlashSaleCurrentResultCache flashSaleCurrentResultCache, IFlashSaleCurrentResultCache flashSaleCurrentResultCache,
IFlashSaleResultRepository flashSaleResultRepository) IFlashSaleResultRepository flashSaleResultRepository)
{ {
@ -32,6 +36,7 @@ public class FlashSaleOrderCreationResultEventHandler : IDistributedEventHandler
FlashSaleInventoryManager = flashSaleInventoryManager; FlashSaleInventoryManager = flashSaleInventoryManager;
UnitOfWorkManager = unitOfWorkManager; UnitOfWorkManager = unitOfWorkManager;
ObjectMapper = objectMapper; ObjectMapper = objectMapper;
AbpApplication = abpApplication;
FlashSaleCurrentResultCache = flashSaleCurrentResultCache; FlashSaleCurrentResultCache = flashSaleCurrentResultCache;
FlashSaleResultRepository = flashSaleResultRepository; FlashSaleResultRepository = flashSaleResultRepository;
} }
@ -86,11 +91,16 @@ public class FlashSaleOrderCreationResultEventHandler : IDistributedEventHandler
protected virtual async Task ResetFlashSaleCurrentResultCacheAsync(FlashSaleResult flashSaleResult) protected virtual async Task ResetFlashSaleCurrentResultCacheAsync(FlashSaleResult flashSaleResult)
{ {
await FlashSaleCurrentResultCache.SetAsync(flashSaleResult.PlanId, flashSaleResult.UserId, using var scope = AbpApplication.ServiceProvider.CreateScope();
var objectMapper = scope.ServiceProvider.GetRequiredService<IObjectMapper>();
var flashSaleCurrentResultCache = scope.ServiceProvider.GetRequiredService<IFlashSaleCurrentResultCache>();
await flashSaleCurrentResultCache.SetAsync(flashSaleResult.PlanId, flashSaleResult.UserId,
new FlashSaleCurrentResultCacheItem new FlashSaleCurrentResultCacheItem
{ {
TenantId = flashSaleResult.TenantId, TenantId = flashSaleResult.TenantId,
ResultDto = ObjectMapper.Map<FlashSaleResult, FlashSaleResultDto>(flashSaleResult) ResultDto = objectMapper.Map<FlashSaleResult, FlashSaleResultDto>(flashSaleResult)
}); });
} }
} }

10
plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSaleResults/CreateFlashSaleResultEventHandler.cs

@ -3,6 +3,7 @@ using EasyAbp.EShop.Plugins.FlashSales.FlashSalePlans;
using EasyAbp.EShop.Plugins.FlashSales.FlashSaleResults.Dtos; using EasyAbp.EShop.Plugins.FlashSales.FlashSaleResults.Dtos;
using EasyAbp.Eshop.Products.Products; using EasyAbp.Eshop.Products.Products;
using EasyAbp.EShop.Products.Products; using EasyAbp.EShop.Products.Products;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -25,6 +26,7 @@ public class CreateFlashSaleResultEventHandler : IDistributedEventHandler<Create
protected ILogger<CreateFlashSaleResultEventHandler> Logger { get; } protected ILogger<CreateFlashSaleResultEventHandler> Logger { get; }
protected IAbpDistributedLock AbpDistributedLock { get; } protected IAbpDistributedLock AbpDistributedLock { get; }
protected IDistributedEventBus DistributedEventBus { get; } protected IDistributedEventBus DistributedEventBus { get; }
protected IAbpApplication AbpApplication { get; }
protected IFlashSaleInventoryManager FlashSaleInventoryManager { get; } protected IFlashSaleInventoryManager FlashSaleInventoryManager { get; }
protected IFlashSaleCurrentResultCache FlashSaleCurrentResultCache { get; } protected IFlashSaleCurrentResultCache FlashSaleCurrentResultCache { get; }
protected IFlashSaleResultRepository FlashSaleResultRepository { get; } protected IFlashSaleResultRepository FlashSaleResultRepository { get; }
@ -36,6 +38,7 @@ public class CreateFlashSaleResultEventHandler : IDistributedEventHandler<Create
ILogger<CreateFlashSaleResultEventHandler> logger, ILogger<CreateFlashSaleResultEventHandler> logger,
IAbpDistributedLock abpDistributedLock, IAbpDistributedLock abpDistributedLock,
IDistributedEventBus distributedEventBus, IDistributedEventBus distributedEventBus,
IAbpApplication abpApplication,
IFlashSaleInventoryManager flashSaleInventoryManager, IFlashSaleInventoryManager flashSaleInventoryManager,
IFlashSaleCurrentResultCache flashSaleCurrentResultCache, IFlashSaleCurrentResultCache flashSaleCurrentResultCache,
IFlashSaleResultRepository flashSaleResultRepository) IFlashSaleResultRepository flashSaleResultRepository)
@ -46,6 +49,7 @@ public class CreateFlashSaleResultEventHandler : IDistributedEventHandler<Create
Logger = logger; Logger = logger;
AbpDistributedLock = abpDistributedLock; AbpDistributedLock = abpDistributedLock;
DistributedEventBus = distributedEventBus; DistributedEventBus = distributedEventBus;
AbpApplication = abpApplication;
FlashSaleInventoryManager = flashSaleInventoryManager; FlashSaleInventoryManager = flashSaleInventoryManager;
FlashSaleCurrentResultCache = flashSaleCurrentResultCache; FlashSaleCurrentResultCache = flashSaleCurrentResultCache;
FlashSaleResultRepository = flashSaleResultRepository; FlashSaleResultRepository = flashSaleResultRepository;
@ -80,7 +84,11 @@ public class CreateFlashSaleResultEventHandler : IDistributedEventHandler<Create
// try to roll back the inventory. // try to roll back the inventory.
UnitOfWorkManager.Current.OnCompleted(async () => UnitOfWorkManager.Current.OnCompleted(async () =>
{ {
if (!await FlashSaleInventoryManager.TryRollBackInventoryAsync(eventData.TenantId, using var scope = AbpApplication.ServiceProvider.CreateScope();
var flashSaleInventoryManager = scope.ServiceProvider.GetRequiredService<IFlashSaleInventoryManager>();
if (!await flashSaleInventoryManager.TryRollBackInventoryAsync(eventData.TenantId,
eventData.ProductInventoryProviderName, eventData.Plan.StoreId, eventData.ProductInventoryProviderName, eventData.Plan.StoreId,
eventData.Plan.ProductId, eventData.Plan.ProductSkuId)) eventData.Plan.ProductId, eventData.Plan.ProductSkuId))
{ {

Loading…
Cancel
Save