Skip to content

Commit 0ffd250

Browse files
committed
Control UserController dependencies via DependencyInjector
UserController should not have reference to ServiceProvider
1 parent 98b8960 commit 0ffd250

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

RealTimeWeatherMonitoringApp/Presentation/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
weatherEventManager.Attach(weatherDataNotifier, weatherBotPublisher);
1717

1818
// Start
19-
new UserController(provider).Start();
19+
provider.GetRequiredService<UserController>().Start();

RealTimeWeatherMonitoringApp/Presentation/UserController.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
using Microsoft.Extensions.DependencyInjection;
21
using RealTimeWeatherMonitoringApp.Application.Interfaces.Service;
32
using RealTimeWeatherMonitoringApp.Domain.Models;
43

54
namespace RealTimeWeatherMonitoringApp.Presentation;
65

76
public class UserController
87
{
9-
private readonly IServiceProvider _provider;
10-
public UserController(IServiceProvider provider) => _provider = provider;
8+
private readonly IBotNotificationService _botNotificationService;
9+
private readonly IDataProcessingService<WeatherData> _weatherDataProcessor;
1110

12-
private IBotNotificationService BotNotificationService => _provider.GetRequiredService<IBotNotificationService>();
13-
14-
private IDataProcessingService<WeatherData> WeatherDataProcessor =>
15-
_provider.GetRequiredService<IDataProcessingService<WeatherData>>();
11+
public UserController(
12+
IBotNotificationService botNotificationService,
13+
IDataProcessingService<WeatherData> weatherDataProcessor)
14+
{
15+
_botNotificationService = botNotificationService;
16+
_weatherDataProcessor = weatherDataProcessor;
17+
}
1618

1719
public void Start()
1820
{
19-
BotNotificationService.OnBotNotification += (_, args) =>
21+
_botNotificationService.OnBotNotification += (_, args) =>
2022
Console.WriteLine($"\n{args.BotName}: {args.Message}");
2123

2224
while (true)
@@ -26,7 +28,7 @@ public void Start()
2628
var input = Console.ReadLine() ?? string.Empty;
2729
if (input.Equals("q", StringComparison.CurrentCultureIgnoreCase)) break;
2830

29-
var result = WeatherDataProcessor.Process(input);
31+
var result = _weatherDataProcessor.Process(input);
3032
if (result.Fail) Console.WriteLine(result.Message);
3133
}
3234
}

RealTimeWeatherMonitoringApp/Presentation/Utility/DependencyInjector.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static IServiceProvider CreateServiceProvider()
2222
InjectInfrastructure(services);
2323
InjectDomain(services);
2424
InjectApplication(services);
25+
InjectPresentation(services);
2526
return services.BuildServiceProvider();
2627
}
2728

@@ -67,4 +68,9 @@ private static void InjectApplication(IServiceCollection services)
6768
services.AddSingleton<IBotEventManager<WeatherData>, BotEventManager<WeatherData>>();
6869
services.AddSingleton<IDataProcessingService<WeatherData>, DataProcessingService<WeatherData>>();
6970
}
71+
72+
private static void InjectPresentation(IServiceCollection services)
73+
{
74+
services.AddSingleton<UserController>();
75+
}
7076
}

0 commit comments

Comments
 (0)