| | | 1 | | namespace Nabs.Persistence; |
| | | 2 | | |
| | | 3 | | public interface ITenantableDbContextFactory<TDbContext> : IDbContextFactory<TDbContext> |
| | | 4 | | where TDbContext : DbContext, ITenantableDbContext |
| | | 5 | | { |
| | | 6 | | TDbContext CreateDbContext(IApplicationContext applicationContext); |
| | | 7 | | |
| | | 8 | | [Obsolete("Use the CreateDbContext method that requires an IApplicationContext.")] |
| | | 9 | | new TDbContext CreateDbContext(); |
| | | 10 | | } |
| | | 11 | | |
| | | 12 | | public class TenantableDbContextFactory<TDbContext>( |
| | | 13 | | string databaseNamePrefix, |
| | | 14 | | IConfigurationRoot configurationRoot) |
| | | 15 | | : ITenantableDbContextFactory<TDbContext> |
| | | 16 | | where TDbContext : DbContext, ITenantableDbContext |
| | | 17 | | { |
| | | 18 | | //TODO: DWS: This needs to be set up properly |
| | | 19 | | const string _testConnectionStringTemplate = "Server=localhost,14331;Database={0};User Id=sa;Password=Password123;Tr |
| | | 20 | | |
| | | 21 | | //TODO: DWS: This assumes azure Sql we will need to provide various options for clouds other than Azure. |
| | | 22 | | const string _envConnectionStringTemplate = "Server=tcp:{0};Database={1};Authentication=Active Directory Default;Enc |
| | | 23 | | |
| | | 24 | | |
| | 1 | 25 | | private readonly string _databaseNamePrefix = databaseNamePrefix; |
| | 1 | 26 | | private readonly IConfigurationRoot _configurationRoot = configurationRoot; |
| | | 27 | | |
| | | 28 | | public TDbContext CreateDbContext(IApplicationContext applicationContext) |
| | | 29 | | { |
| | 1 | 30 | | var databaseName = $"{_databaseNamePrefix}_"; |
| | 1 | 31 | | if (applicationContext.TenantIsolationStrategy == TenantIsolationStrategy.SharedShared) |
| | | 32 | | { |
| | 1 | 33 | | databaseName += "SharedShared"; |
| | | 34 | | } |
| | | 35 | | else |
| | | 36 | | { |
| | 1 | 37 | | databaseName += $"{applicationContext.TenantIsolationStrategy}_{applicationContext.TenantContext.TenantId}"; |
| | | 38 | | } |
| | | 39 | | |
| | 1 | 40 | | var serverName = _configurationRoot.GetConnectionString($"{_databaseNamePrefix}ServerName"); |
| | | 41 | | string? connectionString; |
| | 1 | 42 | | if (string.IsNullOrWhiteSpace(serverName)) |
| | | 43 | | { |
| | 1 | 44 | | connectionString = string.Format(_testConnectionStringTemplate, databaseName); |
| | | 45 | | } |
| | | 46 | | else |
| | | 47 | | { |
| | 0 | 48 | | connectionString = string.Format(_envConnectionStringTemplate, serverName, databaseName); |
| | | 49 | | } |
| | | 50 | | |
| | 1 | 51 | | var optionsBuilder = new DbContextOptionsBuilder<TDbContext>(); |
| | 1 | 52 | | optionsBuilder.UseSqlServer(connectionString); |
| | | 53 | | |
| | 1 | 54 | | var options = optionsBuilder |
| | 1 | 55 | | .Options; |
| | | 56 | | |
| | 1 | 57 | | return (TDbContext)Activator.CreateInstance(typeof(TDbContext), options, applicationContext)!; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | [Obsolete("Use the CreateDbContext method that requires an IApplicationContext.")] |
| | | 61 | | public TDbContext CreateDbContext() |
| | | 62 | | { |
| | 0 | 63 | | throw new NotImplementedException(); |
| | | 64 | | } |
| | | 65 | | } |