Browse Source

Use `IServiceScopeFactory` on UOW completed

pull/270/head
gdlcf88 3 years ago
parent
commit
e710f15271
  1. 28
      modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductAppService.cs
  2. 20
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCanceledEventHandler.cs
  3. 9
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandler.cs
  4. 16
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanCacheInvalidator.cs
  5. 8
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSaleResults/CreateFlashSaleResultEventHandler.cs
  6. 16
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/ProductCacheInvalidator.cs

28
modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductAppService.cs

@ -8,6 +8,7 @@ using EasyAbp.EShop.Products.Options;
using EasyAbp.EShop.Products.ProductInventories;
using EasyAbp.EShop.Products.Products.CacheItems;
using EasyAbp.EShop.Stores.Stores;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Caching;
@ -28,8 +29,8 @@ namespace EasyAbp.EShop.Products.Products
protected override string CrossStorePolicyName { get; set; } = ProductsPermissions.Products.CrossStore;
private readonly IProductManager _productManager;
private readonly IDistributedCache<ProductViewCacheItem> _cache;
private readonly EShopProductsOptions _options;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly IProductInventoryProviderResolver _productInventoryProviderResolver;
private readonly IProductViewCacheKeyProvider _productViewCacheKeyProvider;
private readonly IProductRepository _repository;
@ -37,14 +38,14 @@ namespace EasyAbp.EShop.Products.Products
public ProductAppService(
IProductManager productManager,
IOptions<EShopProductsOptions> options,
IDistributedCache<ProductViewCacheItem> cache,
IServiceScopeFactory serviceScopeFactory,
IProductInventoryProviderResolver productInventoryProviderResolver,
IProductViewCacheKeyProvider productViewCacheKeyProvider,
IProductRepository repository) : base(repository)
{
_productManager = productManager;
_cache = cache;
_options = options.Value;
_serviceScopeFactory = serviceScopeFactory;
_productInventoryProviderResolver = productInventoryProviderResolver;
_productViewCacheKeyProvider = productViewCacheKeyProvider;
_repository = repository;
@ -87,14 +88,19 @@ namespace EasyAbp.EShop.Products.Products
await LoadDtosExtraDataAsync(items, Clock.Now);
await LoadDtosProductGroupDisplayNameAsync(items);
UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); });
await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId);
return dto;
}
protected virtual async Task ClearProductViewCacheAsync(Guid storeId)
protected virtual async Task ClearProductViewCacheOnCurrentUowCompletedAsync(Guid storeId)
{
await _cache.RemoveAsync(await _productViewCacheKeyProvider.GetCacheKeyAsync(storeId));
UnitOfWorkManager.Current.OnCompleted(async () =>
{
using var scope = _serviceScopeFactory.CreateScope();
var cache = scope.ServiceProvider.GetRequiredService<IDistributedCache<ProductViewCacheItem>>();
await cache.RemoveAsync(await _productViewCacheKeyProvider.GetCacheKeyAsync(storeId));
});
}
public override async Task<ProductDto> UpdateAsync(Guid id, CreateUpdateProductDto input)
@ -123,7 +129,7 @@ namespace EasyAbp.EShop.Products.Products
await LoadDtosExtraDataAsync(items, Clock.Now);
await LoadDtosProductGroupDisplayNameAsync(items);
UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); });
await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId);
return dto;
}
@ -351,7 +357,7 @@ namespace EasyAbp.EShop.Products.Products
await _productManager.DeleteAsync(product);
UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); });
await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId);
}
protected virtual void CheckProductIsNotStatic(Product product)
@ -383,7 +389,7 @@ namespace EasyAbp.EShop.Products.Products
await LoadDtosExtraDataAsync(items, Clock.Now);
await LoadDtosProductGroupDisplayNameAsync(items);
UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); });
await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId);
return dto;
}
@ -410,7 +416,7 @@ namespace EasyAbp.EShop.Products.Products
await LoadDtosExtraDataAsync(items, Clock.Now);
await LoadDtosProductGroupDisplayNameAsync(items);
UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); });
await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId);
return dto;
}
@ -434,7 +440,7 @@ namespace EasyAbp.EShop.Products.Products
await LoadDtosExtraDataAsync(items, Clock.Now);
await LoadDtosProductGroupDisplayNameAsync(items);
UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); });
await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId);
return dto;
}

20
plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCanceledEventHandler.cs

