Skip to content

Commit

Permalink
chore: remove optional param for MultiTenantDbContext.Create (#902)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewTriesToCode authored Nov 13, 2024
1 parent 8e830e1 commit 2fc065f
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ public abstract class MultiTenantDbContext : DbContext, IMultiTenantDbContext
/// <inheritdoc />
public TenantNotSetMode TenantNotSetMode { get; set; } = TenantNotSetMode.Throw;

/// <summary>
/// Creates a new instance of a multitenant context that accepts a IMultiTenantContextAccessor instance and an optional DbContextOptions instance.
/// </summary>
/// <param name="tenantInfo">The tenant information to bind to the context.</param>
/// <typeparam name="TContext">The TContext implementation type.</typeparam>
/// <typeparam name="TTenantInfo">The ITenantInfo implementation type.</typeparam>
/// <returns></returns>
public static TContext Create<TContext, TTenantInfo>(TTenantInfo? tenantInfo)
where TContext : DbContext
where TTenantInfo : class, ITenantInfo, new()
=> Create<TContext, TTenantInfo>(tenantInfo, null);

/// <summary>
/// Creates a new instance of a multitenant context that accepts a IMultiTenantContextAccessor instance and an optional DbContextOptions instance.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright Finbuckle LLC, Andrew White, and Contributors.
// Refer to the solution LICENSE file for more information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Finbuckle.MultiTenant.Abstractions;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
Expand All @@ -24,20 +21,10 @@ public MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantConte
{
}

/// <inheritdoc />
public MultiTenantIdentityDbContext(ITenantInfo tenantInfo) : base(tenantInfo)
{
}

/// <inheritdoc />
public MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options)
{
}

/// <inheritdoc />
public MultiTenantIdentityDbContext(ITenantInfo tenantInfo, DbContextOptions options) : base(tenantInfo, options)
{
}

/// <inheritdoc />
protected override void OnModelCreating(ModelBuilder builder)
Expand All @@ -63,20 +50,10 @@ protected MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantCo
{
}

/// <inheritdoc />
protected MultiTenantIdentityDbContext(ITenantInfo tenantInfo) : base(tenantInfo)
{
}

/// <inheritdoc />
protected MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options)
{
}

/// <inheritdoc />
protected MultiTenantIdentityDbContext(ITenantInfo tenantInfo, DbContextOptions options) : base(tenantInfo, options)
{
}

/// <inheritdoc />
protected override void OnModelCreating(ModelBuilder builder)
Expand Down Expand Up @@ -104,20 +81,10 @@ protected MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantCo
{
}

/// <inheritdoc />
protected MultiTenantIdentityDbContext(ITenantInfo tenantInfo) : base(tenantInfo)
{
}

/// <inheritdoc />
protected MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options)
{
}

/// <inheritdoc />
protected MultiTenantIdentityDbContext(ITenantInfo tenantInfo, DbContextOptions options) : base(tenantInfo, options)
{
}

/// <inheritdoc />
protected override void OnModelCreating(ModelBuilder builder)
Expand Down Expand Up @@ -164,11 +131,6 @@ protected MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantCo
TenantInfo = multiTenantContextAccessor.MultiTenantContext.TenantInfo;
}

protected MultiTenantIdentityDbContext(ITenantInfo tenantInfo)
{
TenantInfo = tenantInfo;
}

/// <summary>
/// Constructs the database context instance and binds to the current tenant.
/// </summary>
Expand All @@ -179,11 +141,6 @@ protected MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantCo
TenantInfo = multiTenantContextAccessor.MultiTenantContext.TenantInfo;
}

protected MultiTenantIdentityDbContext(ITenantInfo tenantInfo, DbContextOptions options) : base(options)
{
TenantInfo = tenantInfo;
}

