mirror of https://github.com/abpframework/abp.git
21 changed files with 340 additions and 35 deletions
@ -0,0 +1,9 @@ |
|||
{ |
|||
"ConnectionStrings": { |
|||
"Default": "Server=localhost;Database=AbpDesk;Trusted_Connection=True;", |
|||
"Modules": { |
|||
"Module1": "Module1-ConnString", //TODO: Remove this dummy definitions |
|||
"Module2": "Module2-ConnString" |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
{ |
|||
"ConnectionStrings": { |
|||
"Default": "Server=localhost;Database=AbpDesk;Trusted_Connection=True;", |
|||
"Modules": { |
|||
"Module1": "Module1-ConnString", //TODO: Remove this dummy definitions |
|||
"Module2": "Module2-ConnString" |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
using JetBrains.Annotations; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
public class AbpDbContextConfigurationContext |
|||
{ |
|||
public string ConnectionString { get; } |
|||
|
|||
public string ModuleName { get; } |
|||
|
|||
public virtual DbContextOptionsBuilder DbContextOptions { get; protected set; } |
|||
|
|||
public AbpDbContextConfigurationContext(string connectionString, [CanBeNull] string moduleName) |
|||
{ |
|||
ConnectionString = connectionString; |
|||
ModuleName = moduleName; |
|||
DbContextOptions = new DbContextOptionsBuilder(); |
|||
} |
|||
} |
|||
|
|||
public class AbpDbContextConfigurationContext<TDbContext> : AbpDbContextConfigurationContext |
|||
where TDbContext : AbpDbContext<TDbContext> |
|||
{ |
|||
public new DbContextOptionsBuilder<TDbContext> DbContextOptions => (DbContextOptionsBuilder<TDbContext>)base.DbContextOptions; |
|||
|
|||
public AbpDbContextConfigurationContext(string connectionString, [CanBeNull] string moduleName) |
|||
: base(connectionString, moduleName) |
|||
{ |
|||
base.DbContextOptions = new DbContextOptionsBuilder<TDbContext>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
using System; |
|||
using JetBrains.Annotations; |
|||
using Volo; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
public class AbpDbContextConfigurerAction : IAbpDbContextConfigurer |
|||
{ |
|||
[NotNull] |
|||
public Action<AbpDbContextConfigurationContext> Action { get; } |
|||
|
|||
public AbpDbContextConfigurerAction([NotNull] Action<AbpDbContextConfigurationContext> action) |
|||
{ |
|||
Check.NotNull(action, nameof(action)); |
|||
|
|||
Action = action; |
|||
} |
|||
|
|||
public void Configure(AbpDbContextConfigurationContext context) |
|||
{ |
|||
Action.Invoke(context); |
|||
} |
|||
} |
|||
|
|||
public class AbpDbContextConfigurerAction<TDbContext> : AbpDbContextConfigurerAction |
|||
where TDbContext : AbpDbContext<TDbContext> |
|||
{ |
|||
public AbpDbContextConfigurerAction([NotNull] Action<AbpDbContextConfigurationContext> action) |
|||
: base(action) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
public interface IAbpDbContextConfigurer |
|||
{ |
|||
void Configure(AbpDbContextConfigurationContext context); |
|||
} |
|||
|
|||
public interface IAbpDbContextConfigurer<TDbContext> |
|||
where TDbContext : AbpDbContext<TDbContext> |
|||
{ |
|||
void Configure(AbpDbContextConfigurationContext<TDbContext> context); |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore |
|||
{ |
|||
public class AbpDbContextOptions |
|||
{ |
|||
internal Action<AbpDbContextConfigurationContext> DefaultConfigureAction { get; set; } |
|||
|
|||
internal Dictionary<Type, object> ConfigureActions { get; set; } |
|||
|
|||
public AbpDbContextOptions() |
|||
{ |
|||
ConfigureActions = new Dictionary<Type, object>(); |
|||
} |
|||
|
|||
public void Configure([NotNull] Action<AbpDbContextConfigurationContext> action) |
|||
{ |
|||
Check.NotNull(action, nameof(action)); |
|||
|
|||
DefaultConfigureAction = action; |
|||
} |
|||
|
|||
public void Configure<TDbContext>([NotNull] Action<AbpDbContextConfigurationContext<TDbContext>> action) |
|||
where TDbContext : AbpDbContext<TDbContext> |
|||
{ |
|||
Check.NotNull(action, nameof(action)); |
|||
|
|||
ConfigureActions[typeof(TDbContext)] = action; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.DependencyInjection; |
|||
using Volo.ExtensionMethods; |
|||
using Volo.ExtensionMethods.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.Data |
|||
{ |
|||
public class ConnectionStringResolver : IConnectionStringResolver, ITransientDependency |
|||
{ |
|||
private readonly DbConnectionOptions _options; //TODO: Use IOptionsSnapshot
|
|||
|
|||
public ConnectionStringResolver(IOptions<DbConnectionOptions> options) |
|||
{ |
|||
_options = options.Value; |
|||
} |
|||
|
|||
public string Resolve(string modulename) |
|||
{ |
|||
//Get default value
|
|||
var connString = _options.ConnectionStrings.Default; |
|||
|
|||
//Override by module specific value if provided
|
|||
if (!modulename.IsNullOrEmpty()) |
|||
{ |
|||
var moduleConnString = _options.ConnectionStrings.Modules.GetOrDefault(modulename); |
|||
if (!moduleConnString.IsNullOrEmpty()) |
|||
{ |
|||
connString = moduleConnString; |
|||
} |
|||
} |
|||
|
|||
//TODO: Override by tenant conn string
|
|||
//TODO: Override by tenant module specific conn string
|
|||
|
|||
return connString; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.Data |
|||
{ |
|||
public class ConnectionStringsOption |
|||
{ |
|||
public string Default { get; set; } |
|||
|
|||
public Dictionary<string, string> Modules { get; set; } |
|||
|
|||
public ConnectionStringsOption() |
|||
{ |
|||
Modules = new Dictionary<string, string>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
namespace Volo.Abp.Data |
|||
{ |
|||
public class DbConnectionOptions |
|||
{ |
|||
public ConnectionStringsOption ConnectionStrings { get; set; } |
|||
|
|||
public DbConnectionOptions() |
|||
{ |
|||
ConnectionStrings = new ConnectionStringsOption(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,17 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Data |
|||
{ |
|||
public interface IConnectionInfoResolver |
|||
{ |
|||
ConnectionInfo Resolve(); |
|||
} |
|||
|
|||
public class ConnectionInfo |
|||
{ |
|||
public string ConnectionString { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.Data |
|||
{ |
|||
public interface IConnectionStringResolver |
|||
{ |
|||
string Resolve(string modulename); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue