128 lines
4.2 KiB
C#
128 lines
4.2 KiB
C#
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.Loggers;
|
|
using Mango.Nop.Services;
|
|
using Nop.Core.Caching;
|
|
using Nop.Data;
|
|
|
|
namespace Mango.Nop.Core.Repositories;
|
|
|
|
|
|
public class MgDbContextBase : IMgDbContextBase
|
|
{
|
|
//TODO: ez itt nem ay igazi, kitalálni vmit! - J.
|
|
private readonly CacheKey _auctionAllKey = new("Nop.auction.all-{0}", AUCTION_PATTERN_KEY);
|
|
public const string AUCTION_PATTERN_KEY = "Nop.auction.";
|
|
|
|
protected readonly IMgLockService LockService;
|
|
|
|
public ILogger Logger { get; init; }
|
|
public INopDataProvider DataProvider { get; init; }
|
|
|
|
//public IHttpContextAccessor HttpContextAccessor { get; init; }
|
|
|
|
public MgDbContextBase(INopDataProvider dataProvider, IMgLockService lockService, IEnumerable<IAcLogWriterBase> logWriters)
|
|
{
|
|
LockService = lockService;
|
|
|
|
Logger = new Logger<MgDbContextBase>(logWriters.ToArray());
|
|
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;
|
|
}
|
|
} |