Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF with Unity Container - How to register and resolve ViewModels to Views

Hi I am trying to use Unity container in WPF MVVM application. I have not used Prism as it seems to heavy. Here is the application structure. I am trying to figure out how to resolve Views to ViewModels and dependencies of the view models (services).

Application:

Views

MainWindow.xaml
CustomerList.xaml
CustomerDetail.xaml
BookList.xaml
BookDetail.xaml

ViewModels

MainViewModel

CustomerListViewModel

BoolListViewModel

BookDetailViewModel

CustomerDetailViewModel

Library

ICustomerService (AddCustomer, SaveCustomer, GetCustomers, GetCustomer)

CustomerService:ICustomerService

IBookService (GetBooks, GetBook)

BookService:IBookService

IBookReserveService(Reserve, Return)

BookReserveService:IBookReserveService
  1. MainViewModel needs reference to ICustomerService, and IBookService

  2. CustomerListViewModel needs reference to ICustomerService

  3. BoolListViewModel needs reference to IBookService

  4. BookDetailViewModel needs reference to ICustomerService, and IBookReserveService

  5. CustomerDetailViewModel needs reference to ICustomerService, and IBookReserveService

I have getter setter property for services in each viewmodels.

I am running into issues on how do I use Dependency Injection with WPF especially for Views and ViewModel. I tried with Unity to register and resolve in a console application which is working fine. But I would like some ideas on how this could be done for WPF app. I tried registering

 container.RegisterType<ICustomerService, CustomerService>()
 container.RegisterType<IBookReserveService, BookReserveService>()
 container.RegisterType<IBookService, BookService>()

and resolve it using container.Resolve();

But I was not sure how I could tell which view must use which view model and resolve them when required and not when the app starts. Also, I dont to resolve all mapping in the app start. It should be done when the menu (Selecting a customer to view detail, selecting a book to view detail, save customer, reserve book etc.) is selected.

Mostly what I read wanted to use IView and IViewModel. But not sure I understood the advantage in it.

Any help is greatly appreciated.

like image 393
isakavis Avatar asked Nov 20 '12 19:11

isakavis


1 Answers

Here's one way you can do this. First, register your view-models and services with Unity like this:

// Unity is the container
_container.RegisterType<IMainViewModel, MainViewModel>();
_container.RegisterType<IBookService, BookService>();

Second, set your view's DataContext to a view-model in the view's constructor like this:

public partial class MainView:UserControl
{
   private readonly IUnityContainer _container;

   public MainView(IUnityContainer container)
        {
            InitializeComponent();
            _container = container;   
            this.DataContext = _container.Resolve<IMainViewModel>();            
        }      
}

Third, you'll need to inject your services into your view-models:

public MainViewModel(ICustomerService custService, IBookService bookService ){}

There are other ways to do this using .config files, etc. but this should give you enough to get started, let me know if you need more. You asked what the advantages of DI are and there are many, I feel. Just to name a couple: promotes loose-coupling between your components and improved testability. I feel it's one of the lynch-pins to a good design/implementation.

UPDATE:

With Unity >=3, you can skip the container registration if you follow the naming convention like this:

// This will register all types with a ISample/Sample naming convention 
            container.RegisterTypes(
                AllClasses.FromLoadedAssemblies(),
                WithMappings.FromMatchingInterface,
                WithName.Default);
like image 149
Big Daddy Avatar answered Nov 15 '22 09:11

Big Daddy