diff --git a/DaSaSo.Domain/DaSaSo.Domain.csproj b/DaSaSo.Domain/DaSaSo.Domain.csproj index 4de8048..2b40fef 100644 --- a/DaSaSo.Domain/DaSaSo.Domain.csproj +++ b/DaSaSo.Domain/DaSaSo.Domain.csproj @@ -2,7 +2,7 @@ net6.0 - enable + disable diff --git a/DaSaSo.EntityFramework/Services/ClientDataService.cs b/DaSaSo.EntityFramework/Services/ClientDataService.cs index 28f4c64..6bc3dd0 100644 --- a/DaSaSo.EntityFramework/Services/ClientDataService.cs +++ b/DaSaSo.EntityFramework/Services/ClientDataService.cs @@ -1,5 +1,6 @@ using DaSaSo.Domain.Model; using DaSaSo.Domain.Services; +using DaSaSo.EntityFramework.Services.Common; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using System; @@ -13,31 +14,22 @@ namespace DaSaSo.EntityFramework.Services public class ClientDataService : IDataService { private readonly DaSaSoDbContextFactory _contextFactory; + private readonly NonQueryDataService _nonQueryDataService; public ClientDataService(DaSaSoDbContextFactory contextFactory) { _contextFactory = contextFactory; + _nonQueryDataService = new NonQueryDataService(contextFactory); } public async Task Create(Client entity) { - using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) - { - EntityEntry createdResult = await context.Set().AddAsync(entity); - await context.SaveChangesAsync(); - return createdResult.Entity; - } + return await _nonQueryDataService.Create(entity); } public async Task Delete(int id) { - using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) - { - Client entity = await context.Set().FirstOrDefaultAsync((e) => e.Id == id); - context.Set().Remove(entity); - await context.SaveChangesAsync(); - return true; - } + return await _nonQueryDataService.Delete(id); } public async Task Get(int id) @@ -54,7 +46,7 @@ namespace DaSaSo.EntityFramework.Services { using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) { - IEnumerable entities = await context.Set().ToListAsync(); + IEnumerable entities = await context.Clients.ToListAsync(); return entities; } @@ -62,13 +54,7 @@ namespace DaSaSo.EntityFramework.Services public async Task Update(int id, Client entity) { - using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) - { - entity.Id = id; - context.Set().Update(entity); - await context.SaveChangesAsync(); - return entity; - } + return await _nonQueryDataService.Update(id, entity); } } } diff --git a/DaSaSo.EntityFramework/Services/Common/NonQueryDataService.cs b/DaSaSo.EntityFramework/Services/Common/NonQueryDataService.cs new file mode 100644 index 0000000..b997fae --- /dev/null +++ b/DaSaSo.EntityFramework/Services/Common/NonQueryDataService.cs @@ -0,0 +1,51 @@ +using DaSaSo.Domain.Model; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DaSaSo.EntityFramework.Services.Common +{ + class NonQueryDataService where T: DomainObject + { + private readonly DaSaSoDbContextFactory _contextFactory; + + public NonQueryDataService(DaSaSoDbContextFactory contextFactory) + { + _contextFactory = contextFactory; + } + public async Task Create(T entity) + { + using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) + { + EntityEntry createdEntity = await context.Set().AddAsync(entity); + await context.SaveChangesAsync(); + return createdEntity.Entity; + } + } + + public async Task Delete(int id) + { + using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) + { + T entity = await context.Set().FirstOrDefaultAsync((e) => e.Id == id); + context.Set().Remove(entity); + await context.SaveChangesAsync(); + return true; + } + } + public async Task Update(int id, T entity) + { + using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) + { + entity.Id = id; + context.Set().Update(entity); + await context.SaveChangesAsync(); + return entity; + } + } + } +} diff --git a/DaSaSo.EntityFramework/Services/GenericDataService.cs b/DaSaSo.EntityFramework/Services/GenericDataService.cs index a0af62c..4b7f251 100644 --- a/DaSaSo.EntityFramework/Services/GenericDataService.cs +++ b/DaSaSo.EntityFramework/Services/GenericDataService.cs @@ -1,5 +1,6 @@ using DaSaSo.Domain.Model; using DaSaSo.Domain.Services; +using DaSaSo.EntityFramework.Services.Common; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using System; @@ -13,31 +14,22 @@ namespace DaSaSo.EntityFramework.Services public class GenericDataService : IDataService where T : DomainObject { private readonly DaSaSoDbContextFactory _contextFactory; + private readonly NonQueryDataService _nonQueryDataService; public GenericDataService(DaSaSoDbContextFactory contextFactory) { this._contextFactory = contextFactory; + _nonQueryDataService = new NonQueryDataService(contextFactory); } public async Task Create(T entity) { - using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) - { - EntityEntry createdEntity = await context.Set().AddAsync(entity); - await context.SaveChangesAsync(); - return createdEntity.Entity; - } + return await _nonQueryDataService.Create(entity); } public async Task Delete(int id) { - using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) - { - T entity = await context.Set().FirstOrDefaultAsync((e) => e.Id == id); - context.Set().Remove(entity); - await context.SaveChangesAsync(); - return true; - } + return await _nonQueryDataService.Delete(id); } public async Task Get(int id) @@ -62,13 +54,7 @@ namespace DaSaSo.EntityFramework.Services public async Task Update(int id, T entity) { - using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) - { - entity.Id = id; - context.Set().Update(entity); - await context.SaveChangesAsync(); - return entity; - } + return await _nonQueryDataService.Update(id, entity); } } } diff --git a/DaSaSo.ViewModel/ClientListViewModel.cs b/DaSaSo.ViewModel/ClientListViewModel.cs index ea6241e..1f2b626 100644 --- a/DaSaSo.ViewModel/ClientListViewModel.cs +++ b/DaSaSo.ViewModel/ClientListViewModel.cs @@ -58,7 +58,7 @@ namespace DaSaSo.ViewModel _dataService = dataService; - //LoadClient(); + LoadClient(); SelectClientCommand = new RelayCommand(SelectClient, () => SelectedClient != null); EditClientCommand = new RelayCommand(EditClient, () => SelectedClient != null); AddNewClientCommand = new RelayCommand(AddNewClient); diff --git a/DaSaSo.ViewModel/Commands/UpdateCurrentViewModelCommand.cs b/DaSaSo.ViewModel/Commands/UpdateCurrentViewModelCommand.cs new file mode 100644 index 0000000..5c55c34 --- /dev/null +++ b/DaSaSo.ViewModel/Commands/UpdateCurrentViewModelCommand.cs @@ -0,0 +1,33 @@ +using DaSaSo.ViewModel.Enums; +using DaSaSo.ViewModel.Interface; +using System.Windows.Input; + +namespace DaSaSo.ViewModel.Commands +{ + class UpdateCurrentViewModelCommand : ICommand + { + public event EventHandler? CanExecuteChanged; + private INavigator _navigator; + private readonly IViewModelAbstractFactory _viewModelFactory; + + public UpdateCurrentViewModelCommand(INavigator navigator, IViewModelAbstractFactory viewModelFactory) + { + _navigator = navigator; + _viewModelFactory = viewModelFactory; + } + + public bool CanExecute(object? parameter) + { + return true; + } + + public void Execute(object? parameter) + { + if(parameter is EViewType) + { + EViewType viewType = (EViewType)parameter; + _navigator.CurrentViewModel = _viewModelFactory.CreateViewModel(viewType); + } + } + } +} \ No newline at end of file diff --git a/DaSaSo.ViewModel/Enums/EViewType.cs b/DaSaSo.ViewModel/Enums/EViewType.cs new file mode 100644 index 0000000..87de7db --- /dev/null +++ b/DaSaSo.ViewModel/Enums/EViewType.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DaSaSo.ViewModel.Enums +{ + public enum EViewType + { + Home, + Clients, + Projects, + Buildingsites, + SewerObjects + } +} diff --git a/DaSaSo.ViewModel/Factories/ClientListViewModelFactory.cs b/DaSaSo.ViewModel/Factories/ClientListViewModelFactory.cs new file mode 100644 index 0000000..129fe91 --- /dev/null +++ b/DaSaSo.ViewModel/Factories/ClientListViewModelFactory.cs @@ -0,0 +1,20 @@ +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 ClientListViewModelFactory : IViewModelFactory + { + public ClientListViewModel CreateViewModel() + { + return new ClientListViewModel(new GenericDataService(new DaSaSoDbContextFactory())); + } + } +} diff --git a/DaSaSo.ViewModel/Factories/HomeViewModelFactory.cs b/DaSaSo.ViewModel/Factories/HomeViewModelFactory.cs new file mode 100644 index 0000000..7f8b4f1 --- /dev/null +++ b/DaSaSo.ViewModel/Factories/HomeViewModelFactory.cs @@ -0,0 +1,17 @@ +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 HomeViewModelFactory : IViewModelFactory + { + public HomeViewModel CreateViewModel() + { + return new HomeViewModel(); + } + } +} diff --git a/DaSaSo.ViewModel/Factories/ViewModelAbstractFactory.cs b/DaSaSo.ViewModel/Factories/ViewModelAbstractFactory.cs new file mode 100644 index 0000000..d9d3cac --- /dev/null +++ b/DaSaSo.ViewModel/Factories/ViewModelAbstractFactory.cs @@ -0,0 +1,46 @@ +using DaSaSo.Domain.Model; +using DaSaSo.EntityFramework; +using DaSaSo.EntityFramework.Services; +using DaSaSo.ViewModel.Enums; +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 ViewModelAbstractFactory : IViewModelAbstractFactory + { + private IViewModelFactory _homeViewModelFactory; + private IViewModelFactory _clientListViewModel; + + public ViewModelAbstractFactory(IViewModelFactory homeViewModelFactory, IViewModelFactory clientListViewModel) + { + _homeViewModelFactory = homeViewModelFactory; + _clientListViewModel = clientListViewModel; + } + + public BaseViewModel CreateViewModel(EViewType viewType) + { + + switch (viewType) + { + case EViewType.Home: + return _homeViewModelFactory.CreateViewModel(); + case EViewType.Clients: + return _clientListViewModel.CreateViewModel(); + /*case EViewType.Projects: + break; + case EViewType.Buildingsites: + break; + case EViewType.SewerObjects: + break; + */ + default: + throw new ArgumentException("The Viewtype dos not have a ViewModel.", "viewType"); + } + } + } +} diff --git a/DaSaSo.ViewModel/HomeViewModel.cs b/DaSaSo.ViewModel/HomeViewModel.cs new file mode 100644 index 0000000..e1573d8 --- /dev/null +++ b/DaSaSo.ViewModel/HomeViewModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DaSaSo.ViewModel +{ + public class HomeViewModel : BaseViewModel + { + public string Welcome { get => "Herzlich willkommen"; } + } +} diff --git a/DaSaSo.ViewModel/Interface/INavigator.cs b/DaSaSo.ViewModel/Interface/INavigator.cs new file mode 100644 index 0000000..7c4f1f2 --- /dev/null +++ b/DaSaSo.ViewModel/Interface/INavigator.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; + +namespace DaSaSo.ViewModel.Interface +{ + public interface INavigator + { + BaseViewModel CurrentViewModel { get; set; } + ICommand UpdateViewModelCommand { get; } + } +} diff --git a/DaSaSo.ViewModel/Interface/IViewModelAbstractFactory.cs b/DaSaSo.ViewModel/Interface/IViewModelAbstractFactory.cs new file mode 100644 index 0000000..d88466f --- /dev/null +++ b/DaSaSo.ViewModel/Interface/IViewModelAbstractFactory.cs @@ -0,0 +1,14 @@ +using DaSaSo.ViewModel.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DaSaSo.ViewModel.Interface +{ + public interface IViewModelAbstractFactory + { + BaseViewModel CreateViewModel(EViewType viewType); + } +} diff --git a/DaSaSo.ViewModel/Interface/IViewModelFactory.cs b/DaSaSo.ViewModel/Interface/IViewModelFactory.cs new file mode 100644 index 0000000..e359657 --- /dev/null +++ b/DaSaSo.ViewModel/Interface/IViewModelFactory.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DaSaSo.ViewModel.Interface +{ + public interface IViewModelFactory where T: BaseViewModel + { + T CreateViewModel(); + } +} diff --git a/DaSaSo.ViewModel/State/Navigation/Navigator.cs b/DaSaSo.ViewModel/State/Navigation/Navigator.cs new file mode 100644 index 0000000..cde24ea --- /dev/null +++ b/DaSaSo.ViewModel/State/Navigation/Navigator.cs @@ -0,0 +1,35 @@ +using DaSaSo.ViewModel.Commands; +using DaSaSo.ViewModel.Interface; +using System.ComponentModel; +using System.Runtime.CompilerServices; +using System.Windows.Input; + +namespace DaSaSo.ViewModel.State.Navigation +{ + public class Navigator : INavigator, INotifyPropertyChanged + { + public event PropertyChangedEventHandler? PropertyChanged; + private BaseViewModel _currentViewModel; + public BaseViewModel CurrentViewModel + { + get => _currentViewModel; + set + { + _currentViewModel = value; + OnPropertyChanged(); + } + } + + public ICommand UpdateViewModelCommand { get; set; } + + public Navigator(IViewModelAbstractFactory viewModelFactory) + { + UpdateViewModelCommand = new UpdateCurrentViewModelCommand(this, viewModelFactory); + } + + protected void OnPropertyChanged([CallerMemberName]string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + } +} diff --git a/DaSaSo.ViewModel/Window/MainWindowViewModel.cs b/DaSaSo.ViewModel/Window/MainWindowViewModel.cs index 6c1dd7e..0194f0f 100644 --- a/DaSaSo.ViewModel/Window/MainWindowViewModel.cs +++ b/DaSaSo.ViewModel/Window/MainWindowViewModel.cs @@ -1,6 +1,9 @@ using DaSaSo.Domain.Model; using DaSaSo.EntityFramework; using DaSaSo.EntityFramework.Services; +using DaSaSo.ViewModel.Enums; +using DaSaSo.ViewModel.Interface; +using DaSaSo.ViewModel.State.Navigation; using Microsoft.Toolkit.Mvvm.Input; using System; using System.Collections.Generic; @@ -18,6 +21,8 @@ namespace DaSaSo.ViewModel private Client _selectedClient; private Project _selectedProject; private Buildingsite _selectedBuildingsite; + + public INavigator Navigator { get; set; } public IRelayCommand ListClientsCommand { get; set; } public IRelayCommand ListProjectCommand { get; set; } @@ -85,8 +90,10 @@ namespace DaSaSo.ViewModel } } } - public MainWindowViewModel() + public MainWindowViewModel(INavigator navigator) { + this.Navigator = navigator; + Navigator.UpdateViewModelCommand.Execute(EViewType.Home); ListClientsCommand = new RelayCommand(showClients); ListProjectCommand = new RelayCommand(listProjecte, () => SelectedClient != null); ListBuildingsiteCommand = new RelayCommand(listBuildingsite, () => SelectedProject != null); diff --git a/DaSaSo.Wpf/App.xaml.cs b/DaSaSo.Wpf/App.xaml.cs index 78cb819..f41a6d1 100644 --- a/DaSaSo.Wpf/App.xaml.cs +++ b/DaSaSo.Wpf/App.xaml.cs @@ -4,6 +4,9 @@ using DaSaSo.Domain.Services.ClientServices; using DaSaSo.EntityFramework; using DaSaSo.EntityFramework.Services; using DaSaSo.ViewModel; +using DaSaSo.ViewModel.Factories; +using DaSaSo.ViewModel.Interface; +using DaSaSo.ViewModel.State.Navigation; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; @@ -23,19 +26,27 @@ namespace DaSaSo.Wpf protected override async void OnStartup(StartupEventArgs e) { IServiceProvider serviceProvider = CreateServiceProvider(); - IDataService clientService = new ClientDataService(new DaSaSoDbContextFactory()); - var d = await clientService.GetAll(); - base.OnStartup(e); - MainWindow? window = new MainWindow() { DataContext = new MainWindowViewModel() }; + + MainWindow? window = new MainWindow() { DataContext = serviceProvider.GetRequiredService() }; window.Show(); + base.OnStartup(e); } private IServiceProvider CreateServiceProvider() { IServiceCollection services = new ServiceCollection(); - services.AddSingleton(); - //services.AddSingleton, ClientDataService>(); + services.AddSingleton(); + services.AddSingleton, ClientDataService>(); + + + services.AddSingleton(); + services.AddSingleton, HomeViewModelFactory>(); + services.AddSingleton, ClientListViewModelFactory>(); + services.AddScoped(); + + services.AddScoped(); + return services.BuildServiceProvider(); } diff --git a/DaSaSo.Wpf/Controls/NavigationBar.xaml b/DaSaSo.Wpf/Controls/NavigationBar.xaml new file mode 100644 index 0000000..8915f40 --- /dev/null +++ b/DaSaSo.Wpf/Controls/NavigationBar.xaml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + diff --git a/DaSaSo.Wpf/Controls/NavigationBar.xaml.cs b/DaSaSo.Wpf/Controls/NavigationBar.xaml.cs new file mode 100644 index 0000000..4c2d502 --- /dev/null +++ b/DaSaSo.Wpf/Controls/NavigationBar.xaml.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace DaSaSo.Wpf.Controls +{ + /// + /// Interaction logic for NavigationBar.xaml + /// + public partial class NavigationBar : UserControl + { + public NavigationBar() + { + InitializeComponent(); + } + } +} diff --git a/DaSaSo.Wpf/DaSaSo.Wpf.csproj.user b/DaSaSo.Wpf/DaSaSo.Wpf.csproj.user index 0ceb2db..6832663 100644 --- a/DaSaSo.Wpf/DaSaSo.Wpf.csproj.user +++ b/DaSaSo.Wpf/DaSaSo.Wpf.csproj.user @@ -7,26 +7,41 @@ + + Code + Code Code + + Code + Code + + Designer + Designer + + Designer + Designer Designer + + Designer + Designer diff --git a/DaSaSo.Wpf/Styles/Navigation_Style.xaml b/DaSaSo.Wpf/Styles/Navigation_Style.xaml new file mode 100644 index 0000000..825650e --- /dev/null +++ b/DaSaSo.Wpf/Styles/Navigation_Style.xaml @@ -0,0 +1,4 @@ + + + \ No newline at end of file diff --git a/DaSaSo.Wpf/View/HomeView.xaml b/DaSaSo.Wpf/View/HomeView.xaml new file mode 100644 index 0000000..7f72c16 --- /dev/null +++ b/DaSaSo.Wpf/View/HomeView.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/DaSaSo.Wpf/View/HomeView.xaml.cs b/DaSaSo.Wpf/View/HomeView.xaml.cs new file mode 100644 index 0000000..b7157c0 --- /dev/null +++ b/DaSaSo.Wpf/View/HomeView.xaml.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace DaSaSo.Wpf.View +{ + /// + /// Interaction logic for HomeView.xaml + /// + public partial class HomeView : UserControl + { + public HomeView() + { + InitializeComponent(); + } + } +} diff --git a/DaSaSo.Wpf/Window/MainWindow.xaml b/DaSaSo.Wpf/Window/MainWindow.xaml index 46ff256..c756882 100644 --- a/DaSaSo.Wpf/Window/MainWindow.xaml +++ b/DaSaSo.Wpf/Window/MainWindow.xaml @@ -5,6 +5,8 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ClientViews="clr-namespace:DaSaSo.Wpf.View.Client" xmlns:ProjektViews="clr-namespace:DaSaSo.Wpf.View.Project" + xmlns:controls="clr-namespace:DaSaSo.Wpf.Controls" + xmlns:View="clr-namespace:DaSaSo.Wpf.View" xmlns:local="clr-namespace:DaSaSo.Wpf" xmlns:viewmodel="clr-namespace:DaSaSo.ViewModel;assembly=DaSaSo.ViewModel" d:DataContext="{d:DesignInstance Type=viewmodel:MainWindowViewModel}" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> @@ -18,6 +20,9 @@ + + + @@ -28,13 +33,8 @@ - - - - - - - + +