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).
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
MainViewModel needs reference to ICustomerService, and IBookService
CustomerListViewModel needs reference to ICustomerService
BoolListViewModel needs reference to IBookService
BookDetailViewModel needs reference to ICustomerService, and IBookReserveService
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With