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

Add facades through DI construction #5

Merged
merged 1 commit into from
Oct 23, 2023
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
8 changes: 4 additions & 4 deletions src/ClickHouse.Facades/Context/ClickHouseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public abstract class ClickHouseContext<TContext> : IDisposable, IAsyncDisposabl
{
private bool _initialized = false;
private ClickHouseConnection? _connection;
private ClickHouseFacadeRegistry<TContext>? _facadeRegistry;
private ClickHouseFacadeFactory<TContext> _facadeFactory = null!;
private readonly Dictionary<Type, ClickHouseFacade<TContext>> _facades = new();

private bool _allowDatabaseChanges = false;
Expand Down Expand Up @@ -43,7 +43,7 @@ public string? ServerTimezone
}

public TFacade GetFacade<TFacade>()
where TFacade : ClickHouseFacade<TContext>, new()
where TFacade : ClickHouseFacade<TContext>
{
ThrowIfNotInitialized();

Expand All @@ -52,7 +52,7 @@ public TFacade GetFacade<TFacade>()
return (TFacade) facade;
}

var newFacade = _facadeRegistry!.CreateFacade<TFacade>(_connection!);
var newFacade = _facadeFactory.CreateFacade<TFacade>(_connection!);
_facades.Add(typeof(TFacade), newFacade);

return newFacade;
Expand All @@ -75,7 +75,7 @@ internal void Initialize(ClickHouseContextOptions<TContext> options)
ThrowIfInitialized();

_connection = CreateConnection(options);
_facadeRegistry = options.FacadeRegistry;
_facadeFactory = options.FacadeFactory;
_allowDatabaseChanges = options.AllowDatabaseChanges;

_initialized = true;
Expand Down
10 changes: 5 additions & 5 deletions src/ClickHouse.Facades/Context/ClickHouseContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
public abstract class ClickHouseContextFactory<TContext> : IClickHouseContextFactory<TContext>
where TContext : ClickHouseContext<TContext>, new()
{
private ClickHouseFacadeRegistry<TContext>? _facadeRegistry;
private ClickHouseFacadeFactory<TContext>? _facadeFactory;

internal ClickHouseContextFactory<TContext> Setup(ClickHouseFacadeRegistry<TContext> facadeRegistry)
internal ClickHouseContextFactory<TContext> Setup(ClickHouseFacadeFactory<TContext> facadeFactory)
{
_facadeRegistry = facadeRegistry ?? throw new ArgumentNullException(nameof(facadeRegistry));
_facadeFactory = facadeFactory ?? throw new ArgumentNullException(nameof(facadeFactory));

return this;
}

public TContext CreateContext()
{
if (_facadeRegistry == null)
if (_facadeFactory == null)
{
throw new InvalidOperationException($"{GetType()} has no facade registry.");
}
Expand All @@ -24,7 +24,7 @@ public TContext CreateContext()
SetupContextOptions(builder);

var contextOptions = builder
.WithFacadeRegistry(_facadeRegistry)
.WithFacadeFactory(_facadeFactory)
.Build();

var context = new TContext();
Expand Down
2 changes: 1 addition & 1 deletion src/ClickHouse.Facades/Setup/ClickHouseContextOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public sealed class ClickHouseContextOptions<TContext>
internal string ConnectionString { get; set; } = "";
internal bool AllowDatabaseChanges { get; set; } = false;

internal ClickHouseFacadeRegistry<TContext> FacadeRegistry { get; set; } = new();
internal ClickHouseFacadeFactory<TContext> FacadeFactory { get; set; } = null!;

internal HttpClient? HttpClient { get; set; }
internal IHttpClientFactory? HttpClientFactory { get; set; }
Expand Down
16 changes: 8 additions & 8 deletions src/ClickHouse.Facades/Setup/ClickHouseContextOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class ClickHouseContextOptionsBuilder<TContext>

private OptionalValue<string> _connectionString;
private OptionalValue<bool> _forceSession;
private OptionalValue<ClickHouseFacadeRegistry<TContext>> _facadeRegistry;
private OptionalValue<ClickHouseFacadeFactory<TContext>> _facadeFactory;
private OptionalValue<bool> _allowDatabaseChanges;
private OptionalValue<HttpClient> _httpClient;
private OptionalValue<IHttpClientFactory> _httpClientFactory;
Expand Down Expand Up @@ -64,15 +64,15 @@ public ClickHouseContextOptionsBuilder<TContext> AllowDatabaseChanges()
true);
}

