130 lines
5.1 KiB
C#
130 lines
5.1 KiB
C#
using DaSaSo.Domain.Model;
|
|
using DaSaSo.Domain.Services;
|
|
using DaSaSo.EntityFramework;
|
|
using DaSaSo.EntityFramework.Services;
|
|
using DaSaSo.ViewModel;
|
|
using DaSaSo.ViewModel.Factories;
|
|
using DaSaSo.ViewModel.Interface;
|
|
using DaSaSo.ViewModel.State.ActualState;
|
|
using DaSaSo.ViewModel.State.Navigation;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using System;
|
|
using System.Windows;
|
|
|
|
namespace DaSaSo.Wpf
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for App.xaml
|
|
/// </summary>
|
|
public partial class App : Application
|
|
{
|
|
private readonly IHost _host;
|
|
public App()
|
|
{
|
|
_host = CreateHostBuilder().Build();
|
|
}
|
|
|
|
public static IHostBuilder CreateHostBuilder(string[]? args = null)
|
|
{
|
|
return Host.CreateDefaultBuilder(args)
|
|
.ConfigureAppConfiguration(c=>
|
|
{
|
|
c.AddJsonFile("appsettings.json");
|
|
c.AddEnvironmentVariables();
|
|
})
|
|
.ConfigureServices((context, services) =>
|
|
{
|
|
|
|
string connectionString = context.Configuration.GetConnectionString("default");
|
|
|
|
services.AddSingleton<DaSaSoDbContextFactory>(new DaSaSoDbContextFactory(connectionString));
|
|
services.AddSingleton<IDataService<Client>, ClientDataService>();
|
|
services.AddSingleton<IDataService<Project>, ProjectDataService>();
|
|
services.AddSingleton<ClientListViewModel>();
|
|
|
|
|
|
|
|
services.AddSingleton<IViewModelAbstractFactory, ViewModelAbstractFactory>();
|
|
services.AddSingleton<CreateViewModel<ClientEditViewModel>>(services =>
|
|
{
|
|
return () => new ClientEditViewModel(
|
|
services.GetRequiredService<IDataService<Client>>(),
|
|
services.GetRequiredService<IActualProject>(),
|
|
new ViewModelDelegateRenavigator(
|
|
services.GetRequiredService<INavigator>()
|
|
));
|
|
});
|
|
services.AddSingleton<CreateViewModel<HomeViewModel>>(services =>
|
|
{
|
|
return () => new HomeViewModel();
|
|
});
|
|
services.AddSingleton<CreateViewModel<ClientListViewModel>>(services =>
|
|
{
|
|
return () => new ClientListViewModel(
|
|
services.GetRequiredService<IDataService<Client>>(),
|
|
services.GetRequiredService<IActualProject>(),
|
|
new ViewModelDelegateRenavigator(
|
|
services.GetRequiredService<INavigator>()
|
|
));
|
|
});
|
|
|
|
services.AddSingleton<CreateViewModel<ProjectListViewModel>>(services =>
|
|
{
|
|
return () => new ProjectListViewModel(
|
|
services.GetRequiredService<IDataService<Project>>(),
|
|
services.GetRequiredService<IActualProject>(),
|
|
new ViewModelDelegateRenavigator(
|
|
services.GetRequiredService<INavigator>()));
|
|
});
|
|
|
|
//services.AddSingleton<INavigator, Navigator>();
|
|
services.AddScoped<IActualProject, ActualProject>();
|
|
services.AddScoped<INavigator, Navigator>();
|
|
|
|
services.AddScoped<MainWindowViewModel>();
|
|
});
|
|
}
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
|
|
|
|
|
_host.Start();
|
|
|
|
MainWindow? window = new MainWindow() { DataContext = _host.Services.GetRequiredService<MainWindowViewModel>() };
|
|
window.Show();
|
|
base.OnStartup(e);
|
|
}
|
|
|
|
protected override async void OnExit(ExitEventArgs e)
|
|
{
|
|
_host.StopAsync();
|
|
_host.Dispose();
|
|
base.OnExit(e);
|
|
}
|
|
|
|
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
Exception ex = (Exception)e.ExceptionObject;
|
|
string text = "An application error occured. Plrease contact the Administrator with the following information:\n\n";
|
|
MessageBox.Show(text + " " + ex.Message + "\n\n" + ex.StackTrace);
|
|
}
|
|
catch(Exception ex2)
|
|
{
|
|
MessageBox.Show("Fatal Non-UI error", ex2.Message, MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
}
|
|
}
|