improvements, fixes, etc...
This commit is contained in:
parent
32c37cf22c
commit
08f91408c3
|
|
@ -1,6 +1,17 @@
|
||||||
namespace Mango.Nop.Core.Interfaces;
|
using Mango.Nop.Core.Loggers;
|
||||||
|
using Nop.Data;
|
||||||
|
using System.Transactions;
|
||||||
|
|
||||||
|
namespace Mango.Nop.Core.Interfaces;
|
||||||
|
|
||||||
public interface IMgDbContextBase //: IAcDbContextBase
|
public interface IMgDbContextBase //: IAcDbContextBase
|
||||||
{
|
{
|
||||||
|
ILogger Logger { get; init; }
|
||||||
|
INopDataProvider DataProvider { get; init; }
|
||||||
|
|
||||||
|
bool Transaction(Func<TransactionScope, bool> callbackTransactionBody, bool throwException = false);
|
||||||
|
bool TransactionSafe(Func<TransactionScope, bool> callbackTransactionBody, bool throwException = false);
|
||||||
|
|
||||||
|
Task<bool> TransactionAsync(Func<TransactionScope, Task<bool>> callbackTransactionBody, bool throwException = false);
|
||||||
|
Task<bool> TransactionSafeAsync(Func<TransactionScope, Task<bool>> callbackTransactionBody, bool throwException = false);
|
||||||
}
|
}
|
||||||
|
|
@ -37,6 +37,9 @@
|
||||||
<Reference Include="AyCode.Interfaces.Server">
|
<Reference Include="AyCode.Interfaces.Server">
|
||||||
<HintPath>..\..\..\..\..\..\Aycode\Source\AyCode.Core\AyCode.Services.Server\bin\FruitBank\Debug\net9.0\AyCode.Interfaces.Server.dll</HintPath>
|
<HintPath>..\..\..\..\..\..\Aycode\Source\AyCode.Core\AyCode.Services.Server\bin\FruitBank\Debug\net9.0\AyCode.Interfaces.Server.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Utils">
|
||||||
|
<HintPath>..\..\..\..\..\..\Aycode\Source\AyCode.Core\AyCode.Services.Server\bin\FruitBank\Debug\net9.0\AyCode.Utils.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using AyCode.Interfaces;
|
using AyCode.Interfaces;
|
||||||
|
using AyCode.Utils.Extensions;
|
||||||
using Mango.Nop.Core.Dtos;
|
using Mango.Nop.Core.Dtos;
|
||||||
using Nop.Core.Domain.Customers;
|
using Nop.Core.Domain.Customers;
|
||||||
|
|
||||||
|
|
@ -6,7 +7,7 @@ namespace Mango.Nop.Core.Models;
|
||||||
|
|
||||||
public class MgLoginModelResponse : IAcModelDtoBaseEmpty
|
public class MgLoginModelResponse : IAcModelDtoBaseEmpty
|
||||||
{
|
{
|
||||||
public CustomerDto CustomerDto { get; set; }
|
public CustomerDto? CustomerDto { get; set; }
|
||||||
public string ErrorMessage { get; set; }
|
public string ErrorMessage { get; set; }
|
||||||
|
|
||||||
public MgLoginModelResponse()
|
public MgLoginModelResponse()
|
||||||
|
|
@ -18,4 +19,6 @@ public class MgLoginModelResponse : IAcModelDtoBaseEmpty
|
||||||
CustomerDto = customerDto;
|
CustomerDto = customerDto;
|
||||||
ErrorMessage = errorMessage;
|
ErrorMessage = errorMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsSuccesLogin => CustomerDto != null && ErrorMessage.IsNullOrWhiteSpace();
|
||||||
}
|
}
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
using AyCode.Core.Loggers;
|
using System.Transactions;
|
||||||
|
using AyCode.Core.Consts;
|
||||||
|
using AyCode.Core.Helpers;
|
||||||
|
using AyCode.Core.Loggers;
|
||||||
|
using AyCode.Utils.Extensions;
|
||||||
using Mango.Nop.Core.Interfaces;
|
using Mango.Nop.Core.Interfaces;
|
||||||
using Mango.Nop.Core.Loggers;
|
using Mango.Nop.Core.Loggers;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Mango.Nop.Services;
|
||||||
using Nop.Core.Caching;
|
using Nop.Core.Caching;
|
||||||
using Nop.Core.Domain.Catalog;
|
|
||||||
using Nop.Core.Events;
|
|
||||||
using Nop.Data;
|
using Nop.Data;
|
||||||
using Nop.Services.Events;
|
|
||||||
|
|
||||||
namespace Mango.Nop.Core.Repositories;
|
namespace Mango.Nop.Core.Repositories;
|
||||||
|
|
||||||
|
|
@ -17,14 +18,111 @@ public class MgDbContextBase : IMgDbContextBase
|
||||||
private readonly CacheKey _auctionAllKey = new("Nop.auction.all-{0}", AUCTION_PATTERN_KEY);
|
private readonly CacheKey _auctionAllKey = new("Nop.auction.all-{0}", AUCTION_PATTERN_KEY);
|
||||||
public const string AUCTION_PATTERN_KEY = "Nop.auction.";
|
public const string AUCTION_PATTERN_KEY = "Nop.auction.";
|
||||||
|
|
||||||
|
protected readonly IMgLockService LockService;
|
||||||
|
|
||||||
protected ILogger Logger;
|
public ILogger Logger { get; init; }
|
||||||
protected INopDataProvider DataProvider;
|
public INopDataProvider DataProvider { get; init; }
|
||||||
//protected IHttpContextAccessor HttpContextAccessor;
|
|
||||||
|
|
||||||
public MgDbContextBase(INopDataProvider dataProvider, IEnumerable<IAcLogWriterBase> logWriters)
|
//public IHttpContextAccessor HttpContextAccessor { get; init; }
|
||||||
|
|
||||||
|
public MgDbContextBase(INopDataProvider dataProvider, IMgLockService lockService, IEnumerable<IAcLogWriterBase> logWriters)
|
||||||
{
|
{
|
||||||
|
LockService = lockService;
|
||||||
|
|
||||||
Logger = new Logger<MgDbContextBase>(logWriters.ToArray());
|
Logger = new Logger<MgDbContextBase>(logWriters.ToArray());
|
||||||
DataProvider = dataProvider;
|
DataProvider = dataProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool Transaction(Func<TransactionScope, bool> callbackTransactionBody, bool throwException = false)
|
||||||
|
=> TransactionInner(callbackTransactionBody, throwException);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Using LoskService, global lock!
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="callbackTransactionBody"></param>
|
||||||
|
/// <param name="throwException"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public bool TransactionSafe(Func<TransactionScope, bool> callbackTransactionBody, bool throwException = false)
|
||||||
|
{
|
||||||
|
using (LockService.SemaphoreSlim.UseWait())
|
||||||
|
{
|
||||||
|
return TransactionInner(callbackTransactionBody, throwException);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<bool> TransactionAsync(Func<TransactionScope, Task<bool>> callbackTransactionBody, bool throwException = false)
|
||||||
|
=> TaskHelper.ToThreadPoolTask(() => TransactionInnerAsync(callbackTransactionBody, throwException));
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Using LoskService, global lock!
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="callbackTransactionBody"></param>
|
||||||
|
/// <param name="throwException"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Task<bool> TransactionSafeAsync(Func<TransactionScope, Task<bool>> callbackTransactionBody, bool throwException = false)
|
||||||
|
{
|
||||||
|
return TaskHelper.ToThreadPoolTask(async () =>
|
||||||
|
{
|
||||||
|
using (await LockService.SemaphoreSlim.UseWaitAsync())
|
||||||
|
{
|
||||||
|
return await TransactionInnerAsync(callbackTransactionBody, throwException);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<bool> TransactionInnerAsync(Func<TransactionScope, Task<bool>> callbackTransactionBody, bool throwException = false)
|
||||||
|
{
|
||||||
|
bool result;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var transaction = new TransactionScope( /*TransactionScopeOption.RequiresNew, */TransactionScopeAsyncFlowOption.Enabled))
|
||||||
|
{
|
||||||
|
result = await callbackTransactionBody(transaction);
|
||||||
|
|
||||||
|
if (result) transaction.Complete();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!result) Logger.Warning($"TransactionInnerAsync({this}) transaction ROLLBACK!");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
if (throwException) throw;
|
||||||
|
|
||||||
|
result = false;
|
||||||
|
Logger.Error($"TransactionInnerAsync({this}) transaction error! ex: {ex.Message}{AcEnv.NL}", ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool TransactionInner(Func<TransactionScope, bool> callbackTransactionBody, bool throwException = false)
|
||||||
|
{
|
||||||
|
bool result;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var transaction = new TransactionScope( /*TransactionScopeOption.RequiresNew, */TransactionScopeAsyncFlowOption.Enabled);
|
||||||
|
|
||||||
|
result = callbackTransactionBody(transaction);
|
||||||
|
|
||||||
|
if (result) transaction.Complete();
|
||||||
|
else Logger.Warning($"TransactionInner({this}) transaction ROLLBACK!");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
if (throwException) throw;
|
||||||
|
|
||||||
|
result = false;
|
||||||
|
Logger.Error($"TransactionInnerAsync({this}) transaction error! ex: {ex.Message}{AcEnv.NL}", ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return GetType().Name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue