Implementing IoC for ViewModel

This commit is contained in:
francesco 2020-09-16 17:52:27 +02:00
parent 11d8ac7ba1
commit 0f743122c1
5 changed files with 49 additions and 13 deletions

View File

@ -0,0 +1,31 @@
using Autofac;
using LoggingClient.Model;
using LoggingClient.Repository;
using LoggingClient.ViewModel;
namespace LoggingClient.Container
{
public class Builder
{
public static IContainer BuildAutofacContainer()
{
var builder = new ContainerBuilder();
// ViewModel
builder.RegisterType<CustomerViewModel>().As<CustomerViewModel>();
builder.RegisterType<LogViewModel>().As<LogViewModel>();
builder.RegisterType<LocationViewModel>().As<LocationViewModel>();
builder.RegisterType<NavigationViewModel>().As<NavigationViewModel>();
// Repositories
builder.RegisterType<LoggingRepository>().As<IRepositoryBase<Logging>>();
builder.RegisterType<LocationRepository>().As<IRepositoryBase<Location>>();
builder.RegisterType<CustomerRepository>().As<IRepositoryBase<Customer>>();
builder.RegisterType<CustomerRepositoryLinq>().As<IRepositoryBase<Customer>>();
builder.RegisterType<CustomerRepositoryEF>().As<IRepositoryBase<Customer>>();
return builder.Build();
}
}
}

View File

@ -100,6 +100,7 @@
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="Container\Builder.cs" />
<Compile Include="Model\abrechnung.cs"> <Compile Include="Model\abrechnung.cs">
<DependentUpon>Inventar.tt</DependentUpon> <DependentUpon>Inventar.tt</DependentUpon>
</Compile> </Compile>

View File

@ -1,4 +1,4 @@
using System.Windows; using System.Windows;
using LoggingClient.ViewModel; using LoggingClient.ViewModel;
namespace LoggingClient namespace LoggingClient
@ -6,12 +6,14 @@ namespace LoggingClient
/// <summary> /// <summary>
/// Interaction logic for MainWindow.xaml /// Interaction logic for MainWindow.xaml
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
this.DataContext = new NavigationViewModel(); //Container = Builder.BuildAutofacContainer();
} //DataContext = Container.Resolve<NavigationViewModel>();
} DataContext = new NavigationViewModel();
}
}
} }

View File

@ -6,7 +6,7 @@ using System.Linq;
namespace LoggingClient.Repository namespace LoggingClient.Repository
{ {
public class CustomerRepositoryEF public class CustomerRepositoryEF : IRepositoryBase<Customer>
{ {
public CustomerRepositoryEF() public CustomerRepositoryEF()
{ {

View File

@ -1,8 +1,9 @@
using LoggingClient.Repository; using LoggingClient.Container;
using LoggingClient.ViewModel.Commands; using LoggingClient.ViewModel.Commands;
using System;
using System.ComponentModel; using System.ComponentModel;
using System.Windows.Input; using System.Windows.Input;
using Autofac;
using IContainer = Autofac.IContainer;
namespace LoggingClient.ViewModel namespace LoggingClient.ViewModel
{ {
@ -18,26 +19,27 @@ namespace LoggingClient.ViewModel
get => _selectedViewModel; get => _selectedViewModel;
set { _selectedViewModel = value; OnPropertyChanged("SelectedViewModel"); } set { _selectedViewModel = value; OnPropertyChanged("SelectedViewModel"); }
} }
private IContainer container;
public NavigationViewModel() public NavigationViewModel()
{ {
LogsCommand = new BaseCommand(OpenLogs); LogsCommand = new BaseCommand(OpenLogs);
LocationsCommand = new BaseCommand(OpenLocations); LocationsCommand = new BaseCommand(OpenLocations);
CustomersCommand = new BaseCommand(OpenCustomers); CustomersCommand = new BaseCommand(OpenCustomers);
container = Builder.BuildAutofacContainer();
} }
private void OpenLogs(object obj) private void OpenLogs(object obj)
{ {
SelectedViewModel = new LogViewModel(); SelectedViewModel = container.Resolve<LogViewModel>();
} }
private void OpenLocations(object obj) private void OpenLocations(object obj)
{ {
SelectedViewModel = new LocationViewModel(); SelectedViewModel = container.Resolve<LocationViewModel>();
} }
private void OpenCustomers(object obj) private void OpenCustomers(object obj)
{ {
SelectedViewModel = new CustomerViewModel(); SelectedViewModel = container.Resolve<CustomerViewModel>();
} }
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;