Skip to content

Commit 669943d

Browse files
committed
Use reflection to inject parser strategy dependencies
Add ParsersNamespace constant to define where they should reside
1 parent 630a1d0 commit 669943d

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

RealTimeWeatherMonitoringApp/Presentation/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using RealTimeWeatherMonitoringApp.Domain.Interfaces.Service;
55
using RealTimeWeatherMonitoringApp.Domain.Models;
66
using RealTimeWeatherMonitoringApp.Presentation;
7+
using RealTimeWeatherMonitoringApp.Presentation.Utility;
78

89
// Inject Dependencies
910
var provider = DependencyInjector.CreateServiceProvider();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Reflection;
2+
3+
namespace RealTimeWeatherMonitoringApp.Presentation.Utility;
4+
5+
public static class Configuration
6+
{
7+
/// <summary>
8+
/// Where all implementations of IParsingStrategy should reside.
9+
/// </summary>
10+
public const string ParsersNamespace = "RealTimeWeatherMonitoringApp.Infrastructure.Parsers";
11+
12+
/// <summary>
13+
/// Retrieves all types within a specified namespace.
14+
/// </summary>
15+
/// <param name="namespace">The namespace to search for types.</param>
16+
/// <returns>An enumerable of types within the specified namespace.</returns>
17+
public static IEnumerable<Type> GetAllTypes(string @namespace)
18+
{
19+
if (string.IsNullOrEmpty(@namespace))
20+
throw new ArgumentException("Namespace cannot be null or empty.", nameof(@namespace));
21+
22+
var assembly = Assembly.GetExecutingAssembly();
23+
var types = assembly.GetTypes()
24+
.Where(t => string.Equals(t.Namespace, @namespace, StringComparison.Ordinal))
25+
.ToList();
26+
27+
return types;
28+
}
29+
}

RealTimeWeatherMonitoringApp/Presentation/DependencyInjector.cs renamed to RealTimeWeatherMonitoringApp/Presentation/Utility/DependencyInjector.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
using RealTimeWeatherMonitoringApp.Infrastructure.Factory;
1010
using RealTimeWeatherMonitoringApp.Infrastructure.Interfaces;
1111
using RealTimeWeatherMonitoringApp.Infrastructure.Interfaces.Factory;
12-
using RealTimeWeatherMonitoringApp.Infrastructure.Parsers;
1312
using RealTimeWeatherMonitoringApp.Infrastructure.Repository;
1413
using RealTimeWeatherMonitoringApp.Infrastructure.Service;
1514

16-
namespace RealTimeWeatherMonitoringApp.Presentation;
15+
namespace RealTimeWeatherMonitoringApp.Presentation.Utility;
1716

1817
public static class DependencyInjector
1918
{
@@ -40,13 +39,18 @@ private static void InjectDomain(IServiceCollection services)
4039
services.AddSingleton<IBotControllerService<WeatherData>, BotControllerService<WeatherData>>();
4140
}
4241

42+
4343
private static void InjectApplication(IServiceCollection services)
4444
{
4545
services.AddSingleton<IAutoParsingService<WeatherData>>(_ =>
4646
{
4747
var service = new AutoParsingService<WeatherData>();
48-
service.AddStrategy(new WeatherDataJsonParser());
49-
service.AddStrategy(new WeatherDataXmlParser());
48+
foreach (var parserType in Configuration.GetAllTypes(Configuration.ParsersNamespace))
49+
{
50+
if (Activator.CreateInstance(parserType) is IParsingStrategy<WeatherData> parser)
51+
service.AddStrategy(parser);
52+
}
53+
5054
return service;
5155
});
5256

0 commit comments

Comments
 (0)