-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
44 lines (31 loc) · 1.66 KB
/
Program.cs
File metadata and controls
44 lines (31 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace WeatherDataCollector {
internal class Program {
static void Main(string[] args) {
//REGISTERING DEPENDENCIES
//WITH DEPENDENCY INJECTION, THE WEATHER DATA SOURCE CAN BE EASILY SWAPPED
ServiceProvider services = new ServiceCollection()
//.AddScoped<IWeatherDataSource, APIWeatherSource>()
.AddScoped<IWeatherDataSource, FileWeatherSource>()
.AddDbContext<ApplicationDbContext>()
.AddScoped<DatabaseService>()
.BuildServiceProvider();
var weatherSource = services.GetRequiredService<IWeatherDataSource>();
var databaseService = services.GetRequiredService<DatabaseService>();
WeatherData data = weatherSource.SendWeatherData();
Console.WriteLine("Received weather data: " + data.Temperature + " " + data.Description);
databaseService.SaveWeatherData(data);
//BAD PRACTICE - MANUAL DEPENDENCY MANAGEMENT
/*ApplicationDbContext context = new ApplicationDbContext();
IWeatherDataSource apiTest = new APIWeatherSource();
WeatherData apiData = apiTest.SendWeatherData();
IWeatherDataSource xmlTest = new FileWeatherSource();
WeatherData xmlData = xmlTest.SendWeatherData();
Console.WriteLine(apiData.Temperature + " " + apiData.Description);
DatabaseService databaseHandler = new DatabaseService(context);
databaseHandler.SaveWeatherData(apiData);
databaseHandler.SaveWeatherData(xmlData);*/
}
}
}