< Summary

Information
Class: Nabs.Persistence.TenantableDbContextFactory<T>
Assembly: Nabs.Persistence
File(s): /home/runner/work/Nabs/Nabs/src/Nabs.Persistence/TenantableDbContextFactory.cs
Tag: 90_14636759620
Line coverage
87%
Covered lines: 14
Uncovered lines: 2
Coverable lines: 16
Total lines: 65
Line coverage: 87.5%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
CreateDbContext(...)75%4492.3%
CreateDbContext()100%210%

File(s)

/home/runner/work/Nabs/Nabs/src/Nabs.Persistence/TenantableDbContextFactory.cs

#LineLine coverage
 1namespace Nabs.Persistence;
 2
 3public 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
 12public 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
 125    private readonly string _databaseNamePrefix = databaseNamePrefix;
 126    private readonly IConfigurationRoot _configurationRoot = configurationRoot;
 27
 28    public TDbContext CreateDbContext(IApplicationContext applicationContext)
 29    {
 130        var databaseName = $"{_databaseNamePrefix}_";
 131        if (applicationContext.TenantIsolationStrategy == TenantIsolationStrategy.SharedShared)
 32        {
 133            databaseName += "SharedShared";
 34        }
 35        else
 36        {
 137            databaseName += $"{applicationContext.TenantIsolationStrategy}_{applicationContext.TenantContext.TenantId}";
 38        }
 39
 140        var serverName = _configurationRoot.GetConnectionString($"{_databaseNamePrefix}ServerName");
 41        string? connectionString;
 142        if (string.IsNullOrWhiteSpace(serverName))
 43        {
 144            connectionString = string.Format(_testConnectionStringTemplate, databaseName);
 45        }
 46        else
 47        {
 048            connectionString = string.Format(_envConnectionStringTemplate, serverName, databaseName);
 49        }
 50
 151        var optionsBuilder = new DbContextOptionsBuilder<TDbContext>();
 152        optionsBuilder.UseSqlServer(connectionString);
 53
 154        var options = optionsBuilder
 155            .Options;
 56
 157        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    {
 063        throw new NotImplementedException();
 64    }
 65}