public void ClearTenants_ConfigureAfterClearing() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1", }; var builder = new ContainerBuilder(); builder.RegisterType <StubDependency1Impl1>().AsImplementedInterfaces(); var mtc = new MultitenantContainer(strategy, builder.Build()); mtc.ConfigureTenant("tenant1", b => b.RegisterType <StubDependency1Impl2>().AsImplementedInterfaces()); mtc.ConfigureTenant("tenant2", b => b.RegisterType <StubDependency1Impl3>().AsImplementedInterfaces()); mtc.ConfigureTenant("tenant3", b => b.RegisterType <StubDependency1Impl3>().AsImplementedInterfaces()); mtc.ClearTenants(); mtc.ConfigureTenant("tenant1", b => b.RegisterType <StubDependency1Impl3>().AsImplementedInterfaces()); mtc.ConfigureTenant("tenant2", b => b.RegisterType <StubDependency1Impl2>().AsImplementedInterfaces()); mtc.ConfigureTenant("tenant3", b => b.RegisterType <StubDependency1Impl2>().AsImplementedInterfaces()); Assert.IsType <StubDependency1Impl3>(mtc.Resolve <IStubDependency1>()); strategy.TenantId = "tenant2"; Assert.IsType <StubDependency1Impl2>(mtc.Resolve <IStubDependency1>()); strategy.TenantId = "tenant3"; Assert.IsType <StubDependency1Impl2>(mtc.Resolve <IStubDependency1>()); }
public void Resolve_TenantLevelSingleton() { var builder = new ContainerBuilder(); builder.RegisterType <StubDependency1Impl1>().As <IStubDependency1>().SingleInstance(); var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1" }; var mtc = new MultitenantContainer(strategy, builder.Build()); mtc.ConfigureTenant("tenant1", b => b.RegisterType <StubDependency1Impl2>().As <IStubDependency1>().SingleInstance()); mtc.ConfigureTenant("tenant2", b => b.RegisterType <StubDependency1Impl2>().As <IStubDependency1>().SingleInstance()); // Get the application-level dependency var appLevel = mtc.ApplicationContainer.Resolve <IStubDependency1>(); // Two resolutions for a single tenant var dep1 = mtc.Resolve <IStubDependency1>(); var dep2 = mtc.Resolve <IStubDependency1>(); // One resolution for a different tenant strategy.TenantId = "tenant2"; var dep3 = mtc.Resolve <IStubDependency1>(); Assert.IsInstanceOf <StubDependency1Impl2>(dep1, "Tenant 1's dependency should be the override value."); Assert.IsInstanceOf <StubDependency1Impl2>(dep3, "Tenant 2's dependency should be the override value."); Assert.IsInstanceOf <StubDependency1Impl1>(appLevel, "The application's dependency should be the base value."); Assert.AreSame(dep1, dep2, "The two dependencies resolved for the first tenant should be the same."); Assert.AreNotSame(dep1, dep3, "The dependencies resolved across tenants should not be the same."); Assert.AreNotSame(dep1, appLevel, "The dependencies resolved at the tenant level should not be the same as the ones at the application level."); }
public void ClearTenants_ConfigureAfterClearingFailCaseDemo() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1", }; var builder = new ContainerBuilder(); builder.RegisterType <StubDependency1Impl1>().AsImplementedInterfaces(); var mtc = new MultitenantContainer(strategy, builder.Build()); mtc.ConfigureTenant("tenant1", b => b.RegisterType <StubDependency1Impl2>().AsImplementedInterfaces()); mtc.ConfigureTenant("tenant2", b => b.RegisterType <StubDependency1Impl3>().AsImplementedInterfaces()); mtc.ConfigureTenant("tenant3", b => b.RegisterType <StubDependency1Impl3>().AsImplementedInterfaces()); mtc.ClearTenants(); // contextual tenant is still "tenant1"; this will force creation of container-configured tenant mtc.Resolve <IStubDependency1>(); Assert.Throws <InvalidOperationException>(() => mtc.ConfigureTenant("tenant1", b => b.RegisterType <StubDependency1Impl3>().AsImplementedInterfaces())); // contextual tenant is still "tenant2"; this will force creation of container-configured tenant strategy.TenantId = "tenant2"; mtc.Resolve <IStubDependency1>(); Assert.Throws <InvalidOperationException>(() => mtc.ConfigureTenant("tenant2", b => b.RegisterType <StubDependency1Impl2>().AsImplementedInterfaces())); // contextual tenant is still "tenant3"; this will force creation of container-configured tenant strategy.TenantId = "tenant3"; mtc.Resolve <IStubDependency1>(); Assert.Throws <InvalidOperationException>(() => mtc.ConfigureTenant("tenant3", b => b.RegisterType <StubDependency1Impl2>().AsImplementedInterfaces())); }
public void ClearTenants_TenantSingleton() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1", }; var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build()); mtc.ConfigureTenant("tenant1", b => b.RegisterType <StubDisposableDependency>().SingleInstance()); mtc.ConfigureTenant("tenant2", b => b.RegisterType <StubDisposableDependency>().SingleInstance()); mtc.ConfigureTenant("tenant3", b => b.RegisterType <StubDisposableDependency>().SingleInstance()); var registeredTenants = mtc.GetTenants(); var tenantsStubs = new List <StubDisposableDependency>(); foreach (var tenantId in registeredTenants) { strategy.TenantId = tenantId; tenantsStubs.Add(mtc.Resolve <StubDisposableDependency>()); } mtc.ClearTenants(); foreach (var tenantStub in tenantsStubs) { Assert.True(tenantStub.IsDisposed); } }
public void ClearTenants_ShowFallback() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1", }; var builder = new ContainerBuilder(); builder.RegisterType <StubDependency1Impl1>().AsImplementedInterfaces(); var mtc = new MultitenantContainer(strategy, builder.Build()); mtc.ConfigureTenant("tenant1", b => b.RegisterType <StubDependency1Impl2>().AsImplementedInterfaces()); mtc.ConfigureTenant("tenant2", b => b.RegisterType <StubDependency1Impl3>().AsImplementedInterfaces()); mtc.ConfigureTenant("tenant3", b => b.RegisterType <StubDependency1Impl3>().AsImplementedInterfaces()); var registeredTenants = mtc.GetTenants(); mtc.ClearTenants(); foreach (var tenantId in registeredTenants) { strategy.TenantId = tenantId; Assert.IsType <StubDependency1Impl1>(mtc.Resolve <IStubDependency1>()); } }
public void Resolve_TenantLevelSingleton() { var builder = new ContainerBuilder(); builder.RegisterType <StubDependency1Impl1>().As <IStubDependency1>().SingleInstance(); var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1" }; var mtc = new MultitenantContainer(strategy, builder.Build()); mtc.ConfigureTenant("tenant1", b => b.RegisterType <StubDependency1Impl2>().As <IStubDependency1>().SingleInstance()); mtc.ConfigureTenant("tenant2", b => b.RegisterType <StubDependency1Impl2>().As <IStubDependency1>().SingleInstance()); // Get the application-level dependency var appLevel = mtc.ApplicationContainer.Resolve <IStubDependency1>(); // Two resolutions for a single tenant var dep1 = mtc.Resolve <IStubDependency1>(); var dep2 = mtc.Resolve <IStubDependency1>(); // One resolution for a different tenant strategy.TenantId = "tenant2"; var dep3 = mtc.Resolve <IStubDependency1>(); Assert.IsType <StubDependency1Impl2>(dep1); Assert.IsType <StubDependency1Impl2>(dep3); Assert.IsType <StubDependency1Impl1>(appLevel); Assert.Same(dep1, dep2); Assert.NotSame(dep1, dep3); Assert.NotSame(dep1, appLevel); }
public void ClearTenants_ShowDisposal() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1", }; var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build()); mtc.ConfigureTenant("tenant1", b => b.RegisterType <StubDependency1Impl1>().AsImplementedInterfaces()); mtc.ConfigureTenant("tenant2", b => b.RegisterType <StubDependency1Impl2>().AsImplementedInterfaces()); mtc.ConfigureTenant("tenant3", b => b.RegisterType <StubDependency1Impl3>().AsImplementedInterfaces()); var registeredTenants = mtc.GetTenants(); var tenantScopes = new List <ILifetimeScope>(); foreach (var tenantId in registeredTenants) { tenantScopes.Add(mtc.GetTenantScope(tenantId)); } mtc.ClearTenants(); foreach (var tenantScope in tenantScopes) { Assert.Throws <ObjectDisposedException>(() => tenantScope.Resolve <IStubDependency1>()); } }
public void GetService_ResolvesTenantSpecificRegistrations() { var services = new ServiceCollection(); services.AddSingleton <IStubDependency1, StubDependency1Impl1>(); var builder = new ContainerBuilder(); builder.Populate(services); var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1", }; var mtc = new MultitenantContainer(strategy, builder.Build()); mtc.ConfigureTenant("tenant1", b => b.RegisterType <StubDependency1Impl2>().As <IStubDependency1>()); mtc.ConfigureTenant("tenant2", b => b.RegisterType <StubDependency1Impl3>().As <IStubDependency1>()); var serviceProvider = new AutofacServiceProvider(mtc); Assert.IsType <StubDependency1Impl2>(serviceProvider.GetService <IStubDependency1>()); strategy.TenantId = "tenant2"; Assert.IsType <StubDependency1Impl3>(serviceProvider.GetService <IStubDependency1>()); }
public void GetService_ApplicationLevelSingleton() { var services = new ServiceCollection(); services.AddSingleton <IStubDependency1, StubDependency1Impl1>(); var builder = new ContainerBuilder(); builder.Populate(services); var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1", }; var mtc = new MultitenantContainer(strategy, builder.Build()); var serviceProvider = new AutofacServiceProvider(mtc); // Two resolutions for a single tenant var dep1 = serviceProvider.GetService <IStubDependency1>(); var dep2 = serviceProvider.GetService <IStubDependency1>(); // One resolution for a different tenant strategy.TenantId = "tenant2"; var dep3 = serviceProvider.GetService <IStubDependency1>(); Assert.Same(dep1, dep2); Assert.Same(dep1, dep3); }
public void InstancePerTenant_RootAndPerTenantDependencies() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1" }; var builder = new ContainerBuilder(); builder.RegisterType <StubDependency3Impl>().As <IStubDependency3>().InstancePerTenant(); var mtc = new MultitenantContainer(strategy, builder.Build()); mtc.ConfigureTenant("tenant1", b => b.RegisterType <StubDependency1Impl1>().As <IStubDependency1>().InstancePerTenant()); mtc.ConfigureTenant("tenant2", b => b.RegisterType <StubDependency1Impl1>().As <IStubDependency1>().InstancePerTenant()); // Two resolutions for a single tenant var dep1 = mtc.Resolve <IStubDependency3>(); var dep2 = mtc.Resolve <IStubDependency3>(); // One resolution for a different tenant strategy.TenantId = "tenant2"; var dep3 = mtc.Resolve <IStubDependency3>(); Assert.Same(dep1, dep2); Assert.NotSame(dep1, dep3); Assert.Same(dep1.Dependency, dep2.Dependency); Assert.NotSame(dep1.Dependency, dep3.Dependency); }
public void InstancePerTenant_RootAndPerTenantDependencies() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1" }; var builder = new ContainerBuilder(); builder.RegisterType <StubDependency3Impl>().As <IStubDependency3>().InstancePerTenant(); var mtc = new MultitenantContainer(strategy, builder.Build()); mtc.ConfigureTenant("tenant1", b => b.RegisterType <StubDependency1Impl1>().As <IStubDependency1>().InstancePerTenant()); mtc.ConfigureTenant("tenant2", b => b.RegisterType <StubDependency1Impl1>().As <IStubDependency1>().InstancePerTenant()); // Two resolutions for a single tenant var dep1 = mtc.Resolve <IStubDependency3>(); var dep2 = mtc.Resolve <IStubDependency3>(); // One resolution for a different tenant strategy.TenantId = "tenant2"; var dep3 = mtc.Resolve <IStubDependency3>(); Assert.AreSame(dep1, dep2, "The two dependencies resolved for the first tenant should be the same."); Assert.AreNotSame(dep1, dep3, "The dependencies resolved across tenants should not be the same."); Assert.AreSame(dep1.Dependency, dep2.Dependency, "The two sub-dependencies resolved for the first tenant should be the same."); Assert.AreNotSame(dep1.Dependency, dep3.Dependency, "The sub-dependencies resolved across tenants should not be the same."); }
public void Ctor_SetsProperties() { var container = new ContainerBuilder().Build(); var strategy = new StubTenantIdentificationStrategy(); var mtc = new MultitenantContainer(strategy, container); Assert.Same(container, mtc.ApplicationContainer); Assert.Same(strategy, mtc.TenantIdentificationStrategy); }
public void IdentifyTenant_FailedRetrieval() { var strategy = new StubTenantIdentificationStrategy { IdentificationSuccess = false }; Assert.AreEqual(Guid.Empty, strategy.IdentifyTenant <Guid>(), "The tenant ID should be the default for the type if identification fails."); }
public void IdentifyTenant_FailedConversion() { var strategy = new StubTenantIdentificationStrategy { TenantId = Guid.NewGuid() }; Assert.Throws <InvalidCastException>(() => strategy.IdentifyTenant <int>()); }
public void IdentifyTenant_FailedRetrieval() { var strategy = new StubTenantIdentificationStrategy { IdentificationSuccess = false }; Assert.Equal(Guid.Empty, strategy.IdentifyTenant <Guid>()); }
public void Ctor_SetsProperties() { var container = new ContainerBuilder().Build(); var strategy = new StubTenantIdentificationStrategy(); var mtc = new MultitenantContainer(strategy, container); Assert.AreSame(container, mtc.ApplicationContainer, "The application container wasn't set."); Assert.AreSame(strategy, mtc.TenantIdentificationStrategy, "The tenant ID strategy wasn't set."); }
public void TenantIsConfigured_NotConfigured() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1", }; var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build()); Assert.False(mtc.TenantIsConfigured("tenant1")); }
public void IdentifyTenant_SuccessfulRetrieval() { var expected = Guid.NewGuid(); var strategy = new StubTenantIdentificationStrategy { TenantId = expected }; Assert.AreEqual(expected, strategy.IdentifyTenant <Guid>(), "The tenant ID wasn't properly retrieved and parsed."); }
public void IdentifyTenant_SuccessfulRetrieval() { var expected = Guid.NewGuid(); var strategy = new StubTenantIdentificationStrategy { TenantId = expected }; Assert.Equal(expected, strategy.IdentifyTenant <Guid>()); }
public void ComponentRegistry_ReturnsRegistryFromCurrentTenantLifetimeScope() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1" }; var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build()); var scope = mtc.GetCurrentTenantScope(); Assert.Same(scope.ComponentRegistry, mtc.ComponentRegistry); }
public void ConfigureTenant_RequiresConfiguration() { var builder = new ContainerBuilder(); var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1" }; var mtc = new MultitenantContainer(strategy, builder.Build()); Assert.Throws <ArgumentNullException>(() => mtc.ConfigureTenant("tenant1", null)); }
public void MultitenantContainer_DiagnosticSourceNotNull() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1", }; var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build()); Assert.NotNull(mtc.DiagnosticSource); }
public void ComponentRegistry_ReturnsRegistryFromCurrentTenantLifetimeScope() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1" }; var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build()); var scope = mtc.GetCurrentTenantScope(); Assert.AreSame(scope.ComponentRegistry, mtc.ComponentRegistry, "The ComponentRegistry property should behave based on context."); }
public void TenantIsConfigured_DefaultConfigures() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1", }; var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build()); mtc.GetTenantScope("tenant1"); Assert.True(mtc.TenantIsConfigured("tenant1")); }
public async Task MultitenantContainer_AsyncDispose() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1", }; var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build()); mtc.ConfigureTenant("tenant1", b => b.RegisterType <StubDependency1Impl1>().As <IStubDependency1>()); await mtc.DisposeAsync(); }
public void GetTenantScope_NullIsDefaultTenant() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1", }; var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build()); var scope = mtc.GetTenantScope(null); Assert.NotNull(scope); Assert.NotSame(mtc.ApplicationContainer, scope); }
public void GetCurrentTenantScope_TenantFound() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1" }; var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build()); var current = mtc.GetCurrentTenantScope(); var tenant = mtc.GetTenantScope("tenant1"); Assert.Same(tenant, current); }
public void GetTenantScope_GetsTenantScopeForUnconfiguredTenant() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1" }; var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build()); var scope = mtc.GetTenantScope("tenant1"); Assert.IsNotNull(scope, "The tenant scope retrieved not be null."); Assert.AreNotSame(mtc.ApplicationContainer, scope, "The tenant scope should be a real scope, not just the application container."); }
public void GetCurrentTenantScope_TenantFound() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1" }; var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build()); var current = mtc.GetCurrentTenantScope(); var tenant = mtc.GetTenantScope("tenant1"); Assert.AreSame(tenant, current, "The current scope should be the scope for the identified tenant."); }
public void GetTenantScope_GetsTenantScopeForUnconfiguredTenant() { var strategy = new StubTenantIdentificationStrategy() { TenantId = "tenant1" }; var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build()); var scope = mtc.GetTenantScope("tenant1"); Assert.NotNull(scope); Assert.NotSame(mtc.ApplicationContainer, scope); }