Refactored.

pull/112/head
Halil İbrahim Kalkan 7 years ago
parent aad83726c7
commit 9be60fc4c3

@ -32,7 +32,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Uow
return; return;
} }
var options = new UnitOfWorkStartOptions(); var options = new UnitOfWorkOptions();
unitOfWorkAttr?.SetOptions(options); unitOfWorkAttr?.SetOptions(options);

@ -9,7 +9,7 @@ namespace Volo.Abp.Uow
{ {
public Guid Id => _parent.Id; public Guid Id => _parent.Id;
public IUnitOfWorkStartOptions Options => _parent.Options; public IUnitOfWorkOptions Options => _parent.Options;
public IUnitOfWork Outer => _parent.Outer; public IUnitOfWork Outer => _parent.Outer;
@ -30,9 +30,9 @@ namespace Volo.Abp.Uow
_parent.SetOuter(outer); _parent.SetOuter(outer);
} }
public void SetOptions(UnitOfWorkStartOptions options) public void Initialize(UnitOfWorkOptions options)
{ {
_parent.SetOptions(options); _parent.Initialize(options);
} }
public event EventHandler Completed; public event EventHandler Completed;

@ -4,7 +4,7 @@ namespace Volo.Abp.Uow
{ {
public interface IUnitOfWork : IBasicUnitOfWork, IDatabaseApiContainer, ITransactionApiContainer public interface IUnitOfWork : IBasicUnitOfWork, IDatabaseApiContainer, ITransactionApiContainer
{ {
IUnitOfWorkStartOptions Options { get; } IUnitOfWorkOptions Options { get; }
IUnitOfWork Outer { get; } IUnitOfWork Outer { get; }
@ -14,6 +14,6 @@ namespace Volo.Abp.Uow
void SetOuter([CanBeNull] IUnitOfWork outer); void SetOuter([CanBeNull] IUnitOfWork outer);
void SetOptions(UnitOfWorkStartOptions options); void Initialize([NotNull] UnitOfWorkOptions options);
} }
} }

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

