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
{
///
/// Interaction logic for App.xaml
///
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(new DaSaSoDbContextFactory(connectionString));
services.AddSingleton, ClientDataService>();
services.AddSingleton, ProjectDataService>();
services.AddSingleton, BuildingsiteDataService>();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton>(services =>
{
return () => new ClientEditViewModel(
services.GetRequiredService>(),
services.GetRequiredService(),
new ViewModelDelegateRenavigator(
services.GetRequiredService()
));
});
services.AddSingleton>(services =>
{
return () => new HomeViewModel();
});
services.AddSingleton>(services =>
{
return () => new ClientListViewModel(
services.GetRequiredService>(),
services.GetRequiredService(),
new ViewModelDelegateRenavigator(
services.GetRequiredService()
));
});
services.AddSingleton>(services =>
{
return () => new ProjectListViewModel(
services.GetRequiredService>(),
services.GetRequiredService(),
new ViewModelDelegateRenavigator(
services.GetRequiredService()));
});
services.AddSingleton>(services =>
{
return () => new BuildingsiteListViewModel(
services.GetRequiredService>(),
services.GetRequiredService(),
new ViewModelDelegateRenavigator(
services.GetRequiredService()));
});
//services.AddSingleton();
services.AddScoped();
services.AddScoped();
services.AddScoped();
});
}
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() };
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();
}
}
}