Skip to content

Commit

Permalink
Merge pull request #10490 from abpframework/auto-set-tenantid
Browse files Browse the repository at this point in the history
Automatically set TenantId while creating a new entity object.
  • Loading branch information
maliming committed Nov 2, 2021
2 parents 9e925b2 + 4265739 commit 4d9b04f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace Volo.Abp.Domain.Entities
[Serializable]
public abstract class Entity : IEntity
{
protected Entity()
{
EntityHelper.TrySetTenantId(this);
}

/// <inheritdoc/>
public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,25 @@ public static Type FindPrimaryKeyType([NotNull] Type entityType)
? new Type[] { typeof(DisableIdGenerationAttribute) }
: new Type[] { });
}

public static void TrySetTenantId(IEntity entity)
{
if (entity is not IMultiTenant multiTenantEntity)
{
return;
}

var tenantId = AsyncLocalCurrentTenantAccessor.Instance.Current?.TenantId;
if (tenantId == multiTenantEntity.TenantId)
{
return;
}

ObjectHelper.TrySetProperty(
multiTenantEntity,
x => x.TenantId,
() => tenantId
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class AbpMultiTenancyModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddSingleton<ICurrentTenantAccessor>(AsyncLocalCurrentTenantAccessor.Instance);

var configuration = context.Services.GetConfiguration();
Configure<AbpDefaultTenantStoreOptions>(configuration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

namespace Volo.Abp.MultiTenancy
{
public class AsyncLocalCurrentTenantAccessor : ICurrentTenantAccessor, ISingletonDependency
public class AsyncLocalCurrentTenantAccessor : ICurrentTenantAccessor
{
public static AsyncLocalCurrentTenantAccessor Instance { get; } = new();

public BasicTenantInfo Current
{
get => _currentScope.Value;
Expand All @@ -13,7 +15,7 @@ public BasicTenantInfo Current

private readonly AsyncLocal<BasicTenantInfo> _currentScope;

public AsyncLocalCurrentTenantAccessor()
private AsyncLocalCurrentTenantAccessor()
{
_currentScope = new AsyncLocal<BasicTenantInfo>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ public void Same_Tenants_With_Same_Keys_Considered_As_Same_Objects()
new Car(42, tenantId1).EntityEquals(new Car(42, tenantId1)).ShouldBeTrue();
}

[Fact]
public void Should_Set_TenantId_On_Object_Creation()
{
var tenantId = Guid.NewGuid();
AsyncLocalCurrentTenantAccessor.Instance.Current = new BasicTenantInfo(tenantId);
new Car().TenantId.ShouldBe(tenantId);
}

[Fact]
public void Should_Override_TenantId_Manually()
{
AsyncLocalCurrentTenantAccessor.Instance.Current = null;

var tenantId = Guid.NewGuid();
new Car(0, tenantId).TenantId.ShouldBe(tenantId);
}

public class Person : Entity<Guid>
{
public Person()
Expand All @@ -88,7 +105,7 @@ public Person(Guid id)

public class Car : Entity<int>, IMultiTenant
{
public Guid? TenantId { get; }
public Guid? TenantId { get; private set; }

public Car()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task Create_SetsTenantId()
var uniquePersonName = Guid.NewGuid().ToString();
var personDto = await _peopleAppService.CreateAsync(new PersonDto {Name = uniquePersonName});

var repository = ServiceProvider.GetService<IRepository<Person, Guid>>();
var repository = ServiceProvider.GetRequiredService<IRepository<Person, Guid>>();
var person = await repository.FindAsync(personDto.Id);

person.ShouldNotBeNull();
Expand Down

0 comments on commit 4d9b04f

Please sign in to comment.