@ -3,7 +3,7 @@ using System.Data;
namespace Volo.Abp.Uow namespace Volo.Abp.Uow
{ {
public interface IUnitOfWorkStartOptions public interface IUnitOfWorkOptions
{ {
bool IsTransactional { get; } bool IsTransactional { get; }

@ -11,7 +11,7 @@ namespace Volo.Abp.Uow
{ {
public Guid Id { get; } = Guid.NewGuid(); public Guid Id { get; } = Guid.NewGuid();
public IUnitOfWorkStartOptions Options { get; private set; } public IUnitOfWorkOptions Options { get; private set; }
public IUnitOfWork Outer { get; private set; } public IUnitOfWork Outer { get; private set; }
@ -27,13 +27,13 @@ namespace Volo.Abp.Uow
private readonly Dictionary<string, IDatabaseApi> _databaseApis; private readonly Dictionary<string, IDatabaseApi> _databaseApis;
private readonly Dictionary<string, ITransactionApi> _transactionApis; private readonly Dictionary<string, ITransactionApi> _transactionApis;
private readonly UnitOfWorkOptions _defaultOptions; private readonly UnitOfWorkDefaultOptions _defaultOptions;
private Exception _exception; private Exception _exception;
private bool _isCompleted; private bool _isCompleted;
private bool _isDisposed; private bool _isDisposed;
public UnitOfWork(IServiceProvider serviceProvider, IOptions<UnitOfWorkOptions> options) public UnitOfWork(IServiceProvider serviceProvider, IOptions<UnitOfWorkDefaultOptions> options)
{ {
ServiceProvider = serviceProvider; ServiceProvider = serviceProvider;
_defaultOptions = options.Value; _defaultOptions = options.Value;
@ -42,11 +42,13 @@ namespace Volo.Abp.Uow
_transactionApis = new Dictionary<string, ITransactionApi>(); _transactionApis = new Dictionary<string, ITransactionApi>();
} }
public void SetOptions(UnitOfWorkStartOptions options) public void Initialize(UnitOfWorkOptions options)
{ {
Check.NotNull(options, nameof(options));
if (Options != null) if (Options != null)
{ {
throw new AbpException("Options must be set only once!"); throw new AbpException("This unit of work is already initialized before!");
} }
Options = _defaultOptions.Normalize(options.Clone()); Options = _defaultOptions.Normalize(options.Clone());

@ -37,7 +37,7 @@ namespace Volo.Abp.Uow
/// </summary> /// </summary>
public bool IsDisabled { get; set; } public bool IsDisabled { get; set; }
public virtual void SetOptions(UnitOfWorkStartOptions options) public virtual void SetOptions(UnitOfWorkOptions options)
{ {
if (IsTransactional.HasValue) if (IsTransactional.HasValue)
{ {

@ -0,0 +1,34 @@
using System;
using System.Data;
namespace Volo.Abp.Uow
{
//TODO: Implement default options!
/// <summary>
/// Global (default) unit of work options
/// </summary>
public class UnitOfWorkDefaultOptions
{
public UnitOfWorkTransactionBehavior TransactionBehavior { get; set; }
public IsolationLevel? IsolationLevel { get; set; }
public TimeSpan? Timeout { get; set; }
internal UnitOfWorkOptions Normalize(UnitOfWorkOptions options)
{
if (options.IsolationLevel == null)
{
options.IsolationLevel = IsolationLevel;
}
if (options.Timeout == null)
{
options.Timeout = Timeout;
}
return options;
}
}
}

@ -19,7 +19,7 @@ namespace Volo.Abp.Uow
_ambientUnitOfWork = ambientUnitOfWork; _ambientUnitOfWork = ambientUnitOfWork;
} }
public IBasicUnitOfWork Begin(UnitOfWorkStartOptions options, bool requiresNew = false) public IBasicUnitOfWork Begin(UnitOfWorkOptions options, bool requiresNew = false)
{ {
Check.NotNull(options, nameof(options)); Check.NotNull(options, nameof(options));
@ -29,7 +29,7 @@ namespace Volo.Abp.Uow
} }
var unitOfWork = CreateNewUnitOfWork(); var unitOfWork = CreateNewUnitOfWork();
unitOfWork.SetOptions(options); unitOfWork.Initialize(options);
return unitOfWork; return unitOfWork;
} }
@ -53,7 +53,7 @@ namespace Volo.Abp.Uow
return unitOfWork; return unitOfWork;
} }
public void BeginReserved(string reservationName, UnitOfWorkStartOptions options) public void BeginReserved(string reservationName, UnitOfWorkOptions options)
{ {
if (!TryBeginReserved(reservationName, options)) if (!TryBeginReserved(reservationName, options))
{ {
@ -61,7 +61,7 @@ namespace Volo.Abp.Uow
} }
} }
public bool TryBeginReserved(string reservationName, UnitOfWorkStartOptions options) public bool TryBeginReserved(string reservationName, UnitOfWorkOptions options)
{ {
Check.NotNull(reservationName, nameof(reservationName)); Check.NotNull(reservationName, nameof(reservationName));
@ -79,7 +79,7 @@ namespace Volo.Abp.Uow
} }
uow.IsReserved = false; uow.IsReserved = false;
uow.SetOptions(options); uow.Initialize(options);
return true; return true;
} }

@ -9,7 +9,7 @@ namespace Volo.Abp.Uow
{ {
Check.NotNull(unitOfWorkManager, nameof(unitOfWorkManager)); Check.NotNull(unitOfWorkManager, nameof(unitOfWorkManager));
return unitOfWorkManager.Begin(new UnitOfWorkStartOptions(), requiresNew); return unitOfWorkManager.Begin(new UnitOfWorkOptions(), requiresNew);
} }
public static void BeginReserved([NotNull] this IUnitOfWorkManager unitOfWorkManager, [NotNull] string reservationName) public static void BeginReserved([NotNull] this IUnitOfWorkManager unitOfWorkManager, [NotNull] string reservationName)
@ -17,7 +17,7 @@ namespace Volo.Abp.Uow
Check.NotNull(unitOfWorkManager, nameof(unitOfWorkManager)); Check.NotNull(unitOfWorkManager, nameof(unitOfWorkManager));
Check.NotNull(reservationName, nameof(reservationName)); Check.NotNull(reservationName, nameof(reservationName));
unitOfWorkManager.BeginReserved(reservationName, new UnitOfWorkStartOptions()); unitOfWorkManager.BeginReserved(reservationName, new UnitOfWorkOptions());
} }
public static void TryBeginReserved([NotNull] this IUnitOfWorkManager unitOfWorkManager, [NotNull] string reservationName) public static void TryBeginReserved([NotNull] this IUnitOfWorkManager unitOfWorkManager, [NotNull] string reservationName)
@ -25,7 +25,7 @@ namespace Volo.Abp.Uow
Check.NotNull(unitOfWorkManager, nameof(unitOfWorkManager)); Check.NotNull(unitOfWorkManager, nameof(unitOfWorkManager));
Check.NotNull(reservationName, nameof(reservationName)); Check.NotNull(reservationName, nameof(reservationName));
unitOfWorkManager.TryBeginReserved(reservationName, new UnitOfWorkStartOptions()); unitOfWorkManager.TryBeginReserved(reservationName, new UnitOfWorkOptions());
} }
} }
} }

@ -3,32 +3,25 @@ using System.Data;
namespace Volo.Abp.Uow namespace Volo.Abp.Uow
{ {
//TODO: Implement default options! public class UnitOfWorkOptions : IUnitOfWorkOptions
/// <summary>
/// Global (default) unit of work options
/// </summary>
public class UnitOfWorkOptions
{ {
public UnitOfWorkTransactionBehavior TransactionBehavior { get; set; } /// <summary>
/// Default: false.
/// </summary>
public bool IsTransactional { get; set; }
public IsolationLevel? IsolationLevel { get; set; } public IsolationLevel? IsolationLevel { get; set; }
public TimeSpan? Timeout { get; set; } public TimeSpan? Timeout { get; set; }
internal UnitOfWorkStartOptions Normalize(UnitOfWorkStartOptions options) public UnitOfWorkOptions Clone()
{ {
if (options.IsolationLevel == null) return new UnitOfWorkOptions
{ {
options.IsolationLevel = IsolationLevel; IsTransactional = IsTransactional,
} IsolationLevel = IsolationLevel,
Timeout = Timeout
if (options.Timeout == null) };
{
options.Timeout = Timeout;
}
return options;
} }
} }
} }

@ -1,27 +0,0 @@
using System;
using System.Data;
namespace Volo.Abp.Uow
{
public class UnitOfWorkStartOptions : IUnitOfWorkStartOptions
{
/// <summary>
/// Default: false.
/// </summary>
public bool IsTransactional { get; set; }
public IsolationLevel? IsolationLevel { get; set; }
public TimeSpan? Timeout { get; set; }
public UnitOfWorkStartOptions Clone()
{
return new UnitOfWorkStartOptions
{
IsTransactional = IsTransactional,
IsolationLevel = IsolationLevel,
Timeout = Timeout
};
}
}
}
Loading…
Cancel
Save