internal ClickHouseContextOptionsBuilder<TContext> WithFacadeRegistry(
ClickHouseFacadeRegistry<TContext> facadeRegistry)
internal ClickHouseContextOptionsBuilder<TContext> WithFacadeFactory(
ClickHouseFacadeFactory<TContext> facadeFactory)
{
ExceptionHelpers.ThrowIfNull(facadeRegistry);
ExceptionHelpers.ThrowIfNull(facadeFactory);

return WithPropertyValue(
builder => builder._facadeRegistry,
(builder, value) => builder._facadeRegistry = value,
facadeRegistry);
builder => builder._facadeFactory,
(builder, value) => builder._facadeFactory = value,
facadeFactory);
}

public ClickHouseContextOptionsBuilder<TContext> ForceSessions()
Expand Down Expand Up @@ -108,7 +108,7 @@ protected override ClickHouseContextOptions<TContext> BuildCore()
return new ClickHouseContextOptions<TContext>
{
ConnectionString = connectionString,
FacadeRegistry = _facadeRegistry.NotNullOrThrow(),
FacadeFactory = _facadeFactory.NotNullOrThrow(),
AllowDatabaseChanges = _allowDatabaseChanges.OrElseValue(false),
HttpClient = _httpClient.OrDefault(),
HttpClientFactory = _httpClientFactory.OrDefault(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private ClickHouseContextServiceBuilder()
}

public ClickHouseContextServiceBuilder<TContext> AddFacade<TFacade>()
where TFacade : ClickHouseFacade<TContext>, new()
where TFacade : ClickHouseFacade<TContext>
{
_facadeRegistry.AddFacade<TFacade>();

Expand Down
28 changes: 28 additions & 0 deletions src/ClickHouse.Facades/Setup/ClickHouseFacadeFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using ClickHouse.Client.ADO;
using Microsoft.Extensions.DependencyInjection;

namespace ClickHouse.Facades;

internal class ClickHouseFacadeFactory<TContext>
where TContext : ClickHouseContext<TContext>
{
private readonly ClickHouseFacadeRegistry<TContext> _registry;
private readonly IServiceProvider _serviceProvider;

public ClickHouseFacadeFactory(ClickHouseFacadeRegistry<TContext> registry, IServiceProvider serviceProvider)
{
_registry = registry ?? throw new ArgumentNullException(nameof(registry));
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
}

internal TFacade CreateFacade<TFacade>(ClickHouseConnection connection)
where TFacade : ClickHouseFacade<TContext>
{
if (_registry.Contains<TFacade>())
{
return (_serviceProvider.GetRequiredService<TFacade>().SetConnection(connection) as TFacade)!;
}

throw new InvalidOperationException($"Facade of type {typeof(TFacade)} was not found.");
}
}
15 changes: 4 additions & 11 deletions src/ClickHouse.Facades/Setup/ClickHouseFacadeRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using ClickHouse.Client.ADO;

namespace ClickHouse.Facades;
namespace ClickHouse.Facades;

internal sealed class ClickHouseFacadeRegistry<TContext>
where TContext : ClickHouseContext<TContext>
Expand All @@ -13,14 +11,9 @@ internal void AddFacade<TFacade>()
_facades.Add(typeof(TFacade));
}

internal TFacade CreateFacade<TFacade>(ClickHouseConnection connection)
where TFacade : ClickHouseFacade<TContext>, new()
internal bool Contains<TFacade>()
where TFacade : ClickHouseFacade<TContext>
{
if (_facades.Contains(typeof(TFacade)))
{
return (new TFacade().SetConnection(connection) as TFacade)!;
}

throw new InvalidOperationException($"Facade of type {typeof(TFacade)} was not found.");
return _facades.Contains(typeof(TFacade));
}
}
4 changes: 3 additions & 1 deletion src/ClickHouse.Facades/Setup/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static IServiceCollection AddClickHouseContext<TContext, TContextFactory>
typeof(IClickHouseContextFactory<TContext>),
serviceProvider => ActivatorUtilities
.CreateInstance<TContextFactory>(serviceProvider)
.Setup(serviceProvider.GetRequiredService<ClickHouseFacadeRegistry<TContext>>()),
.Setup(serviceProvider.GetRequiredService<ClickHouseFacadeFactory<TContext>>()),
factoryLifetime);

services.Add(descriptor);
Expand All @@ -43,6 +43,8 @@ public static IServiceCollection AddClickHouseContext<TContext, TContextFactory>
builderAction(builder);
builder.Build(services);

services.AddTransient<ClickHouseFacadeFactory<TContext>>();

return services;
}
}