Client wird gespeichert mittels interface

This commit is contained in:
HuskyTeufel
2021-09-14 19:08:55 +02:00
parent 8eccf7c478
commit bbffb270bc
13 changed files with 149 additions and 70 deletions

View File

@@ -1,5 +1,6 @@
using DaSaSo.Domain.Model; using DaSaSo.Domain.Model;
using DaSaSo.Domain.Services; using DaSaSo.Domain.Services;
using DaSaSo.ViewModel.Interface;
using Microsoft.Toolkit.Mvvm.Input; using Microsoft.Toolkit.Mvvm.Input;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -14,17 +15,19 @@ namespace DaSaSo.ViewModel
{ {
private Client _model; private Client _model;
private IDataService<Client> _dataService; private IDataService<Client> _dataService;
private readonly IActualProject _actualProject;
public Client Model { get => _model; set => _model = value; } public Client Model { get => _model; set => _model = value; }
public IRelayCommand SaveClientCommand { get; set; } public IRelayCommand SaveClientCommand { get; set; }
public ClientEditViewModel(IDataService<Client> dataService, Client model) public ClientEditViewModel(IDataService<Client> dataService, IActualProject actualProject)
{ {
this._model = model;
this._dataService = dataService; this._dataService = dataService;
SaveClientCommand = new RelayCommand(SaveClient); SaveClientCommand = new RelayCommand(SaveClient);
_actualProject = actualProject;
this._model = _actualProject.AktuellClient;
} }
private void SaveClient() private void SaveClient()

View File

@@ -1,5 +1,7 @@
using DaSaSo.Domain.Model; using DaSaSo.Domain.Model;
using DaSaSo.Domain.Services; using DaSaSo.Domain.Services;
using DaSaSo.ViewModel.Commands;
using DaSaSo.ViewModel.Interface;
using Microsoft.Toolkit.Mvvm.Input; using Microsoft.Toolkit.Mvvm.Input;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -20,7 +22,7 @@ namespace DaSaSo.ViewModel
IEnumerable<Client>? result; IEnumerable<Client>? result;
bool _isLoading = true; bool _isLoading = true;
public IRelayCommand SelectClientCommand { get; set; } public ICommand SelectCommand { get; set; }
public IRelayCommand EditClientCommand { get; set; } public IRelayCommand EditClientCommand { get; set; }
public IRelayCommand AddNewClientCommand { get; set; } public IRelayCommand AddNewClientCommand { get; set; }
@@ -32,8 +34,8 @@ namespace DaSaSo.ViewModel
if(_selectedClient != value) if(_selectedClient != value)
{ {
_selectedClient = value; _selectedClient = value;
SelectClientCommand.NotifyCanExecuteChanged(); //SelectClientCommand.NotifyCanExecuteChanged();
EditClientCommand.NotifyCanExecuteChanged(); //EditClientCommand.NotifyCanExecuteChanged();
OnPropertyChanged(); OnPropertyChanged();
} }
} }
@@ -52,16 +54,16 @@ namespace DaSaSo.ViewModel
} }
} }
public ClientListViewModel(IDataService<Client> dataService) public ClientListViewModel(IDataService<Client> dataService, IActualProject actualProject)
{ {
Clients = new ObservableCollection<Client>(); Clients = new ObservableCollection<Client>();
_dataService = dataService; _dataService = dataService;
LoadClient(); LoadClient();
SelectClientCommand = new RelayCommand(SelectClient, () => SelectedClient != null); SelectCommand = new SelectClientCommand(actualProject,this); //= new RelayCommand(SelectClient, () => SelectedClient != null);
EditClientCommand = new RelayCommand(EditClient, () => SelectedClient != null); //EditClientCommand = new RelayCommand(EditClient, () => SelectedClient != null);
AddNewClientCommand = new RelayCommand(AddNewClient); //AddNewClientCommand = new RelayCommand(AddNewClient);
} }
@@ -93,6 +95,7 @@ namespace DaSaSo.ViewModel
private void SelectClient() private void SelectClient()
{ {
Mediator.Notify(Enums.EMediator.SELECTEDCLIENT, SelectedClient); Mediator.Notify(Enums.EMediator.SELECTEDCLIENT, SelectedClient);
} }

View File

@@ -0,0 +1,33 @@
using DaSaSo.ViewModel.Interface;
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 class SelectClientCommand : ICommand
{
public event EventHandler? CanExecuteChanged;
private readonly IActualProject _actualProject;
private readonly ClientListViewModel _clientListViewModel;
public SelectClientCommand(IActualProject actualProject, ClientListViewModel clientListViewModel)
{
_actualProject = actualProject;
_clientListViewModel = clientListViewModel;
}
public bool CanExecute(object? parameter)
{
return true;
}
public void Execute(object? parameter)
{
var s = _clientListViewModel.SelectedClient;
_actualProject.SetClient(s);
}
}
}

View File

@@ -10,6 +10,7 @@ namespace DaSaSo.ViewModel.Enums
{ {
Home, Home,
Clients, Clients,
ClientEdit,
Projects, Projects,
Buildingsites, Buildingsites,
SewerObjects SewerObjects

View File

@@ -0,0 +1,27 @@
using DaSaSo.Domain.Model;
using DaSaSo.EntityFramework;
using DaSaSo.EntityFramework.Services;
using DaSaSo.ViewModel.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DaSaSo.ViewModel.Factories
{
public class ClientEditViewModelFactory : IViewModelFactory<ClientEditViewModel>
{
private readonly IActualProject _actualProject;
public ClientEditViewModelFactory(IActualProject actualProject)
{
_actualProject = actualProject;
}
public ClientEditViewModel CreateViewModel()
{
return new ClientEditViewModel(new GenericDataService<Client>(new DaSaSoDbContextFactory()), _actualProject);
}
}
}

View File

@@ -12,9 +12,16 @@ namespace DaSaSo.ViewModel.Factories
{ {
public class ClientListViewModelFactory : IViewModelFactory<ClientListViewModel> public class ClientListViewModelFactory : IViewModelFactory<ClientListViewModel>
{ {
private readonly IActualProject _actualProject;
public ClientListViewModelFactory(IActualProject actualProject)
{
_actualProject = actualProject;
}
public ClientListViewModel CreateViewModel() public ClientListViewModel CreateViewModel()
{ {
return new ClientListViewModel(new GenericDataService<Client>(new DaSaSoDbContextFactory())); return new ClientListViewModel(new GenericDataService<Client>(new DaSaSoDbContextFactory()),_actualProject);
} }
} }
} }

View File

@@ -14,12 +14,14 @@ namespace DaSaSo.ViewModel.Factories
public class ViewModelAbstractFactory : IViewModelAbstractFactory public class ViewModelAbstractFactory : IViewModelAbstractFactory
{ {
private IViewModelFactory<HomeViewModel> _homeViewModelFactory; private IViewModelFactory<HomeViewModel> _homeViewModelFactory;
private IViewModelFactory<ClientListViewModel> _clientListViewModel; private IViewModelFactory<ClientListViewModel> _clientListViewModelFactory;
private IViewModelFactory<ClientEditViewModel> _clientEditViewModelFactory;
public ViewModelAbstractFactory(IViewModelFactory<HomeViewModel> homeViewModelFactory, IViewModelFactory<ClientListViewModel> clientListViewModel) public ViewModelAbstractFactory(IViewModelFactory<HomeViewModel> homeViewModelFactory, IViewModelFactory<ClientListViewModel> clientListViewModel, IViewModelFactory<ClientEditViewModel> clientEditViewModel)
{ {
_homeViewModelFactory = homeViewModelFactory; _homeViewModelFactory = homeViewModelFactory;
_clientListViewModel = clientListViewModel; _clientListViewModelFactory = clientListViewModel;
_clientEditViewModelFactory = clientEditViewModel;
} }
public BaseViewModel CreateViewModel(EViewType viewType) public BaseViewModel CreateViewModel(EViewType viewType)
@@ -30,7 +32,9 @@ namespace DaSaSo.ViewModel.Factories
case EViewType.Home: case EViewType.Home:
return _homeViewModelFactory.CreateViewModel(); return _homeViewModelFactory.CreateViewModel();
case EViewType.Clients: case EViewType.Clients:
return _clientListViewModel.CreateViewModel(); return _clientListViewModelFactory.CreateViewModel();
case EViewType.ClientEdit:
return _clientEditViewModelFactory.CreateViewModel();
/*case EViewType.Projects: /*case EViewType.Projects:
break; break;
case EViewType.Buildingsites: case EViewType.Buildingsites:

View File

@@ -0,0 +1,18 @@
using DaSaSo.Domain.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DaSaSo.ViewModel.Interface
{
public interface IActualProject
{
Client AktuellClient { get; }
Project AktuellProjekt { get; }
Buildingsite AktuellBaustelle { get; }
void SetClient(Client client);
}
}

View File

@@ -0,0 +1,24 @@
using DaSaSo.Domain.Model;
using DaSaSo.ViewModel.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DaSaSo.ViewModel.State.ActualState
{
public class ActualProject : IActualProject
{
public Client AktuellClient { get; private set; }
public Buildingsite AktuellBaustelle { get; private set; }
public Project AktuellProjekt { get; private set; }
public void SetClient(Client client)
{
AktuellClient = client;
}
}
}

View File

@@ -17,44 +17,22 @@ namespace DaSaSo.ViewModel
{ {
public sealed class MainWindowViewModel : BaseViewModel public sealed class MainWindowViewModel : BaseViewModel
{ {
private BaseViewModel _actualViewModel;
private Client _selectedClient; private Client _selectedClient;
private Project _selectedProject; private Project _selectedProject;
private Buildingsite _selectedBuildingsite; private Buildingsite _selectedBuildingsite;
public INavigator Navigator { get; set; } public INavigator Navigator { get; set; }
public IRelayCommand ListClientsCommand { get; set; }
public IRelayCommand ListProjectCommand { get; set; }
public IRelayCommand ListBuildingsiteCommand { get; set; }
public IRelayCommand ListSewerObjectsCommand { get; set; }
public BaseViewModel ActualViewModel
{
get => _actualViewModel;
set
{
if(_actualViewModel != value)
{
_actualViewModel = value;
OnPropertyChanged();
}
}
}
public Project SelectedProject public Project SelectedProject
{ {
get => _selectedProject; get => _selectedProject;
set set
{ {
if(value == null) if(_selectedProject != value)
{
ListBuildingsiteCommand.NotifyCanExecuteChanged();
}
if(_selectedProject != value && value != null)
{ {
_selectedProject = value; _selectedProject = value;
OnPropertyChanged(); OnPropertyChanged();
ListBuildingsiteCommand.NotifyCanExecuteChanged();
} }
} }
} }
@@ -63,15 +41,10 @@ namespace DaSaSo.ViewModel
get => _selectedBuildingsite; get => _selectedBuildingsite;
set set
{ {
if(value == null) if(_selectedBuildingsite != value)
{
ListSewerObjectsCommand.NotifyCanExecuteChanged();
}
if(_selectedBuildingsite != value && value != null)
{ {
_selectedBuildingsite = value; _selectedBuildingsite = value;
OnPropertyChanged(); OnPropertyChanged();
ListSewerObjectsCommand.NotifyCanExecuteChanged();
} }
} }
} }
@@ -84,9 +57,8 @@ namespace DaSaSo.ViewModel
if(_selectedClient != value) if(_selectedClient != value)
{ {
_selectedClient = value; _selectedClient = value;
SelectedProject = null; //SelectedProject = null;
OnPropertyChanged(); OnPropertyChanged();
ListProjectCommand.NotifyCanExecuteChanged();
} }
} }
} }
@@ -94,10 +66,7 @@ namespace DaSaSo.ViewModel
{ {
this.Navigator = navigator; this.Navigator = navigator;
Navigator.UpdateViewModelCommand.Execute(EViewType.Home); Navigator.UpdateViewModelCommand.Execute(EViewType.Home);
ListClientsCommand = new RelayCommand(showClients);
ListProjectCommand = new RelayCommand(listProjecte, () => SelectedClient != null);
ListBuildingsiteCommand = new RelayCommand(listBuildingsite, () => SelectedProject != null);
ListSewerObjectsCommand = new RelayCommand(listSewerObjects, () => SelectedBuildingsite != null);
Mediator.Subscribe(Enums.EMediator.SELECTEDCLIENT, (tt) => Mediator.Subscribe(Enums.EMediator.SELECTEDCLIENT, (tt) =>
{ {
@@ -107,20 +76,14 @@ namespace DaSaSo.ViewModel
Mediator.Subscribe(Enums.EMediator.EDITCLIENT, (tt) => Mediator.Subscribe(Enums.EMediator.EDITCLIENT, (tt) =>
{ {
ActualViewModel = new ClientEditViewModel(new GenericDataService<Client>(new DaSaSoDbContextFactory()),
(Client)tt);
}); });
Mediator.Subscribe(Enums.EMediator.SHOWCLIENT, (tt) => { Mediator.Subscribe(Enums.EMediator.SHOWCLIENT, (tt) => {
ActualViewModel = null;
}); });
} }
private void showClients()
{
ClientListViewModel clientListViewModel = new ClientListViewModel(new GenericDataService<Client>(new DaSaSoDbContextFactory()));
ActualViewModel = clientListViewModel;
clientListViewModel.LoadClient();
}
private void listSewerObjects() private void listSewerObjects()
{ {
@@ -134,8 +97,7 @@ namespace DaSaSo.ViewModel
private void listProjecte() private void listProjecte()
{ {
Debugger.Break();
ActualViewModel = new ProjectListViewModel(new GenericDataService<Project>(new DaSaSoDbContextFactory()),SelectedClient);
} }
} }
} }

View File

@@ -1,19 +1,14 @@
using DaSaSo.Domain.Model; using DaSaSo.Domain.Model;
using DaSaSo.Domain.Services; using DaSaSo.Domain.Services;
using DaSaSo.Domain.Services.ClientServices;
using DaSaSo.EntityFramework; using DaSaSo.EntityFramework;
using DaSaSo.EntityFramework.Services; using DaSaSo.EntityFramework.Services;
using DaSaSo.ViewModel; using DaSaSo.ViewModel;
using DaSaSo.ViewModel.Factories; using DaSaSo.ViewModel.Factories;
using DaSaSo.ViewModel.Interface; using DaSaSo.ViewModel.Interface;
using DaSaSo.ViewModel.State.ActualState;
using DaSaSo.ViewModel.State.Navigation; using DaSaSo.ViewModel.State.Navigation;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System; using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
namespace DaSaSo.Wpf namespace DaSaSo.Wpf
@@ -23,7 +18,7 @@ namespace DaSaSo.Wpf
/// </summary> /// </summary>
public partial class App : Application public partial class App : Application
{ {
protected override async void OnStartup(StartupEventArgs e) protected override void OnStartup(StartupEventArgs e)
{ {
IServiceProvider serviceProvider = CreateServiceProvider(); IServiceProvider serviceProvider = CreateServiceProvider();
@@ -43,6 +38,8 @@ namespace DaSaSo.Wpf
services.AddSingleton<IViewModelAbstractFactory, ViewModelAbstractFactory>(); services.AddSingleton<IViewModelAbstractFactory, ViewModelAbstractFactory>();
services.AddSingleton<IViewModelFactory<HomeViewModel>, HomeViewModelFactory>(); services.AddSingleton<IViewModelFactory<HomeViewModel>, HomeViewModelFactory>();
services.AddSingleton<IViewModelFactory<ClientListViewModel>, ClientListViewModelFactory>(); services.AddSingleton<IViewModelFactory<ClientListViewModel>, ClientListViewModelFactory>();
services.AddSingleton<IViewModelFactory<ClientEditViewModel>, ClientEditViewModelFactory>();
services.AddScoped<IActualProject, ActualProject>();
services.AddScoped<INavigator, Navigator>(); services.AddScoped<INavigator, Navigator>();
services.AddScoped<MainWindowViewModel>(); services.AddScoped<MainWindowViewModel>();

View File

@@ -15,7 +15,7 @@
<RowDefinition Height="auto" /> <RowDefinition Height="auto" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<RadioButton Grid.Row="0" Content="Kunden" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateViewModelCommand}" CommandParameter="{x:Static nav:EViewType.Clients}" /> <RadioButton Grid.Row="0" Content="Kunden" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateViewModelCommand}" CommandParameter="{x:Static nav:EViewType.Clients}" />
<RadioButton Grid.Row="1" Content="Projekte" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateViewModelCommand}" CommandParameter="{x:Static nav:EViewType.Projects}" /> <RadioButton Grid.Row="1" Content="Projekte" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateViewModelCommand}" CommandParameter="{x:Static nav:EViewType.ClientEdit}" />
<RadioButton Grid.Row="2" Content="Baustellen" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateViewModelCommand}" CommandParameter="{x:Static nav:EViewType.Buildingsites}" /> <RadioButton Grid.Row="2" Content="Baustellen" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateViewModelCommand}" CommandParameter="{x:Static nav:EViewType.Buildingsites}" />
<RadioButton Grid.Row="3" Content="Objekten" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateViewModelCommand}" CommandParameter="{x:Static nav:EViewType.SewerObjects}" /> <RadioButton Grid.Row="3" Content="Objekten" Style="{StaticResource ToggleButtonList}" Command="{Binding UpdateViewModelCommand}" CommandParameter="{x:Static nav:EViewType.SewerObjects}" />
</Grid> </Grid>

View File

@@ -13,7 +13,7 @@
<StackPanel> <StackPanel>
<TextBlock Text="Kundenliste" /> <TextBlock Text="Kundenliste" />
<ListView ItemsSource="{Binding Clients}" DisplayMemberPath="Firstname" SelectedItem="{Binding SelectedClient, Mode=TwoWay}"/> <ListView ItemsSource="{Binding Clients}" DisplayMemberPath="Firstname" SelectedItem="{Binding SelectedClient, Mode=TwoWay}"/>
<Button Content="Kunde auswählen" Command="{Binding SelectClientCommand}" /> <Button Content="Kunde auswählen" Command="{Binding SelectCommand}" />
<Button Content="Kunde Editieren" Command="{Binding EditClientCommand}"/> <Button Content="Kunde Editieren" Command="{Binding EditClientCommand}"/>
<Button Content="Kunde Hinzufügen" Command="{Binding AddNewClientCommand}" /> <Button Content="Kunde Hinzufügen" Command="{Binding AddNewClientCommand}" />
</StackPanel> </StackPanel>