Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable annotation fixes for consistency #41

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Autofac.Multitenant/ITenantIdentificationStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public interface ITenantIdentificationStrategy
/// <see langword="true" /> if the tenant could be identified; <see langword="false" />
/// if not.
/// </returns>
/// <remarks>
/// <para>
/// It is technically possible to allow the tenant to be identified but
/// still have the tenant ID come out as <see langword="null"/>. If this
/// happens, it indicates the strategy has intentionally chosen the "default
/// tenant" as the active tenant rather than requiring fallback logic to
/// occur.
/// </para>
/// </remarks>
[SuppressMessage("Microsoft.Design", "CA1007:UseGenericsWhereAppropriate", Justification = "Tenant identifiers are objects.")]
bool TryIdentifyTenant(out object? tenantId);
}
6 changes: 3 additions & 3 deletions src/Autofac.Multitenant/MultitenantContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ private ILifetimeScope CreateTenantScope(object tenantId, Action<ContainerBuilde
/// <seealso cref="ConfigurationActionBuilder"/>
/// <seealso cref="ConfigureTenant(object, Action{ContainerBuilder})"/>
[SuppressMessage("CA1513", "CA1513", Justification = "ObjectDisposedException.ThrowIf is not available in all target frameworks.")]
public bool ReconfigureTenant(object tenantId, Action<ContainerBuilder> configuration)
public bool ReconfigureTenant(object? tenantId, Action<ContainerBuilder> configuration)
{
if (configuration == null)
{
Expand Down Expand Up @@ -488,7 +488,7 @@ public IEnumerable<object> GetTenants()
/// </summary>
/// <param name="tenantId">The tenant ID to test.</param>
/// <returns>If configured, <c>true</c>; otherwise <c>false</c>.</returns>
public bool TenantIsConfigured(object tenantId)
public bool TenantIsConfigured(object? tenantId)
{
tenantId ??= _defaultTenantId;

Expand All @@ -500,7 +500,7 @@ public bool TenantIsConfigured(object tenantId)
/// </summary>
/// <param name="tenantId">The ID of the tenant to dispose.</param>
/// <returns><c>true</c> if the tenant-collection was modified; otherwise, <c>false</c>.</returns>
public bool RemoveTenant(object tenantId)
public bool RemoveTenant(object? tenantId)
{
tenantId ??= _defaultTenantId;

Expand Down
34 changes: 33 additions & 1 deletion test/Autofac.Multitenant.Test/MultitenantContainerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,37 @@ public void Resolve_ApplicationLevelSingleton()
Assert.Same(dep1, dep3);
}

[Fact]
public void Resolve_DefaultTenantFallbackToApplicationContainer()
{
var builder = new ContainerBuilder();
builder.RegisterType<StubDependency1Impl1>().As<IStubDependency1>();
var strategy = new StubTenantIdentificationStrategy()
{
TenantId = null,
};
using var mtc = new MultitenantContainer(strategy, builder.Build());
Assert.IsType<StubDependency1Impl1>(mtc.Resolve<IStubDependency1>());
}

[Fact]
public void Resolve_ResolvesDefaultTenantSpecificRegistrations()
{
var builder = new ContainerBuilder();
builder.RegisterType<StubDependency1Impl1>().As<IStubDependency1>();
var strategy = new StubTenantIdentificationStrategy()
{
TenantId = null,
};
using var mtc = new MultitenantContainer(strategy, builder.Build());
mtc.ConfigureTenant(null, b => b.RegisterType<StubDependency1Impl2>().As<IStubDependency1>());
mtc.ConfigureTenant("tenant2", b => b.RegisterType<StubDependency1Impl3>().As<IStubDependency1>());

Assert.IsType<StubDependency1Impl2>(mtc.Resolve<IStubDependency1>());
strategy.TenantId = "tenant2";
Assert.IsType<StubDependency1Impl3>(mtc.Resolve<IStubDependency1>());
}

[Fact]
public void Resolve_ResolvesTenantSpecificRegistrations()
{
Expand Down Expand Up @@ -621,12 +652,13 @@ public void GetTenants_CheckRegistered()
TenantId = "tenant1",
};
using var mtc = new MultitenantContainer(strategy, new ContainerBuilder().Build());
mtc.ConfigureTenant(null, b => b.RegisterType<StubDependency1Impl1>().AsImplementedInterfaces());
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().ToList();
Assert.Equal(3, registeredTenants.Count);
Assert.Equal(4, registeredTenants.Count);

foreach (var tenantId in registeredTenants)
{
Expand Down