mirror of https://github.com/abpframework/abp
parent
08dd58c92e
commit
d3c613d121
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.EntityFrameworkCore;
|
||||
using Volo.ExtensionMethods.Collections.Generic;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
internal static class DbContextOptionsFactory
|
||||
{
|
||||
public static DbContextOptions<TDbContext> Create<TDbContext>(IServiceProvider serviceProvider)
|
||||
where TDbContext : AbpDbContext<TDbContext>
|
||||
{
|
||||
const string moduleName = ""; //TODO: Use AbpModuleDescriptor instead of module name?
|
||||
|
||||
using (var scope = serviceProvider.CreateScope())
|
||||
{
|
||||
var connInfoResolver = scope.ServiceProvider.GetRequiredService<IConnectionStringResolver>();
|
||||
|
||||
var context = new AbpDbContextConfigurationContext<TDbContext>(connInfoResolver.Resolve(moduleName), moduleName);
|
||||
|
||||
var dbContextOptions = scope.ServiceProvider.GetRequiredService<IOptions<AbpDbContextOptions>>().Value;
|
||||
|
||||
var configureAction = dbContextOptions.ConfigureActions.GetOrDefault(typeof(TDbContext));
|
||||
if (configureAction != null)
|
||||
{
|
||||
((Action<AbpDbContextConfigurationContext<TDbContext>>)configureAction).Invoke(context);
|
||||
}
|
||||
else if (dbContextOptions.DefaultConfigureAction != null)
|
||||
{
|
||||
dbContextOptions.DefaultConfigureAction.Invoke(context);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new AbpException("Should set a configure action for dbcontext"); //TODO: Better message
|
||||
}
|
||||
|
||||
return context.DbContextOptions.Options;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,20 +0,0 @@
|
||||
using Volo.Abp.EntityFrameworkCore;
|
||||
|
||||
namespace Volo.Abp.Repositories.EntityFrameworkCore
|
||||
{
|
||||
public class DefaultDbContextProvider<TDbContext> : IDbContextProvider<TDbContext>
|
||||
where TDbContext : AbpDbContext<TDbContext>
|
||||
{
|
||||
private readonly TDbContext _dbContext;
|
||||
|
||||
public DefaultDbContextProvider(TDbContext dbContext) //TODO: Should create this dynamically!
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public TDbContext GetDbContext()
|
||||
{
|
||||
return _dbContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
using Volo.Abp.EntityFrameworkCore;
|
||||
using Volo.Abp.Uow;
|
||||
|
||||
namespace Volo.Abp.Repositories.EntityFrameworkCore
|
||||
{
|
||||
public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbContext>
|
||||
where TDbContext : AbpDbContext<TDbContext>
|
||||
{
|
||||
private readonly TDbContext _dbContext;
|
||||
|
||||
private readonly IUnitOfWorkManager _unitOfWorkManager;
|
||||
|
||||
public UnitOfWorkDbContextProvider(
|
||||
TDbContext dbContext,
|
||||
IUnitOfWorkManager unitOfWorkManager) //TODO: Should create this dynamically inside a unit of work.
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_unitOfWorkManager = unitOfWorkManager;
|
||||
}
|
||||
|
||||
public TDbContext GetDbContext()
|
||||
{
|
||||
//if (_unitOfWorkManager.Current == null)
|
||||
//{
|
||||
// throw new AbpException("A DbContext can only be created inside a unit of work!");
|
||||
//}
|
||||
|
||||
return _dbContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
using System.Threading.Tasks;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Volo.Abp.Uow
|
||||
{
|
||||
internal class ChildUnitOfWork : IUnitOfWork
|
||||
{
|
||||
private readonly IUnitOfWork _parent;
|
||||
|
||||
public ChildUnitOfWork([NotNull] IUnitOfWork parent)
|
||||
{
|
||||
Check.NotNull(parent, nameof(parent));
|
||||
|
||||
_parent = parent;
|
||||
}
|
||||
|
||||
public Task SaveChangesAsync()
|
||||
{
|
||||
return _parent.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public Task CompleteAsync()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Volo.Abp.Uow
|
||||
{
|
||||
internal class UnitOfWorkInfo
|
||||
{
|
||||
public IUnitOfWork UnitOfWork { get; set; }
|
||||
|
||||
public IServiceScope ServiceScope { get; set; }
|
||||
|
||||
public UnitOfWorkInfo(IUnitOfWork unitOfWork, IServiceScope serviceScope)
|
||||
{
|
||||
UnitOfWork = unitOfWork;
|
||||
ServiceScope = serviceScope;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue