From 8fe51a15d5f0b801343750ac82c2a383a6b39fe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20Ko=C5=BEelj?= Date: Fri, 20 Jan 2023 10:21:46 +0100 Subject: [PATCH] Add IConfiguration constructors to Storage and Endpoint configuration --- src/Connected.Configuration/ConfigurationService.cs | 6 ++---- .../DatabaseConfiguration.cs | 13 ++++++------- .../Endpoints/EndpointConfiguration.cs | 9 ++++++++- .../IDatabaseConfiguration.cs | 2 +- src/Connected.Configuration/StorageConfiguration.cs | 8 +++++--- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/Connected.Configuration/ConfigurationService.cs b/src/Connected.Configuration/ConfigurationService.cs index e2b0e68..d87ecba 100644 --- a/src/Connected.Configuration/ConfigurationService.cs +++ b/src/Connected.Configuration/ConfigurationService.cs @@ -8,11 +8,9 @@ internal class ConfigurationService : IConfigurationService { public ConfigurationService(IConfiguration configuration) { - Endpoint = new EndpointConfiguration(); - configuration.Bind("endpoint", Endpoint); + Endpoint = new EndpointConfiguration(configuration.GetSection("endpoint")); - Storage = new StorageConfiguration(); - configuration.Bind("storage", Storage); + Storage = new StorageConfiguration(configuration.GetSection("storage")); } public IEndpointConfiguration Endpoint { get; } diff --git a/src/Connected.Configuration/DatabaseConfiguration.cs b/src/Connected.Configuration/DatabaseConfiguration.cs index 9935937..0b4cef0 100644 --- a/src/Connected.Configuration/DatabaseConfiguration.cs +++ b/src/Connected.Configuration/DatabaseConfiguration.cs @@ -1,19 +1,18 @@ using System.Collections.Immutable; +using Microsoft.Extensions.Configuration; namespace Connected.Configuration { internal class DatabaseConfiguration : IDatabaseConfiguration { - private List _shards; + private readonly List _shards; - public DatabaseConfiguration() + public DatabaseConfiguration(IConfiguration configuration) { - /* - * TODO: read from config - */ - DefaultConnectionString = "server=PIT-ZBOOK\\sqlexpress; database=connected; trusted_connection=true;TrustServerCertificate=True;multiple active result sets=true"; - + DefaultConnectionString = ""; _shards = new(); + configuration.Bind(this); + configuration.Bind("shards", _shards); } public string? DefaultConnectionString { get; init; } diff --git a/src/Connected.Configuration/Endpoints/EndpointConfiguration.cs b/src/Connected.Configuration/Endpoints/EndpointConfiguration.cs index a042b84..d009b21 100644 --- a/src/Connected.Configuration/Endpoints/EndpointConfiguration.cs +++ b/src/Connected.Configuration/Endpoints/EndpointConfiguration.cs @@ -1,7 +1,14 @@ -namespace Connected.Configuration.Endpoints +using Microsoft.Extensions.Configuration; + +namespace Connected.Configuration.Endpoints { internal sealed class EndpointConfiguration : IEndpointConfiguration { + public EndpointConfiguration(IConfiguration configuration) + { + configuration.Bind(this); + } + public string? Address { get; set; } } } diff --git a/src/Connected.Configuration/IDatabaseConfiguration.cs b/src/Connected.Configuration/IDatabaseConfiguration.cs index 94ce5d4..af05b8c 100644 --- a/src/Connected.Configuration/IDatabaseConfiguration.cs +++ b/src/Connected.Configuration/IDatabaseConfiguration.cs @@ -4,7 +4,7 @@ namespace Connected.Configuration { public interface IDatabaseConfiguration { - string DefaultConnectionString { get; } + string? DefaultConnectionString { get; } ImmutableList Shards { get; } } diff --git a/src/Connected.Configuration/StorageConfiguration.cs b/src/Connected.Configuration/StorageConfiguration.cs index 67526e5..1d55817 100644 --- a/src/Connected.Configuration/StorageConfiguration.cs +++ b/src/Connected.Configuration/StorageConfiguration.cs @@ -1,10 +1,12 @@ -namespace Connected.Configuration +using Microsoft.Extensions.Configuration; + +namespace Connected.Configuration { internal class StorageConfiguration : IStorageConfiguration { - public StorageConfiguration() + public StorageConfiguration(IConfiguration configuration) { - Databases = new DatabaseConfiguration(); + Databases = new DatabaseConfiguration(configuration.GetSection("databases")); } public IDatabaseConfiguration Databases { get; }