/// <inheritdoc />
protected override void OnModelCreating(ModelBuilder builder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ namespace Finbuckle.MultiTenant.EntityFrameworkCore.Test.Extensions.EntityTypeBu

public class TestIdentityDbContext : EntityFrameworkCore.MultiTenantIdentityDbContext
{
public TestIdentityDbContext(ITenantInfo tenantInfo) : base(tenantInfo)
public TestIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor) : base(multiTenantContextAccessor)
{
}
}

public TestIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void WorkWithCreateNoOptions()
}

[Fact]
public void CreateArbitraryDbContext()
public void CreateMultiTenantIdentityDbContext()
{
var tenant1 = new TenantInfo
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright Finbuckle LLC, Andrew White, and Contributors.
// Refer to the solution LICENSE file for more information.

using System;
using System.Linq;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
Expand All @@ -18,11 +16,11 @@ public void WorkWithDependencyInjection()
services.AddMultiTenant<TenantInfo>();
services.AddDbContext<TestIdentityDbContext>();
var scope = services.BuildServiceProvider().CreateScope();

var context = scope.ServiceProvider.GetService<TestIdentityDbContext>();
Assert.NotNull(context);
}

[Fact]
public void WorkWithSingleParamCtor()
{
Expand All @@ -32,7 +30,7 @@ public void WorkWithSingleParamCtor()
Identifier = "abc",
Name = "abc"
};
using var c = new TestIdentityDbContext(tenant1);
using var c = EntityFrameworkCore.MultiTenantDbContext.Create<TestIdentityDbContext, TenantInfo>(tenant1);

Assert.NotNull(c);
}
Expand All @@ -53,15 +51,15 @@ public void AdjustUniqueIndexes(Type entityType)
Identifier = "abc",
Name = "abc"
};
using var c = new TestIdentityDbContext(tenant1);
using var c = EntityFrameworkCore.MultiTenantDbContext.Create<TestIdentityDbContext, TenantInfo>(tenant1);

foreach (var index in c.Model.FindEntityType(entityType)!.GetIndexes().Where(i => i.IsUnique))
{
var props = index.Properties.Select(p => p.Name);
Assert.Contains("TenantId", props);
}
}

[Theory]
// [InlineData(typeof(IdentityUser))]
// [InlineData(typeof(IdentityRole))]
Expand All @@ -78,7 +76,7 @@ public void AdjustPrimaryKeys(Type entityType)
Identifier = "abc",
Name = "abc"
};
using var c = new TestIdentityDbContext(tenant1);
using var c = EntityFrameworkCore.MultiTenantDbContext.Create<TestIdentityDbContext, TenantInfo>(tenant1);

foreach (var key in c.Model.FindEntityType(entityType)!.GetKeys())
{
Expand All @@ -103,7 +101,7 @@ public void SetMultiTenantOnIdentityDbContextVariant_None(Type entityType, bool
Identifier = "abc",
Name = "abc"
};
using var c = new TestIdentityDbContext(tenant1);
using var c = EntityFrameworkCore.MultiTenantDbContext.Create<TestIdentityDbContext, TenantInfo>(tenant1);

Assert.Equal(isMultiTenant, c.Model.FindEntityType(entityType).IsMultiTenant());
}
Expand All @@ -124,7 +122,7 @@ public void SetMultiTenantOnIdentityDbContextVariant_TUser(Type entityType, bool
Identifier = "abc",
Name = "abc"
};
using var c = new TestIdentityDbContextTUser(tenant1);
using var c = EntityFrameworkCore.MultiTenantDbContext.Create<TestIdentityDbContextTUser, TenantInfo>(tenant1);

Assert.Equal(isMultiTenant, c.Model.FindEntityType(entityType).IsMultiTenant());
}
Expand All @@ -145,7 +143,8 @@ public void SetMultiTenantOnIdentityDbContextVariant_TUser_TRole(Type entityType
Identifier = "abc",
Name = "abc"
};
using var c = new TestIdentityDbContextTUserTRole(tenant1);
using var c =
EntityFrameworkCore.MultiTenantDbContext.Create<TestIdentityDbContextTUserTRole, TenantInfo>(tenant1);

