mirror of https://github.com/abpframework/abp.git
10 changed files with 183 additions and 26 deletions
@ -0,0 +1,9 @@ |
|||||
|
namespace Volo.Abp.MultiTenancy |
||||
|
{ |
||||
|
public class CurrentTenantResolveContext : ICurrentTenantResolveContext |
||||
|
{ |
||||
|
public ITenantInfo Tenant { get; set; } |
||||
|
|
||||
|
public bool Handled { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
namespace Volo.Abp.MultiTenancy |
||||
|
{ |
||||
|
public interface ICurrentTenantResolveContext |
||||
|
{ |
||||
|
ITenantInfo Tenant { get; set; } |
||||
|
|
||||
|
bool Handled { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
namespace Volo.Abp.MultiTenancy |
||||
|
{ |
||||
|
public interface IMultiTenancyManager |
||||
|
{ |
||||
|
ITenantInfo CurrentTenant { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
namespace Volo.Abp.MultiTenancy |
||||
|
{ |
||||
|
public interface ITenantInfo |
||||
|
{ |
||||
|
string Id { get; } |
||||
|
|
||||
|
string Name { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
namespace Volo.Abp.MultiTenancy |
||||
|
{ |
||||
|
public interface ITenantResolver |
||||
|
{ |
||||
|
void Resolve(ICurrentTenantResolveContext context); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.MultiTenancy |
||||
|
{ |
||||
|
public class TenantInfo : ITenantInfo |
||||
|
{ |
||||
|
public string Id { get; } |
||||
|
|
||||
|
public string Name { get; } |
||||
|
|
||||
|
private TenantInfo() |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public TenantInfo([NotNull] string id, [NotNull] string name) |
||||
|
{ |
||||
|
Check.NotNull(id, nameof(id)); |
||||
|
Check.NotNull(name, nameof(name)); |
||||
|
|
||||
|
Id = id; |
||||
|
Name = name; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
using System; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.MultiTenancy |
||||
|
{ |
||||
|
public class TenantResolverAction : ITenantResolver |
||||
|
{ |
||||
|
private readonly Action<ICurrentTenantResolveContext> _resolveAction; |
||||
|
|
||||
|
public TenantResolverAction([NotNull] Action<ICurrentTenantResolveContext> resolveAction) |
||||
|
{ |
||||
|
Check.NotNull(resolveAction, nameof(resolveAction)); |
||||
|
|
||||
|
_resolveAction = resolveAction; |
||||
|
} |
||||
|
|
||||
|
public void Resolve(ICurrentTenantResolveContext context) |
||||
|
{ |
||||
|
_resolveAction(context); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,77 @@ |
|||||
|
using System; |
||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.MultiTenancy |
||||
|
{ |
||||
|
public class MultiTenantManager_TenantResolver_Tests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Get_Current_Tenant_As_Null_If_No_Resolver() |
||||
|
{ |
||||
|
//Arrange
|
||||
|
|
||||
|
var manager = new MultiTenancyManager(new ITenantResolver[0]); |
||||
|
|
||||
|
//Act
|
||||
|
|
||||
|
manager.CurrentTenant.ShouldBeNull(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Get_Current_Tenant_From_Single_Resolver() |
||||
|
{ |
||||
|
//Arrange
|
||||
|
|
||||
|
var fakeTenant = new TenantInfo(Guid.NewGuid().ToString(), "acme"); |
||||
|
|
||||
|
//Act
|
||||
|
|
||||
|
var manager = new MultiTenancyManager(new[] |
||||
|
{ |
||||
|
new TenantResolverAction(context => |
||||
|
{ |
||||
|
context.Tenant = fakeTenant; |
||||
|
context.Handled = true; |
||||
|
}) |
||||
|
}); |
||||
|
|
||||
|
//Assert
|
||||
|
|
||||
|
manager.CurrentTenant.ShouldBe(fakeTenant); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Get_Current_Tenant_From_Two_Resolvers() |
||||
|
{ |
||||
|
//Arrange
|
||||
|
|
||||
|
var fakeTenant = new TenantInfo(Guid.NewGuid().ToString(), "acme"); |
||||
|
|
||||
|
//Act
|
||||
|
|
||||
|
var manager = new MultiTenancyManager(new[] |
||||
|
{ |
||||
|
new TenantResolverAction(context => |
||||
|
{ |
||||
|
context.Tenant = new TenantInfo(Guid.NewGuid().ToString(), "skipped-tenant"); |
||||
|
}), |
||||
|
new TenantResolverAction(context => |
||||
|
{ |
||||
|
context.Tenant = fakeTenant; |
||||
|
context.Handled = true; |
||||
|
}), |
||||
|
new TenantResolverAction(context => |
||||
|
{ |
||||
|
context.Tenant = new TenantInfo(Guid.NewGuid().ToString(), "skipped-tenant"); |
||||
|
context.Handled = true; |
||||
|
}) |
||||
|
}); |
||||
|
|
||||
|
//Assert
|
||||
|
|
||||
|
manager.CurrentTenant.ShouldBe(fakeTenant); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue