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>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Container\Builder.cs" />
<Compile Include="Model\abrechnung.cs">
<DependentUpon>Inventar.tt</DependentUpon>
</Compile>

View File

@ -1,4 +1,4 @@
using System.Windows;
using System.Windows;
using LoggingClient.ViewModel;
namespace LoggingClient
@ -6,12 +6,14 @@ namespace LoggingClient
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public partial class MainWindow : Window
{
public MainWindow()
{
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
{
public class CustomerRepositoryEF
public class CustomerRepositoryEF : IRepositoryBase<Customer>
{
public CustomerRepositoryEF()
{

View File

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