@ -2,8 +2,6 @@
using EasyAbp.EShop.Orders.Orders;
using EasyAbp.EShop.Plugins.FlashSales.FlashSaleResults;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Volo.Abp;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;
@ -13,27 +11,27 @@ namespace EasyAbp.EShop.Plugins.FlashSales.FlashSalePlans;
public class FlashSaleOrderCanceledEventHandler : IDistributedEventHandler<OrderCanceledEto>, ITransientDependency
{
protected IServiceScopeFactory ServiceScopeFactory { get; }
protected IFlashSaleResultRepository FlashSaleResultRepository { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected IAbpApplication AbpApplication { get; }
public FlashSaleOrderCanceledEventHandler(
IServiceScopeFactory serviceScopeFactory,
IFlashSaleResultRepository flashSaleResultRepository,
IUnitOfWorkManager unitOfWorkManager,
IAbpApplication abpApplication)
IUnitOfWorkManager unitOfWorkManager)
{
ServiceScopeFactory = serviceScopeFactory;
FlashSaleResultRepository = flashSaleResultRepository;
UnitOfWorkManager = unitOfWorkManager;
AbpApplication = abpApplication;
}
[UnitOfWork(true)]
public virtual async Task HandleEventAsync(OrderCanceledEto eventData)
{
var flashSaleResult = await FlashSaleResultRepository
.SingleOrDefaultAsync(x => x.Status != FlashSaleResultStatus.Failed && x.StoreId == eventData.Order.StoreId && x.OrderId == eventData.Order.Id);
.SingleOrDefaultAsync(x =>
x.Status != FlashSaleResultStatus.Failed && x.StoreId == eventData.Order.StoreId &&
x.OrderId == eventData.Order.Id);
if (flashSaleResult == null)
{
return;
@ -45,10 +43,10 @@ public class FlashSaleOrderCanceledEventHandler : IDistributedEventHandler<Order
UnitOfWorkManager.Current.OnCompleted(async () =>
{
using var scope = AbpApplication.ServiceProvider.CreateScope();
using var scope = ServiceScopeFactory.CreateScope();
var flashSaleCurrentResultCache = scope.ServiceProvider.GetRequiredService<IFlashSaleCurrentResultCache>();
// remove the cache so the user can try to order again.
await flashSaleCurrentResultCache.RemoveAsync(flashSaleResult.PlanId, flashSaleResult.UserId);
});
}
}
}

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

