-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
92 lines (78 loc) · 3.41 KB
/
App.xaml.cs
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using CrylatoProjectDesktop.DbContexts;
using CrylatoProjectDesktop.Models;
using CrylatoProjectDesktop.Services;
using CrylatoProjectDesktop.Services.TaskCreators;
using CrylatoProjectDesktop.Services.TaskProviders;
using CrylatoProjectDesktop.Stores;
using CrylatoProjectDesktop.ViewModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
namespace CrylatoProjectDesktop
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private const string CONNECTION_STRING = "Data Source=taskmaker.db";
private IHost _host;
public App()
{
_host = Host.CreateDefaultBuilder().ConfigureServices(services =>
{
services.AddSingleton(new TaskMakerDbContextFactory(CONNECTION_STRING));
services.AddSingleton<ITaskProvider, DatabaseTaskProvider>();
services.AddSingleton<ITaskCreator, DatabaseTaskCreator>();
services.AddTransient<TaskBook>();
services.AddSingleton((s) => new Team("Deafault team", s.GetRequiredService<TaskBook>()));
services.AddTransient((s) => CreateTaskListingViewModel(s));
services.AddSingleton<Func<TaskListingViewModel>>((s) => () => s.GetRequiredService<TaskListingViewModel>());
services.AddSingleton<NavigationService<TaskListingViewModel>>();
services.AddTransient<MakeTaskViewModel>();
services.AddSingleton<Func<MakeTaskViewModel>>((s) => () => s.GetRequiredService<MakeTaskViewModel>());
services.AddSingleton<NavigationService<MakeTaskViewModel>>();
services.AddSingleton<TeamStore>();
services.AddSingleton<NavigationStore>();
services.AddSingleton<MainViewModel>();
services.AddSingleton(s => new MainWindow()
{
DataContext = s.GetRequiredService<MainViewModel>()
});
})
.Build();
}
private TaskListingViewModel CreateTaskListingViewModel(IServiceProvider services)
{
return TaskListingViewModel.LoadViewModel(
services.GetRequiredService<TeamStore>(),
services.GetRequiredService<NavigationService<MakeTaskViewModel>>()
);
}
protected override void OnStartup(StartupEventArgs e)
{
_host.Start();
TaskMakerDbContextFactory taskMakerDbContextFactory = _host.Services.GetRequiredService<TaskMakerDbContextFactory>();
using (TaskMakerDbContext dbContext = taskMakerDbContextFactory.CreateDbContext())
{
dbContext.Database.Migrate();
}
NavigationService<TaskListingViewModel> navigationService = _host.Services.GetRequiredService<NavigationService<TaskListingViewModel>>();
navigationService.Navigate();
MainWindow = _host.Services.GetRequiredService<MainWindow>();
MainWindow.Show();
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
_host.Dispose();
base.OnExit(e);
}
}
}