Skip to content

Commit 2b785d1

Browse files
committed
Introduce FileReader and refactor ConfigurationFactory dependencies
1 parent 611c81b commit 2b785d1

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

RealTimeWeatherMonitoringApp/Infrastructure/Factory/ConfigurationFactory.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Text.Json;
22
using System.Text.Json.Serialization;
33
using RealTimeWeatherMonitoringApp.Infrastructure.Configuration;
4+
using RealTimeWeatherMonitoringApp.Infrastructure.Interfaces;
45
using RealTimeWeatherMonitoringApp.Infrastructure.Interfaces.Factory;
56

67
namespace RealTimeWeatherMonitoringApp.Infrastructure.Factory;
@@ -15,14 +16,11 @@ public class ConfigurationFactory : IConfigurationFactory
1516

1617
private readonly Lazy<IEnumerable<BotConfiguration>> _botConfigurations;
1718

18-
public ConfigurationFactory(string configurationFilepath)
19+
public ConfigurationFactory(string configurationFilepath, IFileReader fileReader)
1920
{
2021
_botConfigurations = new Lazy<IEnumerable<BotConfiguration>>(() =>
2122
{
22-
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
23-
var fullPath = Path.Combine(baseDirectory, configurationFilepath);
24-
var jsonText = File.ReadAllText(fullPath);
25-
23+
var jsonText = fileReader.ReadAllText(configurationFilepath);
2624
var botDict = JsonSerializer.Deserialize<Dictionary<string, BotConfiguration>>(jsonText, JsonOptions.Value);
2725
return botDict == null
2826
? Enumerable.Empty<BotConfiguration>()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace RealTimeWeatherMonitoringApp.Infrastructure.Interfaces;
2+
3+
public interface IFileReader
4+
{
5+
string ReadAllText(string path);
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using RealTimeWeatherMonitoringApp.Infrastructure.Interfaces;
2+
3+
namespace RealTimeWeatherMonitoringApp.Infrastructure.Service;
4+
5+
public class FileReader : IFileReader
6+
{
7+
public string ReadAllText(string path) => File.ReadAllText(path);
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using RealTimeWeatherMonitoringApp.Infrastructure.Interfaces;
2+
3+
namespace RealTimeWeatherMonitoringApp.Infrastructure.Service;
4+
5+
/// <summary>
6+
/// Appends the base directory to the given relative path
7+
/// </summary>
8+
public class RelativeFileReader : IFileReader
9+
{
10+
private readonly IFileReader _fileReader;
11+
public RelativeFileReader(IFileReader fileReader) => _fileReader = fileReader;
12+
13+
public string ReadAllText(string path)
14+
{
15+
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
16+
var fullPath = Path.Combine(baseDirectory, path);
17+
return _fileReader.ReadAllText(fullPath);
18+
}
19+
}

RealTimeWeatherMonitoringApp/Presentation/Utility/DependencyInjector.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ public static IServiceProvider CreateServiceProvider()
2929

3030
private static void InjectInfrastructure(IServiceCollection services)
3131
{
32-
services.AddSingleton<IConfigurationFactory, ConfigurationFactory>(_ =>
33-
new ConfigurationFactory(DirectoryStructureUtility.ConfigurationFilepath));
32+
services.AddSingleton<IFileReader>(_ => new RelativeFileReader(new FileReader()));
33+
services.AddSingleton<IConfigurationFactory, ConfigurationFactory>(p =>
34+
{
35+
var fileReader = p.GetRequiredService<IFileReader>();
36+
return new ConfigurationFactory(DirectoryStructureUtility.ConfigurationFilepath, fileReader);
37+
});
38+
3439
services.AddSingleton<IEvaluatorFactory<WeatherData>, WeatherEvaluatorFactory>();
3540
services.AddSingleton<IBotFactory<WeatherData>, BotFactory<WeatherData>>();
3641
services.AddSingleton<IBotInitializer<WeatherData>, BotInitializer<WeatherData>>();

0 commit comments

Comments
 (0)