Skip to content

Commit

Permalink
chore: add nullable reference types to the base project (#498)
Browse files Browse the repository at this point in the history
* Initial nullable reference type implementations for base project.

Co-authored-by: Brandon Foss <brandon_foss@selinc.com>
  • Loading branch information
fossbrandon and Brandon Foss authored Dec 18, 2021
1 parent 4e32086 commit 9ddb7f0
Show file tree
Hide file tree
Showing 47 changed files with 317 additions and 211 deletions.
6 changes: 3 additions & 3 deletions src/Finbuckle.MultiTenant/Abstractions/IMultiTenantContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Finbuckle.MultiTenant
public interface IMultiTenantContext<T>
where T : class, ITenantInfo, new()
{
T TenantInfo { get; set; }
StrategyInfo StrategyInfo { get; set; }
StoreInfo<T> StoreInfo { get; set; }
T? TenantInfo { get; set; }
StrategyInfo? StrategyInfo { get; set; }
StoreInfo<T>? StoreInfo { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace Finbuckle.MultiTenant
{
public interface IMultiTenantContextAccessor
{
object MultiTenantContext { get; set; }
object? MultiTenantContext { get; set; }
}

public interface IMultiTenantContextAccessor<T> where T : class, ITenantInfo, new()
{
IMultiTenantContext<T> MultiTenantContext { get; set; }
IMultiTenantContext<T>? MultiTenantContext { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Finbuckle.MultiTenant/Abstractions/IMultiTenantStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ namespace Finbuckle.MultiTenant
/// </summary>
/// <param name="identifier"></param>
/// <returns></returns>
Task<TTenantInfo> TryGetByIdentifierAsync(string identifier);
Task<TTenantInfo?> TryGetByIdentifierAsync(string identifier);

/// <summary>
/// Retrieve the TTenantInfo for a given tenant Id.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<TTenantInfo> TryGetAsync(string id);
Task<TTenantInfo?> TryGetAsync(string id);


/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface IMultiTenantStrategy
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
Task<string> GetIdentifierAsync(object context);
Task<string?> GetIdentifierAsync(object context);

/// <summary>
/// Determines strategy execution order. Normally handled in the order registered.
Expand Down
8 changes: 4 additions & 4 deletions src/Finbuckle.MultiTenant/Abstractions/ITenantInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace Finbuckle.MultiTenant
{
public interface ITenantInfo
{
string Id { get; set; }
string Identifier { get; set; }
string Name { get; set; }
string ConnectionString { get; set; }
string? Id { get; set; }
string? Identifier { get; set; }
string? Name { get; set; }
string? ConnectionString { get; set; }
}
}
6 changes: 3 additions & 3 deletions src/Finbuckle.MultiTenant/Abstractions/ITenantResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace Finbuckle.MultiTenant
{
public interface ITenantResolver
{
Task<object> ResolveAsync(object context);
Task<object?> ResolveAsync(object context);
}

public interface ITenantResolver<T>
where T : class, ITenantInfo, new()
{
IEnumerable<IMultiTenantStrategy> Strategies { get; }
IEnumerable<IMultiTenantStore<T>> Stores { get; }
Task<IMultiTenantContext<T>> ResolveAsync(object context);

Task<IMultiTenantContext<T>?> ResolveAsync(object context);
}
}
4 changes: 2 additions & 2 deletions src/Finbuckle.MultiTenant/Events/TenantNotFoundContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Finbuckle.MultiTenant
{
public class TenantNotResolvedContext
{
public object Context { get; set; }
public string Identifier { get; set; }
public object? Context { get; set; }
public string? Identifier { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/Finbuckle.MultiTenant/Events/TenantResolvedContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Finbuckle.MultiTenant
{
public class TenantResolvedContext
{
public object Context { get; set; }
public ITenantInfo TenantInfo { get; set; }
public object? Context { get; set; }
public ITenantInfo? TenantInfo { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static class FinbuckleMultiTenantBuilderExtensions
/// Adds a DistributedCacheStore to the application.
/// </summary>
public static FinbuckleMultiTenantBuilder<TTenantInfo> WithDistributedCacheStore<TTenantInfo>(this FinbuckleMultiTenantBuilder<TTenantInfo> builder)
where TTenantInfo : class, ITenantInfo, new()
where TTenantInfo : class, ITenantInfo, new()
=> builder.WithDistributedCacheStore(TimeSpan.MaxValue);


/// <summary>
/// Adds a DistributedCacheStore to the application.
Expand All @@ -34,7 +34,9 @@ public static FinbuckleMultiTenantBuilder<TTenantInfo> WithDistributedCacheStore
public static FinbuckleMultiTenantBuilder<TTenantInfo> WithDistributedCacheStore<TTenantInfo>(this FinbuckleMultiTenantBuilder<TTenantInfo> builder, TimeSpan? slidingExpiration)
where TTenantInfo : class, ITenantInfo, new()
{
return builder.WithStore<DistributedCacheStore<TTenantInfo>>(ServiceLifetime.Transient, Constants.TenantToken, slidingExpiration);
var storeParams = slidingExpiration is null ? new object[] { Constants.TenantToken } : new object[] { Constants.TenantToken, slidingExpiration };

return builder.WithStore<DistributedCacheStore<TTenantInfo>>(ServiceLifetime.Transient, storeParams);
}

/// <summary>
Expand All @@ -53,10 +55,10 @@ public static FinbuckleMultiTenantBuilder<TTenantInfo> WithHttpRemoteStore<TTena
/// <param name="clientConfig">An action to configure the underlying HttpClient.</param>
public static FinbuckleMultiTenantBuilder<TTenantInfo> WithHttpRemoteStore<TTenantInfo>(this FinbuckleMultiTenantBuilder<TTenantInfo> builder,
string endpointTemplate,
Action<IHttpClientBuilder> clientConfig) where TTenantInfo : class, ITenantInfo, new()
Action<IHttpClientBuilder>? clientConfig) where TTenantInfo : class, ITenantInfo, new()
{
var httpClientBuilder = builder.Services.AddHttpClient(typeof(HttpRemoteStoreClient<TTenantInfo>).FullName);
if (clientConfig != null)
var httpClientBuilder = builder.Services.AddHttpClient(typeof(HttpRemoteStoreClient<TTenantInfo>).FullName!);
if (clientConfig != null && httpClientBuilder != null)
clientConfig(httpClientBuilder);

builder.Services.TryAddSingleton<HttpRemoteStoreClient<TTenantInfo>>();
Expand Down Expand Up @@ -119,10 +121,10 @@ public static FinbuckleMultiTenantBuilder<TTenantInfo> WithStaticStrategy<TTenan
{
if (string.IsNullOrWhiteSpace(identifier))
{
throw new ArgumentException("Invalid value for \"identifier\"", nameof(identifier));
throw new ArgumentNullException(nameof(identifier), "Invalid value for \"identifier\"");
}

return builder.WithStrategy<StaticStrategy>(ServiceLifetime.Singleton, new object[] { identifier }); ;
return builder.WithStrategy<StaticStrategy>(ServiceLifetime.Singleton, new object[] { identifier });
}

/// <summary>
Expand Down
22 changes: 11 additions & 11 deletions src/Finbuckle.MultiTenant/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public static FinbuckleMultiTenantBuilder<T> AddMultiTenant<T>(this IServiceColl
services.AddScoped<ITenantResolver<T>, TenantResolver<T>>();
services.AddScoped<ITenantResolver>(sp => (ITenantResolver)sp.GetRequiredService<ITenantResolver<T>>());

services.AddScoped<IMultiTenantContext<T>>(sp => sp.GetRequiredService<IMultiTenantContextAccessor<T>>().MultiTenantContext);
services.AddScoped<T>(sp => sp.GetRequiredService<IMultiTenantContextAccessor<T>>().MultiTenantContext?.TenantInfo);
services.AddScoped<ITenantInfo>(sp => sp.GetService<T>());
services.AddScoped<IMultiTenantContext<T>>(sp => sp.GetRequiredService<IMultiTenantContextAccessor<T>>().MultiTenantContext!);

services.AddScoped<T>(sp => sp.GetRequiredService<IMultiTenantContextAccessor<T>>().MultiTenantContext?.TenantInfo!);
services.AddScoped<ITenantInfo>(sp => sp.GetService<T>()!);

services.AddSingleton<IMultiTenantContextAccessor<T>, MultiTenantContextAccessor<T>>();
services.AddSingleton<IMultiTenantContextAccessor>(sp => (IMultiTenantContextAccessor)sp.GetRequiredService<IMultiTenantContextAccessor<T>>());

services.Configure<MultiTenantOptions>(config);

return new FinbuckleMultiTenantBuilder<T>(services);
}

Expand All @@ -55,13 +55,13 @@ public static bool DecorateService<TService, TImpl>(this IServiceCollection serv
var newService = new ServiceDescriptor(existingService.ServiceType,
sp =>
{
TService inner = (TService)ActivatorUtilities.CreateInstance(sp, existingService.ImplementationType);
TService inner = (TService)ActivatorUtilities.CreateInstance(sp, existingService.ImplementationType!);

var parameters2 = new object[parameters.Length + 1];
Array.Copy(parameters, 0, parameters2, 1, parameters.Length);
parameters2[0] = inner;

return ActivatorUtilities.CreateInstance<TImpl>(sp, parameters2);
return ActivatorUtilities.CreateInstance<TImpl>(sp, parameters2)!;
},
existingService.Lifetime);

Expand All @@ -71,7 +71,7 @@ public static bool DecorateService<TService, TImpl>(this IServiceCollection serv
sp =>
{
TService inner = (TService)existingService.ImplementationInstance;
return ActivatorUtilities.CreateInstance<TImpl>(sp, inner, parameters);
return ActivatorUtilities.CreateInstance<TImpl>(sp, inner, parameters)!;
},
existingService.Lifetime);
}
Expand All @@ -81,7 +81,7 @@ public static bool DecorateService<TService, TImpl>(this IServiceCollection serv
sp =>
{
TService inner = (TService)existingService.ImplementationFactory(sp);
return ActivatorUtilities.CreateInstance<TImpl>(sp, inner, parameters);
return ActivatorUtilities.CreateInstance<TImpl>(sp, inner, parameters)!;
},
existingService.Lifetime);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Finbuckle.MultiTenant/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static bool ImplementsOrInheritsUnboundGeneric(this Type source, Type unb
return source.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == unboundGeneric);
}

Type toCheck = source;
Type? toCheck = source;

if (unboundGeneric != toCheck)
{
Expand Down
1 change: 1 addition & 0 deletions src/Finbuckle.MultiTenant/Finbuckle.MultiTenant.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFrameworks>net6.0;net5.0;netstandard2.1</TargetFrameworks>
<Title>Finbuckle.MultiTenant</Title>
<Description>Main library package for Finbuckle.MultiTenant.</Description>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace Finbuckle.MultiTenant.Core
public class MultiTenantContextAccessor<T> : IMultiTenantContextAccessor<T>, IMultiTenantContextAccessor
where T : class, ITenantInfo, new()
{
internal static AsyncLocal<IMultiTenantContext<T>> _asyncLocalContext = new AsyncLocal<IMultiTenantContext<T>>();
internal static AsyncLocal<IMultiTenantContext<T>?> _asyncLocalContext = new AsyncLocal<IMultiTenantContext<T>?>();

public IMultiTenantContext<T> MultiTenantContext
{
public IMultiTenantContext<T>? MultiTenantContext
{
get
{
return _asyncLocalContext.Value;
Expand All @@ -23,7 +23,7 @@ public IMultiTenantContext<T> MultiTenantContext
}
}

object IMultiTenantContextAccessor.MultiTenantContext
object? IMultiTenantContextAccessor.MultiTenantContext
{
get => MultiTenantContext;
set => MultiTenantContext = value as IMultiTenantContext<T> ?? MultiTenantContext;
Expand Down
6 changes: 3 additions & 3 deletions src/Finbuckle.MultiTenant/MultiTenantContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Finbuckle.MultiTenant
public class MultiTenantContext<T> : IMultiTenantContext<T>
where T : class, ITenantInfo, new()
{
public T TenantInfo { get; set; }
public StrategyInfo StrategyInfo { get; set; }
public StoreInfo<T> StoreInfo { get; set; }
public T? TenantInfo { get; set; }
public StrategyInfo? StrategyInfo { get; set; }
public StoreInfo<T>? StoreInfo { get; set; }
}
}
9 changes: 7 additions & 2 deletions src/Finbuckle.MultiTenant/MultiTenantException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ namespace Finbuckle.MultiTenant
/// </summary>
public class MultiTenantException : Exception
{
public MultiTenantException(string message, Exception innerException = null)
: base(message, innerException) { }
public MultiTenantException(string? message) : base(message)
{
}

public MultiTenantException(string? message, Exception? innerException) : base(message, innerException)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// https://github.com/dotnet/runtime/blob/5aad989cebe00f0987fcb842ea5b7cbe986c67df/src/libraries/Microsoft.Extensions.Options/src/OptionsFactory.cs

using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Options;

namespace Finbuckle.MultiTenant.Options
Expand Down Expand Up @@ -34,7 +33,7 @@ public MultiTenantOptionsFactory(IEnumerable<IConfigureOptions<TOptions>> config
// by checking for an array and enumerate over that, so we don't need to allocate an enumerator.
// When it isn't already an array, convert it to one, but don't use System.Linq to avoid pulling Linq in to
// small trimmed applications.

_configureOptions = configureOptions as IConfigureOptions<TOptions>[] ?? new List<IConfigureOptions<TOptions>>(configureOptions).ToArray();
_postConfigureOptions = postConfigureOptions as IPostConfigureOptions<TOptions>[] ?? new List<IPostConfigureOptions<TOptions>>(postConfigureOptions).ToArray();
_validations = validations as IValidateOptions<TOptions>[] ?? new List<IValidateOptions<TOptions>>(validations).ToArray();
Expand Down Expand Up @@ -68,7 +67,7 @@ public TOptions Create(string name)
{
post.PostConfigure(name, options);
}

if (_validations.Length > 0)
{
var failures = new List<string>();
Expand All @@ -85,7 +84,7 @@ public TOptions Create(string name)
throw new OptionsValidationException(name, typeof(TOptions), failures);
}
}

return options;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Finbuckle.MultiTenant/StoreInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Finbuckle.MultiTenant
{
public class StoreInfo<TTenantInfo> where TTenantInfo : class, ITenantInfo, new()
{
public Type StoreType { get; internal set; }
public IMultiTenantStore<TTenantInfo> Store { get; internal set; }
public Type? StoreType { get; internal set; }
public IMultiTenantStore<TTenantInfo>? Store { get; internal set; }
}
}
Loading

0 comments on commit 9ddb7f0

Please sign in to comment.