mirror of https://github.com/abpframework/abp.git
11 changed files with 172 additions and 41 deletions
@ -1,31 +1,65 @@ |
|||
using System.Threading.Tasks; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace Volo.Abp.Repositories.EntityFrameworkCore |
|||
{ |
|||
public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbContext> |
|||
public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbContext> |
|||
where TDbContext : AbpDbContext<TDbContext> |
|||
{ |
|||
private readonly TDbContext _dbContext; |
|||
|
|||
private readonly IUnitOfWorkManager _unitOfWorkManager; |
|||
private readonly IConnectionStringResolver _connectionStringResolver; |
|||
|
|||
public UnitOfWorkDbContextProvider( |
|||
TDbContext dbContext, |
|||
IUnitOfWorkManager unitOfWorkManager) //TODO: Should create this dynamically inside a unit of work.
|
|||
IUnitOfWorkManager unitOfWorkManager, |
|||
IConnectionStringResolver connectionStringResolver) |
|||
{ |
|||
_dbContext = dbContext; |
|||
_unitOfWorkManager = unitOfWorkManager; |
|||
_connectionStringResolver = connectionStringResolver; |
|||
} |
|||
|
|||
public TDbContext GetDbContext() |
|||
{ |
|||
//if (_unitOfWorkManager.Current == null)
|
|||
//{
|
|||
// throw new AbpException("A DbContext can only be created inside a unit of work!");
|
|||
//}
|
|||
var unitOfWork = _unitOfWorkManager.Current; |
|||
if (unitOfWork == null) |
|||
{ |
|||
throw new AbpException("A DbContext can only be created inside a unit of work!"); |
|||
} |
|||
|
|||
var moduleName = "";//TODO: Get module name from DbContext?
|
|||
var dbContextKey = $"{moduleName}_{typeof(TDbContext).FullName}_{_connectionStringResolver.Resolve(moduleName)}"; |
|||
|
|||
var databaseApi = unitOfWork.GetOrAddDatabaseApi( |
|||
dbContextKey, |
|||
() => new DbContextDatabaseApi<TDbContext>( |
|||
unitOfWork.ServiceProvider.GetRequiredService<TDbContext>() |
|||
)); |
|||
|
|||
return ((DbContextDatabaseApi<TDbContext>)databaseApi).DbContext; |
|||
} |
|||
} |
|||
|
|||
public class DbContextDatabaseApi<TDbContext> : IDatabaseApi |
|||
where TDbContext : AbpDbContext<TDbContext> |
|||
{ |
|||
public TDbContext DbContext { get; } |
|||
|
|||
return _dbContext; |
|||
public DbContextDatabaseApi(TDbContext dbContext) |
|||
{ |
|||
DbContext = dbContext; |
|||
} |
|||
|
|||
public Task SaveChangesAsync() |
|||
{ |
|||
return DbContext.SaveChangesAsync(); |
|||
} |
|||
|
|||
public Task CommitAsync() |
|||
{ |
|||
return DbContext.SaveChangesAsync(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,23 +1,68 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Volo.ExtensionMethods.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.Uow |
|||
{ |
|||
public class UnitOfWork : IUnitOfWork |
|||
{ |
|||
public IServiceProvider ServiceProvider { get; } |
|||
|
|||
private readonly Dictionary<string, IDatabaseApi> _databaseApis; |
|||
|
|||
public UnitOfWork(IServiceProvider serviceProvider) |
|||
{ |
|||
ServiceProvider = serviceProvider; |
|||
|
|||
_databaseApis = new Dictionary<string, IDatabaseApi>(); |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
throw new NotImplementedException(); |
|||
//TODO: Remove itself from IUnitOfWorkManager
|
|||
} |
|||
|
|||
public async Task SaveChangesAsync() |
|||
{ |
|||
foreach (var databaseApi in _databaseApis.Values) |
|||
{ |
|||
await databaseApi.SaveChangesAsync(); |
|||
} |
|||
} |
|||
|
|||
public Task SaveChangesAsync() |
|||
public async Task CompleteAsync() |
|||
{ |
|||
throw new NotImplementedException(); |
|||
foreach (var databaseApi in _databaseApis.Values) |
|||
{ |
|||
await databaseApi.CommitAsync(); |
|||
} |
|||
} |
|||
|
|||
public Task CompleteAsync() |
|||
public IDatabaseApi FindDatabaseApi(string id) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
return _databaseApis.GetOrDefault(id); |
|||
} |
|||
|
|||
public IDatabaseApi GetOrAddDatabaseApi(string id, Func<IDatabaseApi> factory) |
|||
{ |
|||
Check.NotNull(id, nameof(id)); |
|||
Check.NotNull(factory, nameof(factory)); |
|||
|
|||
return _databaseApis.GetOrAdd(id, factory); |
|||
} |
|||
|
|||
public IDatabaseApi AddDatabaseApi(string id, IDatabaseApi databaseApi) |
|||
{ |
|||
Check.NotNull(id, nameof(id)); |
|||
Check.NotNull(databaseApi, nameof(databaseApi)); |
|||
|
|||
if (_databaseApis.ContainsKey(id)) |
|||
{ |
|||
throw new AbpException($"There is already a database api with same id: {id}"); |
|||
} |
|||
|
|||
return _databaseApis[id] = databaseApi; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue