Commands auf Async umgestellt

This commit is contained in:
HuskyTeufel
2021-09-15 19:12:31 +02:00
parent 00718f9821
commit e0c9839275
13 changed files with 242 additions and 90 deletions

View File

@@ -8,13 +8,21 @@ using System.Threading.Tasks;
namespace DaSaSo.EntityFramework
{
public class DaSaSoDbContextFactory : IDesignTimeDbContextFactory<DaSaSoDbContext>
public class DaSaSoDbContextFactory
{
public DaSaSoDbContext CreateDbContext(string[]? args = null)
private readonly string _connectionString;
public DaSaSoDbContextFactory(string connectionString)
{
_connectionString = connectionString;
}
public DaSaSoDbContext CreateDbContext()
{
var options = new DbContextOptionsBuilder<DaSaSoDbContext>();
options.UseNpgsql("Host = localhost; Database = dasaso; Username = kansan; Password = kansan");
return new DaSaSoDbContext(options.Options);
options.UseNpgsql(_connectionString);
DaSaSoDbContext result = new DaSaSoDbContext(options.Options);
return result;
}
}
}

View File

@@ -0,0 +1,60 @@
using DaSaSo.Domain.Model;
using DaSaSo.Domain.Services;
using DaSaSo.EntityFramework.Services.Common;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DaSaSo.EntityFramework.Services
{
public class ProjectDataService : IDataService<Project>
{
private readonly DaSaSoDbContextFactory _contextFactory;
private readonly NonQueryDataService<Project> _nonQueryDataService;
public ProjectDataService(DaSaSoDbContextFactory contextFactory)
{
_contextFactory = contextFactory;
_nonQueryDataService = new NonQueryDataService<Project>(contextFactory);
}
public async Task<Project> Create(Project entity)
{
return await _nonQueryDataService.Create(entity);
}
public async Task<bool> Delete(int id)
{
return await _nonQueryDataService.Delete(id);
}
public Task<Project> Get(int id)
{
throw new NotImplementedException();
}
public async Task<IEnumerable<Project>> GetAllByClient(Client client)
{
// Get Clientid
int id = client.Id;
using (DaSaSoDbContext context = _contextFactory.CreateDbContext())
{
IEnumerable<Project> entities = await context.Projects.Where(x => x.Client.Id == id).ToListAsync();
return entities;
}
}
public Task<IEnumerable<Project>> GetAll()
{
throw new NotImplementedException();
}
public async Task<Project> Update(int id, Project entity)
{
return await _nonQueryDataService.Update(id, entity);
}
}
}

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace DaSaSo.ViewModel.Commands
{
public abstract class AsyncCommandBase : ICommand
{
bool _isExecuting = false;
public event EventHandler? CanExecuteChanged;
public bool IsExecuting
{
get => _isExecuting;
set
{
_isExecuting = value;
CanExecuteChanged?.Invoke(this, new EventArgs());
}
}
public bool CanExecute(object? parameter)
{
return !IsExecuting;
}
public async void Execute(object? parameter)
{
IsExecuting = true;
await ExecuteAsync(parameter);
IsExecuting = false;
}
public abstract Task ExecuteAsync(object? parameter);
}
}

View File

@@ -11,15 +11,13 @@ using System.Windows.Input;
namespace DaSaSo.ViewModel.Commands
{
public class EditClientCommand : ICommand
public class EditClientCommand : AsyncCommandBase
{
private readonly IDataService<Client> dataservice;
private readonly IActualProject actualProject;
private readonly IRenavigator renavigator;
private readonly ClientListViewModel clientListViewModel;
public event EventHandler? CanExecuteChanged;
public EditClientCommand(IDataService<Client> dataservice, IActualProject actualProject,IRenavigator renavigator, ClientListViewModel clientListViewModel)
@@ -30,12 +28,8 @@ namespace DaSaSo.ViewModel.Commands
this.clientListViewModel = clientListViewModel;
}
public bool CanExecute(object? parameter)
{
return true;
}
public void Execute(object? parameter)
public override async Task ExecuteAsync(object? parameter)
{
actualProject.SetClient(clientListViewModel.SelectedClient);
renavigator.Renavigate(new ClientEditViewModel(dataservice,actualProject,renavigator));

View File

@@ -8,9 +8,9 @@ using System.Windows.Input;
namespace DaSaSo.ViewModel.Commands
{
public class SelectClientCommand : ICommand
public class SelectClientCommand : AsyncCommandBase
{
public event EventHandler? CanExecuteChanged;
private readonly IActualProject _actualProject;
private readonly ClientListViewModel _clientListViewModel;
public SelectClientCommand(IActualProject actualProject, ClientListViewModel clientListViewModel)
@@ -19,15 +19,11 @@ namespace DaSaSo.ViewModel.Commands
_clientListViewModel = clientListViewModel;
}
public bool CanExecute(object? parameter)
{
return true;
}
public void Execute(object? parameter)
public override async Task ExecuteAsync(object? parameter)
{
var s = _clientListViewModel.SelectedClient;
_actualProject.SetClient(s);
}
}
}

View File

@@ -4,9 +4,8 @@ using System.Windows.Input;
namespace DaSaSo.ViewModel.Commands
{
class UpdateCurrentViewModelCommand : ICommand
class UpdateCurrentViewModelCommand : AsyncCommandBase
{
public event EventHandler? CanExecuteChanged;
private INavigator _navigator;
private readonly IViewModelAbstractFactory _viewModelFactory;
@@ -16,12 +15,9 @@ namespace DaSaSo.ViewModel.Commands
_viewModelFactory = viewModelFactory;
}
public bool CanExecute(object? parameter)
{
return true;
}
public void Execute(object? parameter)
public override async Task ExecuteAsync(object? parameter)
{
if(parameter is EViewType)
{

View File

@@ -20,15 +20,15 @@ namespace DaSaSo.ViewModel.Factories
public ViewModelAbstractFactory(
CreateViewModel<HomeViewModel> createHomeViewModel,
CreateViewModel<ClientListViewModel> createClientListViewModel/*,
CreateViewModel<ClientListViewModel> createClientListViewModel,
CreateViewModel<ClientEditViewModel> createClientEditViewModel,
CreateViewModel<ProjectListViewModel> createProjektListViewModel*/
CreateViewModel<ProjectListViewModel> createProjektListViewModel
)
{
_createHomeViewModel = createHomeViewModel;
_createClientListViewModel = createClientListViewModel;
/*_createClientEditViewModel = createClientEditViewModel;
_createProjektListViewModel = createProjektListViewModel;*/
_createClientEditViewModel = createClientEditViewModel;
_createProjektListViewModel = createProjektListViewModel;
}
public BaseViewModel CreateViewModel(EViewType viewType)
@@ -50,7 +50,7 @@ namespace DaSaSo.ViewModel.Factories
break;
*/
default:
throw new ArgumentException("The Viewtype dos not have a ViewModel.", "viewType");
throw new ArgumentException("The Viewtype does not have a ViewModel.", "viewType");
}
}
}

View File

@@ -1,9 +1,11 @@
using DaSaSo.Domain.Model;
using DaSaSo.Domain.Services;
using DaSaSo.EntityFramework.Services;
using DaSaSo.ViewModel.Interface;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -12,25 +14,27 @@ namespace DaSaSo.ViewModel
{
public class ProjectListViewModel : BaseViewModel
{
private GenericDataService<Project> genericDataService;
private ProjectDataService genericDataService;
private IActualProject actualProject;
private IRenavigator renavigator;
public ObservableCollection<Project> Projekte { get; }
public ProjectListViewModel(GenericDataService<Project> genericDataService, IActualProject actualProject, IRenavigator renavigator)
public ProjectListViewModel(IDataService<Project> genericDataService, IActualProject actualProject, IRenavigator renavigator)
{
Projekte = new ObservableCollection<Project>();
this.genericDataService = genericDataService;
this.genericDataService = (genericDataService as ProjectDataService);
this.actualProject = actualProject;
this.renavigator = renavigator;
LoadProjecte();
}
public async void LoadProjecte()
{
var projekte = await genericDataService.GetAll();
var projekte = await genericDataService.GetAllByClient(actualProject.AktuellClient);
//
InitCollection(Projekte, projekte);

View File

@@ -23,6 +23,9 @@ namespace DaSaSo.ViewModel
private string _clientname = "";
private string _projektname = "";
private string _buildingsitename = "";
public bool CanSelectProject { get => _actualProject.AktuellClient != null; }
public bool CanSelectBuildingSite { get => _actualProject.AktuellProjekt != null; }
public bool CanSelectSewerObjects { get => _actualProject.AktuellBaustelle != null; }
public INavigator Navigator { get; set; }
public ICommand UpdateCurrentViewModelCommand { get; }
@@ -80,16 +83,19 @@ namespace DaSaSo.ViewModel
private void _actualProject_BuildingSiteChanged(object? sender, EventArgs e)
{
Buildingsitename = _actualProject.AktuellBaustelle.BuildingSiteNumber;
OnPropertyChanged(nameof(CanSelectSewerObjects));
}
private void _actualProject_ProjectChanged(object? sender, EventArgs e)
{
Projektname = _actualProject.AktuellProjekt.Name;
OnPropertyChanged(nameof(CanSelectBuildingSite));
}
private void _actualProject_ClientChanged(object? sender, EventArgs e)
{
ClientName = _actualProject.AktuellClient.Firstname;
OnPropertyChanged(nameof(CanSelectProject));
}
}
}

View File

@@ -7,7 +7,9 @@ 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;
@@ -18,42 +20,28 @@ namespace DaSaSo.Wpf
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
private readonly IHost _host;
public App()
{
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);
_host = CreateHostBuilder().Build();
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
public static IHostBuilder CreateHostBuilder(string[]? args = null)
{
try
return Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(c=>
{
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)
c.AddJsonFile("appsettings.json");
c.AddEnvironmentVariables();
})
.ConfigureServices((context, services) =>
{
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();
}
string connectionString = context.Configuration.GetConnectionString("default");
private IServiceProvider CreateServiceProvider()
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton<DaSaSoDbContextFactory>();
services.AddSingleton<DaSaSoDbContextFactory>(new DaSaSoDbContextFactory(connectionString));
services.AddSingleton<IDataService<Client>, ClientDataService>();
services.AddSingleton<IDataService<Project>, ProjectDataService>();
services.AddSingleton<ClientListViewModel>();
@@ -82,15 +70,60 @@ namespace DaSaSo.Wpf
));
});
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>();
return services.BuildServiceProvider();
});
}
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();
}
}
}

View File

@@ -4,7 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DaSaSo.Wpf.Controls"
xmlns:nav="clr-namespace:DaSaSo.ViewModel.Enums;assembly=DaSaSo.ViewModel"
xmlns:nav="clr-namespace:DaSaSo.ViewModel.Enums;assembly=DaSaSo.ViewModel" xmlns:viewmodel="clr-namespace:DaSaSo.ViewModel;assembly=DaSaSo.ViewModel" d:DataContext="{d:DesignInstance Type=viewmodel:MainWindowViewModel}"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
@@ -14,9 +14,9 @@
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<RadioButton Grid.Row="0" Content="Kunden" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateCurrentViewModelCommand}" CommandParameter="{x:Static nav:EViewType.Clients}" />
<RadioButton Grid.Row="1" Content="Projekte" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateCurrentViewModelCommand}" CommandParameter="{x:Static nav:EViewType.ClientEdit}" />
<RadioButton Grid.Row="2" Content="Baustellen" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateCurrentViewModelCommand}" CommandParameter="{x:Static nav:EViewType.Buildingsites}" />
<RadioButton Grid.Row="3" Content="Objekten" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateCurrentViewModelCommand}" CommandParameter="{x:Static nav:EViewType.SewerObjects}" />
<RadioButton Grid.Row="0" Content="Kunden" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateCurrentViewModelCommand}" IsEnabled="True" CommandParameter="{x:Static nav:EViewType.Clients}" />
<RadioButton Grid.Row="1" Content="Projekte" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateCurrentViewModelCommand}" IsEnabled="{Binding CanSelectProject}" CommandParameter="{x:Static nav:EViewType.Projects}" />
<RadioButton Grid.Row="2" Content="Baustellen" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateCurrentViewModelCommand}" IsEnabled="{Binding CanSelectBuildingSite}" CommandParameter="{x:Static nav:EViewType.Buildingsites}" />
<RadioButton Grid.Row="3" Content="Objekten" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateCurrentViewModelCommand}" IsEnabled="{Binding CanSelectSewerObjects}" CommandParameter="{x:Static nav:EViewType.SewerObjects}" />
</Grid>
</UserControl>

View File

@@ -7,9 +7,19 @@
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0-rc.1.21451.13" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DaSaSo.Domain\DaSaSo.Domain.csproj" />
<ProjectReference Include="..\DaSaSo.ViewModel\DaSaSo.ViewModel.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,5 @@
{
"ConnectionStrings": {
"default": "Host = localhoste; Database = dasaso; Username = kansan; Password = kansan"
}
}