@ -4,7 +4,6 @@ using EasyAbp.EShop.Plugins.FlashSales.FlashSaleResults.Dtos;
using EasyAbp.EShop.Products.Products;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Volo.Abp;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.ObjectMapping;
@ -17,18 +16,18 @@ public class FlashSaleOrderCreationResultEventHandler : IDistributedEventHandler
{
protected ILogger<FlashSaleOrderCreationResultEventHandler> Logger { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected IAbpApplication AbpApplication { get; }
protected IServiceScopeFactory ServiceScopeFactory { get; }
protected IFlashSaleResultRepository FlashSaleResultRepository { get; }
public FlashSaleOrderCreationResultEventHandler(
ILogger<FlashSaleOrderCreationResultEventHandler> logger,
IUnitOfWorkManager unitOfWorkManager,
IAbpApplication abpApplication,
IServiceScopeFactory serviceScopeFactory,
IFlashSaleResultRepository flashSaleResultRepository)
{
Logger = logger;
UnitOfWorkManager = unitOfWorkManager;
AbpApplication = abpApplication;
ServiceScopeFactory = serviceScopeFactory;
FlashSaleResultRepository = flashSaleResultRepository;
}
@ -52,7 +51,7 @@ public class FlashSaleOrderCreationResultEventHandler : IDistributedEventHandler
UnitOfWorkManager.Current.OnCompleted(async () =>
{
using var scope = AbpApplication.ServiceProvider.CreateScope();
using var scope = ServiceScopeFactory.CreateScope();
var flashSaleInventoryManager = scope.ServiceProvider.GetRequiredService<IFlashSaleInventoryManager>();
var flashSaleCurrentResultCache = scope.ServiceProvider.GetRequiredService<IFlashSaleCurrentResultCache>();

16
plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanCacheInvalidator.cs

@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
@ -8,15 +9,19 @@ using Volo.Abp.Uow;
namespace EasyAbp.EShop.Plugins.FlashSales.FlashSalePlans;
public class FlashSalePlanCacheInvalidator : ILocalEventHandler<EntityChangedEventData<FlashSalePlan>>, ITransientDependency
public class FlashSalePlanCacheInvalidator : ILocalEventHandler<EntityChangedEventData<FlashSalePlan>>,
ITransientDependency
{
protected IServiceScopeFactory ServiceScopeFactory { get; }
protected IDistributedCache<FlashSalePlanCacheItem, Guid> DistributedCache { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
public FlashSalePlanCacheInvalidator(
IServiceScopeFactory serviceScopeFactory,
IDistributedCache<FlashSalePlanCacheItem, Guid> distributedCache,
IUnitOfWorkManager unitOfWorkManager)
{
ServiceScopeFactory = serviceScopeFactory;
DistributedCache = distributedCache;
UnitOfWorkManager = unitOfWorkManager;
}
@ -27,7 +32,12 @@ public class FlashSalePlanCacheInvalidator : ILocalEventHandler<EntityChangedEve
UnitOfWorkManager.Current?.OnCompleted(async () =>
{
await DistributedCache.RemoveAsync(eventData.Entity.Id);
using var scope = ServiceScopeFactory.CreateScope();
var distributedCache =
scope.ServiceProvider.GetRequiredService<IDistributedCache<FlashSalePlanCacheItem, Guid>>();
await distributedCache.RemoveAsync(eventData.Entity.Id);
});
}
}
}

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

@ -22,10 +22,10 @@ public class CreateFlashSaleResultEventHandler : IDistributedEventHandler<Create
protected IObjectMapper ObjectMapper { get; }
protected ICurrentTenant CurrentTenant { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected IServiceScopeFactory ServiceScopeFactory { get; }
protected ILogger<CreateFlashSaleResultEventHandler> Logger { get; }
protected IAbpDistributedLock AbpDistributedLock { get; }
protected IDistributedEventBus DistributedEventBus { get; }
protected IAbpApplication AbpApplication { get; }
protected IFlashSaleInventoryManager FlashSaleInventoryManager { get; }
protected IFlashSaleCurrentResultCache FlashSaleCurrentResultCache { get; }
protected IFlashSaleResultRepository FlashSaleResultRepository { get; }
@ -34,10 +34,10 @@ public class CreateFlashSaleResultEventHandler : IDistributedEventHandler<Create
IObjectMapper objectMapper,
ICurrentTenant currentTenant,
IUnitOfWorkManager unitOfWorkManager,
IServiceScopeFactory serviceScopeFactory,
ILogger<CreateFlashSaleResultEventHandler> logger,
IAbpDistributedLock abpDistributedLock,
IDistributedEventBus distributedEventBus,
IAbpApplication abpApplication,
IFlashSaleInventoryManager flashSaleInventoryManager,
IFlashSaleCurrentResultCache flashSaleCurrentResultCache,
IFlashSaleResultRepository flashSaleResultRepository)
@ -45,10 +45,10 @@ public class CreateFlashSaleResultEventHandler : IDistributedEventHandler<Create
ObjectMapper = objectMapper;
CurrentTenant = currentTenant;
UnitOfWorkManager = unitOfWorkManager;
ServiceScopeFactory = serviceScopeFactory;
Logger = logger;
AbpDistributedLock = abpDistributedLock;
DistributedEventBus = distributedEventBus;
AbpApplication = abpApplication;
FlashSaleInventoryManager = flashSaleInventoryManager;
FlashSaleCurrentResultCache = flashSaleCurrentResultCache;
FlashSaleResultRepository = flashSaleResultRepository;
@ -83,7 +83,7 @@ public class CreateFlashSaleResultEventHandler : IDistributedEventHandler<Create
// try to roll back the inventory.
UnitOfWorkManager.Current.OnCompleted(async () =>
{
using var scope = AbpApplication.ServiceProvider.CreateScope();
using var scope = ServiceScopeFactory.CreateScope();
var flashSaleInventoryManager = scope.ServiceProvider.GetRequiredService<IFlashSaleInventoryManager>();

16
plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/ProductCacheInvalidator.cs

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.EventBus.Distributed;
@ -13,13 +14,16 @@ public class ProductCacheInvalidator :
{
protected IProductCache ProductCache { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected IServiceScopeFactory ServiceScopeFactory { get; }
public ProductCacheInvalidator(
IProductCache productCache,
IUnitOfWorkManager unitOfWorkManager)
IUnitOfWorkManager unitOfWorkManager,
IServiceScopeFactory serviceScopeFactory)
{
ProductCache = productCache;
UnitOfWorkManager = unitOfWorkManager;
ServiceScopeFactory = serviceScopeFactory;
}
public virtual async Task HandleEventAsync(EntityUpdatedEto<ProductEto> eventData)
@ -28,7 +32,9 @@ public class ProductCacheInvalidator :
UnitOfWorkManager.Current?.OnCompleted(async () =>
{
await ProductCache.RemoveAsync(eventData.Entity.Id);
using var scope = ServiceScopeFactory.CreateScope();
var productCache = scope.ServiceProvider.GetRequiredService<IProductCache>();
await productCache.RemoveAsync(eventData.Entity.Id);
});
}
@ -38,7 +44,9 @@ public class ProductCacheInvalidator :
UnitOfWorkManager.Current?.OnCompleted(async () =>
{
await ProductCache.RemoveAsync(eventData.Entity.Id);
using var scope = ServiceScopeFactory.CreateScope();
var productCache = scope.ServiceProvider.GetRequiredService<IProductCache>();
await productCache.RemoveAsync(eventData.Entity.Id);
});
}
}
}
Loading…
Cancel
Save