Files
DaSaSo/DaSaSo.Wpf/App.xaml.cs
2021-09-15 17:21:56 +02:00

99 lines
3.9 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.DependencyInjection;
using System;
using System.Windows;
namespace DaSaSo.Wpf
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
IServiceProvider serviceProvider = CreateServiceProvider();
MainWindow? window = new MainWindow() { DataContext = serviceProvider.GetRequiredService<MainWindowViewModel>() };
window.Show();
base.OnStartup(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();
}
private IServiceProvider CreateServiceProvider()
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton<DaSaSoDbContextFactory>();
services.AddSingleton<IDataService<Client>, ClientDataService>();
services.AddSingleton<ClientListViewModel>();
services.AddSingleton<IViewModelAbstractFactory, ViewModelAbstractFactory>();
services.AddSingleton<CreateViewModel<ClientEditViewModel>>(services =>
{
return () => new ClientEditViewModel(
services.GetRequiredService<IDataService<Client>>(),
services.GetRequiredService<IActualProject>(),
new ViewModelDelegateRenavigator<HomeViewModel>(
services.GetRequiredService<INavigator>(),
services.GetRequiredService<CreateViewModel<HomeViewModel>>()
));
});
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<ClientEditViewModel>(
services.GetRequiredService<INavigator>(),
services.GetRequiredService<CreateViewModel<ClientEditViewModel>>()
));
});
//services.AddSingleton<INavigator, Navigator>();
services.AddScoped<IActualProject, ActualProject>();
services.AddScoped<INavigator, Navigator>();
services.AddScoped<MainWindowViewModel>();
return services.BuildServiceProvider();
}
}
}