File tree Expand file tree Collapse file tree 3 files changed +38
-4
lines changed
RealTimeWeatherMonitoringApp/Presentation Expand file tree Collapse file tree 3 files changed +38
-4
lines changed Original file line number Diff line number Diff line change 44using RealTimeWeatherMonitoringApp . Domain . Interfaces . Service ;
55using RealTimeWeatherMonitoringApp . Domain . Models ;
66using RealTimeWeatherMonitoringApp . Presentation ;
7+ using RealTimeWeatherMonitoringApp . Presentation . Utility ;
78
89// Inject Dependencies
910var provider = DependencyInjector . CreateServiceProvider ( ) ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 99using RealTimeWeatherMonitoringApp . Infrastructure . Factory ;
1010using RealTimeWeatherMonitoringApp . Infrastructure . Interfaces ;
1111using RealTimeWeatherMonitoringApp . Infrastructure . Interfaces . Factory ;
12- using RealTimeWeatherMonitoringApp . Infrastructure . Parsers ;
1312using RealTimeWeatherMonitoringApp . Infrastructure . Repository ;
1413using RealTimeWeatherMonitoringApp . Infrastructure . Service ;
1514
16- namespace RealTimeWeatherMonitoringApp . Presentation ;
15+ namespace RealTimeWeatherMonitoringApp . Presentation . Utility ;
1716
1817public 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
You can’t perform that action at this time.
0 commit comments