Assert.Equal(isMultiTenant, c.Model.FindEntityType(entityType).IsMultiTenant());
}
Expand All @@ -166,7 +165,7 @@ public void SetMultiTenantOnIdentityDbContextVariant_All(Type entityType, bool i
Identifier = "abc",
Name = "abc"
};
using var c = new TestIdentityDbContextAll(tenant1);
using var c = EntityFrameworkCore.MultiTenantDbContext.Create<TestIdentityDbContextAll, TenantInfo>(tenant1);

Assert.Equal(isMultiTenant, c.Model.FindEntityType(entityType).IsMultiTenant());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,15 @@ namespace Finbuckle.MultiTenant.EntityFrameworkCore.Test.MultiTenantIdentityDbCo

public class TestIdentityDbContext : EntityFrameworkCore.MultiTenantIdentityDbContext
{
public TestIdentityDbContext(TenantInfo tenantInfo)
: base(tenantInfo)
{
}

public TestIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor) : base(multiTenantContextAccessor)
{
}

public TestIdentityDbContext(ITenantInfo tenantInfo) : base(tenantInfo)
public TestIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor) : base(
multiTenantContextAccessor)
{
}

public TestIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options)
{
}

public TestIdentityDbContext(ITenantInfo tenantInfo, DbContextOptions options) : base(tenantInfo, options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("DataSource=:memory:");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// Copyright Finbuckle LLC, Andrew White, and Contributors.
// Refer to the solution LICENSE file for more information.

using Finbuckle.MultiTenant.Abstractions;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace Finbuckle.MultiTenant.EntityFrameworkCore.Test.MultiTenantIdentityDbContext;

public class TestIdentityDbContextAll : MultiTenantIdentityDbContext<IdentityUser, IdentityRole, string, IdentityUserClaim<string>, IdentityUserRole<string>, IdentityUserLogin<string>, IdentityRoleClaim<string>, IdentityUserToken<string>>
{
public TestIdentityDbContextAll(IMultiTenantContextAccessor multiTenantContextAccessor) : base(multiTenantContextAccessor)
{
}

public TestIdentityDbContextAll(TenantInfo tenantInfo)
: base(tenantInfo)
public TestIdentityDbContextAll(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options)
{
}
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
// Copyright Finbuckle LLC, Andrew White, and Contributors.
// Refer to the solution LICENSE file for more information.

using Finbuckle.MultiTenant.Abstractions;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace Finbuckle.MultiTenant.EntityFrameworkCore.Test.MultiTenantIdentityDbContext;

public class TestIdentityDbContextTUser : MultiTenantIdentityDbContext<IdentityUser>
{
public TestIdentityDbContextTUser(TenantInfo tenantInfo)
: base(tenantInfo)
public TestIdentityDbContextTUser(IMultiTenantContextAccessor multiTenantContextAccessor) : base(multiTenantContextAccessor)
{
}
}

public TestIdentityDbContextTUser(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
// Copyright Finbuckle LLC, Andrew White, and Contributors.
// Refer to the solution LICENSE file for more information.

using Finbuckle.MultiTenant.Abstractions;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace Finbuckle.MultiTenant.EntityFrameworkCore.Test.MultiTenantIdentityDbContext;

public class TestIdentityDbContextTUserTRole : MultiTenantIdentityDbContext<IdentityUser, IdentityRole, string>
{
public TestIdentityDbContextTUserTRole(TenantInfo tenantInfo)
: base(tenantInfo)
public TestIdentityDbContextTUserTRole(IMultiTenantContextAccessor multiTenantContextAccessor) : base(multiTenantContextAccessor)
{
}
}

public TestIdentityDbContextTUserTRole(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options)
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
Expand Down

0 comments on commit 2fc065f

Please sign in to comment.