Implementing IoC for ViewModel
This commit is contained in:
parent
11d8ac7ba1
commit
0f743122c1
31
LoggingClient/LoggingClient/Container/Builder.cs
Normal file
31
LoggingClient/LoggingClient/Container/Builder.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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>
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ using System.Linq;
|
||||
|
||||
namespace LoggingClient.Repository
|
||||
{
|
||||
public class CustomerRepositoryEF
|
||||
public class CustomerRepositoryEF : IRepositoryBase<Customer>
|
||||
{
|
||||
public CustomerRepositoryEF()
|
||||
{
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user