Browse Source

Refactored UOW.

pull/112/head
Halil İbrahim Kalkan 9 years ago
parent
commit
6c6b1b080a
  1. BIN
      src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll
  2. 5
      src/Volo.Abp/Volo/Abp/Uow/IUnitOfWorkManager.cs
  3. 36
      src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkManager.cs
  4. 21
      src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkManagerExtensions.cs
  5. 6
      src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkStartOptions.cs
  6. 2
      test/Volo.Abp.Tests/Volo/Abp/Uow/UnitOfWork_Nested_Tests.cs

BIN
src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll

Binary file not shown.

5
src/Volo.Abp/Volo/Abp/Uow/IUnitOfWorkManager.cs

@ -8,7 +8,10 @@ namespace Volo.Abp.Uow
IUnitOfWork Current { get; }
[NotNull]
IBasicUnitOfWork Begin([NotNull] UnitOfWorkStartOptions options);
IBasicUnitOfWork Begin([NotNull] UnitOfWorkStartOptions options, bool requiresNew = false);
[NotNull]
IBasicUnitOfWork Reserve([NotNull] string reservationName, bool requiresNew = false);
void BeginReserved([NotNull] string reservationName, [NotNull] UnitOfWorkStartOptions options);

36
src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkManager.cs

@ -1,4 +1,5 @@
using System;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
@ -17,22 +18,38 @@ namespace Volo.Abp.Uow
_ambientUnitOfWork = ambientUnitOfWork;
}
public IBasicUnitOfWork Begin(UnitOfWorkStartOptions options)
public IBasicUnitOfWork Begin(UnitOfWorkStartOptions options, bool requiresNew = false)
{
Check.NotNull(options, nameof(options));
if (!options.RequiresNew && _ambientUnitOfWork.UnitOfWork != null && !_ambientUnitOfWork.UnitOfWork.IsReserved)
if (!requiresNew && _ambientUnitOfWork.UnitOfWork != null && !_ambientUnitOfWork.UnitOfWork.IsReserved)
{
return new ChildUnitOfWork(_ambientUnitOfWork.UnitOfWork);
}
if (_ambientUnitOfWork.UnitOfWork != null)
var unitOfWork = CreateNewUnitOfWork();
unitOfWork.SetOptions(options);
return unitOfWork;
}
public IBasicUnitOfWork Reserve(string reservationName, bool requiresNew = false)
{
Check.NotNull(reservationName, nameof(reservationName));
if (!requiresNew &&
_ambientUnitOfWork.UnitOfWork != null &&
_ambientUnitOfWork.UnitOfWork.IsReservedFor(reservationName))
{
//Requires new because there is already a current UOW but it's reserved
options.RequiresNew = true;
return new ChildUnitOfWork(_ambientUnitOfWork.UnitOfWork);
}
return CreateUnitOfWork(options);
var unitOfWork = CreateNewUnitOfWork();
unitOfWork.IsReserved = true;
unitOfWork.ReservationName = reservationName;
return unitOfWork;
}
public void BeginReserved(string reservationName, UnitOfWorkStartOptions options)
@ -62,6 +79,7 @@ namespace Volo.Abp.Uow
uow.IsReserved = false;
uow.SetOptions(options);
return true;
}
@ -78,10 +96,9 @@ namespace Volo.Abp.Uow
return uow;
}
private IUnitOfWork CreateUnitOfWork(UnitOfWorkStartOptions options)
private IUnitOfWork CreateNewUnitOfWork()
{
var scope = _serviceProvider.CreateScope();
try
{
var outerUow = _ambientUnitOfWork.UnitOfWork;
@ -89,9 +106,6 @@ namespace Volo.Abp.Uow
var unitOfWork = scope.ServiceProvider.GetRequiredService<IUnitOfWork>();
unitOfWork.SetOuter(outerUow);
unitOfWork.IsReserved = options.ReservationName != null;
unitOfWork.ReservationName = options.ReservationName;
unitOfWork.SetOptions(options); //TODO: Should not call this for reservation?
_ambientUnitOfWork.SetUnitOfWork(unitOfWork);

21
src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkManagerExtensions.cs

@ -5,28 +5,11 @@ namespace Volo.Abp.Uow
public static class UnitOfWorkManagerExtensions
{
[NotNull]
public static IBasicUnitOfWork Begin([NotNull] this IUnitOfWorkManager unitOfWorkManager)
public static IBasicUnitOfWork Begin([NotNull] this IUnitOfWorkManager unitOfWorkManager, bool requiresNew = false)
{
Check.NotNull(unitOfWorkManager, nameof(unitOfWorkManager));
return unitOfWorkManager.Begin(new UnitOfWorkStartOptions());
}
[NotNull]
public static IBasicUnitOfWork BeginNew([NotNull] this IUnitOfWorkManager unitOfWorkManager)
{
Check.NotNull(unitOfWorkManager, nameof(unitOfWorkManager));
return unitOfWorkManager.Begin(new UnitOfWorkStartOptions {RequiresNew = true});
}
[NotNull]
public static IBasicUnitOfWork Reserve([NotNull] this IUnitOfWorkManager unitOfWorkManager, [NotNull] string reservationName)
{
Check.NotNull(unitOfWorkManager, nameof(unitOfWorkManager));
Check.NotNull(reservationName, nameof(reservationName));
return unitOfWorkManager.Begin(new UnitOfWorkStartOptions { ReservationName = reservationName });
return unitOfWorkManager.Begin(new UnitOfWorkStartOptions(), requiresNew);
}
public static void BeginReserved([NotNull] this IUnitOfWorkManager unitOfWorkManager, [NotNull] string reservationName)

6
src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkStartOptions.cs

@ -5,12 +5,6 @@ namespace Volo.Abp.Uow
{
public class UnitOfWorkStartOptions
{
//Revise this since Begin/BeginReserved accepts different options and that can make confusion!
public bool RequiresNew { get; set; }
public string ReservationName { get; set; }
public bool? IsTransactional { get; set; }
public IsolationLevel? IsolationLevel { get; set; }

2
test/Volo.Abp.Tests/Volo/Abp/Uow/UnitOfWork_Nested_Tests.cs

@ -26,7 +26,7 @@ namespace Volo.Abp.Uow
_unitOfWorkManager.Current.ShouldNotBeNull();
_unitOfWorkManager.Current.ShouldBe(uow1);
using (var uow2 = _unitOfWorkManager.BeginNew())
using (var uow2 = _unitOfWorkManager.Begin(requiresNew: true))
{
_unitOfWorkManager.Current.ShouldNotBeNull();
_unitOfWorkManager.Current.Id.ShouldNotBe(uow1.Id);

Loading…
Cancel
Save