Skip to content

Commit

Permalink
Merge pull request #270 from EasyAbp/on-completed
Browse files Browse the repository at this point in the history
Use `IServiceScopeFactory` on UOW completed
  • Loading branch information
gdlcf88 committed Aug 3, 2023
2 parents 9c1a014 + e710f15 commit 3390c40
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand All @@ -28,23 +29,23 @@ public class ProductAppService :
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;

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;
Expand Down Expand Up @@ -87,14 +88,19 @@ public override async Task<ProductDto> CreateAsync(CreateUpdateProductDto input)
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)
Expand Down Expand Up @@ -123,7 +129,7 @@ public override async Task<ProductDto> UpdateAsync(Guid id, CreateUpdateProductD
await LoadDtosExtraDataAsync(items, Clock.Now);
await LoadDtosProductGroupDisplayNameAsync(items);

UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); });
await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId);

return dto;
}
Expand Down Expand Up @@ -351,7 +357,7 @@ public override async Task DeleteAsync(Guid id)

await _productManager.DeleteAsync(product);

UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); });
await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId);
}

protected virtual void CheckProductIsNotStatic(Product product)
Expand Down Expand Up @@ -383,7 +389,7 @@ public async Task<ProductDto> CreateSkuAsync(Guid productId, CreateProductSkuDto
await LoadDtosExtraDataAsync(items, Clock.Now);
await LoadDtosProductGroupDisplayNameAsync(items);

UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); });
await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId);

return dto;
}
Expand All @@ -410,7 +416,7 @@ public virtual async Task<ProductDto> UpdateSkuAsync(Guid productId, Guid produc
await LoadDtosExtraDataAsync(items, Clock.Now);
await LoadDtosProductGroupDisplayNameAsync(items);

UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); });
await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId);

return dto;
}
Expand All @@ -434,7 +440,7 @@ public virtual async Task<ProductDto> DeleteSkuAsync(Guid productId, Guid produc
await LoadDtosExtraDataAsync(items, Clock.Now);
await LoadDtosProductGroupDisplayNameAsync(items);

UnitOfWorkManager.Current.OnCompleted(async () => { await ClearProductViewCacheAsync(product.StoreId); });
await ClearProductViewCacheOnCurrentUowCompletedAsync(product.StoreId);

return dto;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -45,10 +43,10 @@ public virtual async Task HandleEventAsync(OrderCanceledEto eventData)

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);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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;
Expand All @@ -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;
}

Expand All @@ -52,7 +51,7 @@ public virtual async Task HandleEventAsync(FlashSaleOrderCreationResultEto event

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>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,15 +9,19 @@

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;
}
Expand All @@ -27,7 +32,12 @@ public virtual async Task HandleEventAsync(EntityChangedEventData<FlashSalePlan>

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);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -34,21 +34,21 @@ public CreateFlashSaleResultEventHandler(
IObjectMapper objectMapper,
ICurrentTenant currentTenant,
IUnitOfWorkManager unitOfWorkManager,
IServiceScopeFactory serviceScopeFactory,
ILogger<CreateFlashSaleResultEventHandler> logger,
IAbpDistributedLock abpDistributedLock,
IDistributedEventBus distributedEventBus,
IAbpApplication abpApplication,
IFlashSaleInventoryManager flashSaleInventoryManager,
IFlashSaleCurrentResultCache flashSaleCurrentResultCache,
IFlashSaleResultRepository flashSaleResultRepository)
{
ObjectMapper = objectMapper;
CurrentTenant = currentTenant;
UnitOfWorkManager = unitOfWorkManager;
ServiceScopeFactory = serviceScopeFactory;
Logger = logger;
AbpDistributedLock = abpDistributedLock;
DistributedEventBus = distributedEventBus;
AbpApplication = abpApplication;
FlashSaleInventoryManager = flashSaleInventoryManager;
FlashSaleCurrentResultCache = flashSaleCurrentResultCache;
FlashSaleResultRepository = flashSaleResultRepository;
Expand Down Expand Up @@ -83,7 +83,7 @@ await FlashSaleCurrentResultCache.SetAsync(eventData.Plan.Id, eventData.UserId,
// 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>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -28,7 +32,9 @@ public virtual async Task HandleEventAsync(EntityUpdatedEto<ProductEto> eventDat

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

Expand All @@ -38,7 +44,9 @@ public virtual async Task HandleEventAsync(EntityDeletedEto<ProductEto> eventDat

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

0 comments on commit 3390c40

Please sign in to comment.