Skip to content

Commit 9e02244

Browse files
committed
Introduce & Implement configuration factory
1 parent 0281487 commit 9e02244

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Text.Json;
2+
using RealTimeWeatherMonitoringApp.Infrastructure.Interfaces;
3+
4+
namespace RealTimeWeatherMonitoringApp.Infrastructure.Configuration;
5+
6+
public class ConfigurationFactory : IConfigurationFactory
7+
{
8+
private const string ConfigurationFilePath = "Infrastructure/Configuration/configuration.json";
9+
10+
private readonly Lazy<IEnumerable<BotConfiguration>> _botConfigurations = new(() =>
11+
{
12+
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
13+
var fullPath = Path.Combine(baseDirectory, ConfigurationFilePath);
14+
var jsonText = File.ReadAllText(fullPath);
15+
16+
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
17+
var botDict = JsonSerializer.Deserialize<Dictionary<string, BotConfiguration>>(jsonText, options);
18+
if (botDict == null) return Enumerable.Empty<BotConfiguration>();
19+
20+
return botDict
21+
.Select(kvp => kvp.Value with { Name = kvp.Key });
22+
});
23+
24+
public IEnumerable<BotConfiguration> CreateBotConfigurations() => _botConfigurations.Value;
25+
}

RealTimeWeatherMonitoringApp/Infrastructure/configuration.json renamed to RealTimeWeatherMonitoringApp/Infrastructure/Configuration/configuration.json

File renamed without changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using RealTimeWeatherMonitoringApp.Infrastructure.Configuration;
2+
3+
namespace RealTimeWeatherMonitoringApp.Infrastructure.Interfaces;
4+
5+
public interface IConfigurationFactory
6+
{
7+
IEnumerable<BotConfiguration> CreateBotConfigurations();
8+
}

RealTimeWeatherMonitoringApp/RealTimeWeatherMonitoringApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<None Update="Infrastructure\configuration.json">
15+
<None Update="Infrastructure\Configuration\configuration.json">
1616
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1717
</None>
1818
</ItemGroup>

0 commit comments

Comments
 (0)