diff --git a/src/Volo.Abp/Volo/Abp/Uow/ChildUnitOfWork.cs b/src/Volo.Abp/Volo/Abp/Uow/ChildUnitOfWork.cs index d5a6e82246..3683bbd7c4 100644 --- a/src/Volo.Abp/Volo/Abp/Uow/ChildUnitOfWork.cs +++ b/src/Volo.Abp/Volo/Abp/Uow/ChildUnitOfWork.cs @@ -79,6 +79,11 @@ namespace Volo.Abp.Uow return _parent.FindDatabaseApi(key); } + public void AddDatabaseApi(string key, IDatabaseApi api) + { + _parent.AddDatabaseApi(key, api); + } + public IDatabaseApi GetOrAddDatabaseApi(string key, Func factory) { return _parent.GetOrAddDatabaseApi(key, factory); @@ -94,6 +99,11 @@ namespace Volo.Abp.Uow _parent.AddTransactionApi(key, api); } + public ITransactionApi GetOrAddTransactionApi(string key, Func factory) + { + return _parent.GetOrAddTransactionApi(key, factory); + } + public void Dispose() { diff --git a/src/Volo.Abp/Volo/Abp/Uow/IDatabaseApiContainer.cs b/src/Volo.Abp/Volo/Abp/Uow/IDatabaseApiContainer.cs index d896574a9a..5ee4ff5f41 100644 --- a/src/Volo.Abp/Volo/Abp/Uow/IDatabaseApiContainer.cs +++ b/src/Volo.Abp/Volo/Abp/Uow/IDatabaseApiContainer.cs @@ -9,6 +9,8 @@ namespace Volo.Abp.Uow [CanBeNull] IDatabaseApi FindDatabaseApi([NotNull] string key); + void AddDatabaseApi([NotNull] string key, [NotNull] IDatabaseApi api); + [NotNull] IDatabaseApi GetOrAddDatabaseApi([NotNull] string key, [NotNull] Func factory); } diff --git a/src/Volo.Abp/Volo/Abp/Uow/ITransactionApiContainer.cs b/src/Volo.Abp/Volo/Abp/Uow/ITransactionApiContainer.cs index dde7ace71f..b3dd050dc5 100644 --- a/src/Volo.Abp/Volo/Abp/Uow/ITransactionApiContainer.cs +++ b/src/Volo.Abp/Volo/Abp/Uow/ITransactionApiContainer.cs @@ -1,4 +1,5 @@ -using JetBrains.Annotations; +using System; +using JetBrains.Annotations; namespace Volo.Abp.Uow { @@ -8,5 +9,8 @@ namespace Volo.Abp.Uow ITransactionApi FindTransactionApi([NotNull] string key); void AddTransactionApi([NotNull] string key, [NotNull] ITransactionApi api); + + [NotNull] + ITransactionApi GetOrAddTransactionApi([NotNull] string key, [NotNull] Func factory); } } \ No newline at end of file diff --git a/src/Volo.Abp/Volo/Abp/Uow/UnitOfWork.cs b/src/Volo.Abp/Volo/Abp/Uow/UnitOfWork.cs index 173a8718df..810a94ec91 100644 --- a/src/Volo.Abp/Volo/Abp/Uow/UnitOfWork.cs +++ b/src/Volo.Abp/Volo/Abp/Uow/UnitOfWork.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -using JetBrains.Annotations; using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; @@ -115,6 +114,19 @@ namespace Volo.Abp.Uow return _databaseApis.GetOrDefault(key); } + public void AddDatabaseApi(string key, IDatabaseApi api) + { + Check.NotNull(key, nameof(key)); + Check.NotNull(api, nameof(api)); + + if (_databaseApis.ContainsKey(key)) + { + throw new AbpException("There is already a database API in this unit of work with given key: " + key); + } + + _databaseApis.Add(key, api); + } + public IDatabaseApi GetOrAddDatabaseApi(string key, Func factory) { Check.NotNull(key, nameof(key)); @@ -137,12 +149,20 @@ namespace Volo.Abp.Uow if (_transactionApis.ContainsKey(key)) { - throw new AbpException("There is already a transaction API in this unit of work!"); + throw new AbpException("There is already a transaction API in this unit of work with given key: " + key); } _transactionApis.Add(key, api); } + public ITransactionApi GetOrAddTransactionApi(string key, Func factory) + { + Check.NotNull(key, nameof(key)); + Check.NotNull(factory, nameof(factory)); + + return _transactionApis.GetOrAdd(key, factory); + } + protected virtual void OnCompleted() { Completed.InvokeSafely(this);