Moved to multitenancy abstractions module.

pull/191/head
Halil İbrahim Kalkan 8 years ago
parent 4099af8cc1
commit 9e8a18ceae

@ -28,6 +28,7 @@ using Volo.Abp.Ui.Navigation;
using Volo.Abp.VirtualFileSystem; using Volo.Abp.VirtualFileSystem;
using Volo.Abp.IdentityServer.Jwt; using Volo.Abp.IdentityServer.Jwt;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
using Volo.Abp.MultiTenancy.ConfigurationStore;
namespace AbpDesk.Web.Mvc namespace AbpDesk.Web.Mvc
{ {
@ -72,11 +73,11 @@ namespace AbpDesk.Web.Mvc
{ {
options.Tenants = new[] options.Tenants = new[]
{ {
new Tenant( new TenantInfo(
Guid.Parse("446a5211-3d72-4339-9adc-845151f8ada0"), Guid.Parse("446a5211-3d72-4339-9adc-845151f8ada0"),
"acme" "acme"
), ),
new Tenant( new TenantInfo(
Guid.Parse("25388015-ef1c-4355-9c18-f6b6ddbaf89d"), Guid.Parse("25388015-ef1c-4355-9c18-f6b6ddbaf89d"),
"volosoft" "volosoft"
) )

@ -1,15 +1,17 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
namespace Volo.Abp.Data namespace Volo.Abp.Data
{ {
[Serializable]
public class ConnectionStrings : Dictionary<string, string> public class ConnectionStrings : Dictionary<string, string>
{ {
public const string DefaultConnectionStringName = "Default"; public const string DefaultConnectionStringName = "Default";
public string Default public string Default
{ {
get { return this.GetOrDefault(DefaultConnectionStringName); } get => this.GetOrDefault(DefaultConnectionStringName);
set { this[DefaultConnectionStringName] = value; } set => this[DefaultConnectionStringName] = value;
} }
} }
} }

@ -15,6 +15,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" /> <ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" />
<ProjectReference Include="..\Volo.Abp.Data\Volo.Abp.Data.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -1,8 +1,10 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Data;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
namespace Volo.Abp.MultiTenancy namespace Volo.Abp.MultiTenancy
{ {
[DependsOn(typeof(AbpDataModule))]
public class AbpMultiTenancyAbstractionsModule : AbpModule public class AbpMultiTenancyAbstractionsModule : AbpModule
{ {
public override void ConfigureServices(IServiceCollection services) public override void ConfigureServices(IServiceCollection services)

@ -19,7 +19,7 @@ namespace Volo.Abp.MultiTenancy
_currentScope = new AsyncLocal<TenantScope>(); _currentScope = new AsyncLocal<TenantScope>();
} }
public IDisposable EnterScope(Tenant tenant) public IDisposable EnterScope(TenantInfo tenant)
{ {
var parentScope = CurrentScope; var parentScope = CurrentScope;
CurrentScope = new TenantScope(tenant); CurrentScope = new TenantScope(tenant);

@ -3,7 +3,7 @@ using System.Linq;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
namespace Volo.Abp.MultiTenancy namespace Volo.Abp.MultiTenancy.ConfigurationStore
{ {
//TODO: Move to another package. //TODO: Move to another package.
[Dependency(TryRegister = true)] [Dependency(TryRegister = true)]
@ -16,12 +16,12 @@ namespace Volo.Abp.MultiTenancy
_options = options.Value; _options = options.Value;
} }
public Tenant Find(string name) public TenantInfo Find(string name)
{ {
return _options.Tenants.FirstOrDefault(t => t.Name == name); return _options.Tenants.FirstOrDefault(t => t.Name == name);
} }
public Tenant Find(Guid id) public TenantInfo Find(Guid id)
{ {
return _options.Tenants.FirstOrDefault(t => t.Id == id); return _options.Tenants.FirstOrDefault(t => t.Id == id);
} }

@ -0,0 +1,12 @@
namespace Volo.Abp.MultiTenancy.ConfigurationStore
{
public class ConfigurationTenantStoreOptions
{
public TenantInfo[] Tenants { get; set; }
public ConfigurationTenantStoreOptions()
{
Tenants = new TenantInfo[0];
}
}
}

@ -6,7 +6,7 @@ namespace Volo.Abp.MultiTenancy
public interface IMultiTenancyManager public interface IMultiTenancyManager
{ {
[CanBeNull] [CanBeNull]
Tenant CurrentTenant { get; } TenantInfo CurrentTenant { get; }
IDisposable ChangeTenant(Guid? tenantId); IDisposable ChangeTenant(Guid? tenantId);

@ -11,6 +11,6 @@ namespace Volo.Abp.MultiTenancy
[CanBeNull] [CanBeNull]
TenantScope CurrentScope { get; } TenantScope CurrentScope { get; }
IDisposable EnterScope([CanBeNull] Tenant tenant); IDisposable EnterScope([CanBeNull] TenantInfo tenant);
} }
} }

@ -6,9 +6,9 @@ namespace Volo.Abp.MultiTenancy
public interface ITenantStore public interface ITenantStore
{ {
[CanBeNull] [CanBeNull]
Tenant Find(string name); TenantInfo Find(string name);
[CanBeNull] [CanBeNull]
Tenant Find(Guid id); TenantInfo Find(Guid id);
} }
} }

@ -8,7 +8,7 @@ namespace Volo.Abp.MultiTenancy
public class MultiTenancyManager : IMultiTenancyManager, ITransientDependency public class MultiTenancyManager : IMultiTenancyManager, ITransientDependency
{ {
public Tenant CurrentTenant => GetCurrentTenant(); public TenantInfo CurrentTenant => GetCurrentTenant();
private readonly ITenantScopeProvider _tenantScopeProvider; private readonly ITenantScopeProvider _tenantScopeProvider;
private readonly ITenantStore _tenantStore; private readonly ITenantStore _tenantStore;
@ -59,7 +59,7 @@ namespace Volo.Abp.MultiTenancy
return _tenantScopeProvider.EnterScope(tenant); return _tenantScopeProvider.EnterScope(tenant);
} }
protected virtual Tenant GetCurrentTenant() protected virtual TenantInfo GetCurrentTenant()
{ {
if (_tenantScopeProvider.CurrentScope != null) if (_tenantScopeProvider.CurrentScope != null)
{ {
@ -71,7 +71,7 @@ namespace Volo.Abp.MultiTenancy
return ResolveTenant(); return ResolveTenant();
} }
protected virtual Tenant ResolveTenant() protected virtual TenantInfo ResolveTenant()
{ {
var tenantIdOrName = _tenantResolver.ResolveTenantIdOrName(); var tenantIdOrName = _tenantResolver.ResolveTenantIdOrName();
if (tenantIdOrName == null) if (tenantIdOrName == null)
@ -79,7 +79,7 @@ namespace Volo.Abp.MultiTenancy
return null; return null;
} }
Tenant tenant; TenantInfo tenant;
//Try to find by id //Try to find by id
if (Guid.TryParse(tenantIdOrName, out var tenantId)) if (Guid.TryParse(tenantIdOrName, out var tenantId))

@ -31,12 +31,12 @@ namespace Volo.Abp.MultiTenancy
//Requesting default connection string //Requesting default connection string
if (connectionStringName == null) if (connectionStringName == null)
{ {
return tenant.FindDefaultConnectionString() ?? return tenant.ConnectionStrings.Default ??
Options.ConnectionStrings.Default; Options.ConnectionStrings.Default;
} }
//Requesting specific connection string //Requesting specific connection string
var connString = tenant.FindConnectionString(connectionStringName); var connString = tenant.ConnectionStrings.GetOrDefault(connectionStringName);
if (connString != null) if (connString != null)
{ {
return connString; return connString;
@ -53,7 +53,7 @@ namespace Volo.Abp.MultiTenancy
return connStringInOptions; return connStringInOptions;
} }
return tenant.FindDefaultConnectionString() ?? return tenant.ConnectionStrings.Default ??
Options.ConnectionStrings.Default; Options.ConnectionStrings.Default;
} }
} }

@ -0,0 +1,31 @@
using System;
using JetBrains.Annotations;
using Volo.Abp.Data;
namespace Volo.Abp.MultiTenancy
{
[Serializable]
public class TenantInfo
{
public Guid Id { get; }
public string Name { get; }
public ConnectionStrings ConnectionStrings { get; }
private TenantInfo()
{
}
public TenantInfo(Guid id, [NotNull] string name)
{
Check.NotNull(name, nameof(name));
Id = id;
Name = name;
ConnectionStrings = new ConnectionStrings();
}
}
}

@ -9,9 +9,9 @@ namespace Volo.Abp.MultiTenancy
/// Not null value for a tenant. /// Not null value for a tenant.
/// </summary> /// </summary>
[CanBeNull] [CanBeNull]
public Tenant Tenant { get; } public TenantInfo Tenant { get; }
public TenantScope([CanBeNull] Tenant tenant) public TenantScope([CanBeNull] TenantInfo tenant)
{ {
Tenant = tenant; Tenant = tenant;
} }

@ -1,12 +0,0 @@
namespace Volo.Abp.MultiTenancy
{
public class ConfigurationTenantStoreOptions
{
public Tenant[] Tenants { get; set; }
public ConfigurationTenantStoreOptions()
{
Tenants = new Tenant[0];
}
}
}

@ -4,7 +4,7 @@ using Volo.Abp.Domain.Entities;
namespace Volo.Abp.MultiTenancy namespace Volo.Abp.MultiTenancy
{ {
public class TenantConnectionString : Entity public class TenantConnectionString : Entity //TODO: This should be a value object!
{ {
public virtual Guid TenantId { get; protected set; } public virtual Guid TenantId { get; protected set; }

@ -7,6 +7,7 @@ using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
using Shouldly; using Shouldly;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
using Volo.Abp.MultiTenancy.ConfigurationStore;
using Xunit; using Xunit;
namespace Volo.Abp.AspNetCore.MultiTenancy namespace Volo.Abp.AspNetCore.MultiTenancy
@ -31,7 +32,7 @@ namespace Volo.Abp.AspNetCore.MultiTenancy
{ {
options.Tenants = new[] options.Tenants = new[]
{ {
new Tenant(_testTenantId, _testTenantName) new TenantInfo(_testTenantId, _testTenantName)
}; };
}); });
}); });

@ -1,8 +1,8 @@
using System; using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Shouldly; using Shouldly;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
using Volo.Abp.MultiTenancy.ConfigurationStore;
using Xunit; using Xunit;
namespace Volo.Abp.Data.MultiTenancy namespace Volo.Abp.Data.MultiTenancy
@ -30,18 +30,17 @@ namespace Volo.Abp.Data.MultiTenancy
services.Configure<ConfigurationTenantStoreOptions>(options => services.Configure<ConfigurationTenantStoreOptions>(options =>
{ {
var tenant1Id = Guid.NewGuid();
options.Tenants = new[] options.Tenants = new[]
{ {
new Tenant(tenant1Id, "tenant1") new TenantInfo(Guid.NewGuid(), "tenant1")
{ {
ConnectionStrings = ConnectionStrings =
{ {
new TenantConnectionString(tenant1Id,ConnectionStrings.DefaultConnectionStringName, "tenant1-default-value"), { ConnectionStrings.DefaultConnectionStringName, "tenant1-default-value"},
new TenantConnectionString(tenant1Id,"db1", "tenant1-db1-value") {"db1", "tenant1-db1-value"}
} }
}, },
new Tenant(Guid.NewGuid(), "tenant2") new TenantInfo(Guid.NewGuid(), "tenant2")
}; };
}); });
} }

@ -1,6 +1,7 @@
using System; using System;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Shouldly; using Shouldly;
using Volo.Abp.MultiTenancy.ConfigurationStore;
using Xunit; using Xunit;
namespace Volo.Abp.MultiTenancy namespace Volo.Abp.MultiTenancy
@ -32,8 +33,8 @@ namespace Volo.Abp.MultiTenancy
{ {
options.Tenants = new[] options.Tenants = new[]
{ {
new Tenant(Guid.NewGuid(), _tenantA), new TenantInfo(Guid.NewGuid(), _tenantA),
new Tenant(Guid.NewGuid(), _tenantB) new TenantInfo(Guid.NewGuid(), _tenantB)
}; };
}); });
} }

Loading…
Cancel
Save