Satisfying Child View Model Dependencies

I am creating a master-detail form. The master view model instantiates the detail view model. These part view models have several dependencies that must satisfy new instances of the class. (This is because they need service levels that work in a separate data context from the vm wizard.)

What would be the best way to fulfill these dependencies?

Thanks,
Ben

+4
source share
4 answers

Some features:

Hard Coded Links

Solving the problem can solve the following approach. However, since it introduces hard-coded dependencies, the use of this is out of the question.

// in the master view model var detailViewModel = new DetailViewModel(new AccountService(), new TransactionService()); 

Resolution through IoC Framework

Another option would be for the parent view model to reference the IoC structure. This approach introduces the dependency of the main view model on the IoC framewok.

 // in the master view model var detailViewModel = new DetailViewModel(resolver.GetNew<IAccountService>(), resolver.GetNew<IAccountService>()); 

Factory Func <> s

 class MasterViewModel { public MasterViewModel(Func<Service.IAccountService> accountServiceFactory, Func<Service.ITransactionService> transactionServiceFactory) { this.accountServiceFactory = accountServiceFactory; this.transactionServiceFactory = transactionServiceFactory; // instances for MasterViewModel internal use this.accountService = this.accountServiceFactory(); this.transactionService = this.transactionServiceFactory(): } public SelectedItem { set { selectedItem = value; DetailToEdit = new DetailViewModel(selectedItem.Id, accountServiceFactory(), transactionServiceFactory()); } // .... 
0
source

The BookLibrary example of the WPF Application Framework (WAF) example shows how to implement the Master / Detail script with the MV-VM. It uses MEF as an IoC container to satisfy ViewModel dependencies.

+1
source

You can also use the container to create a detailed view:

 var detailViewModel = container.CreateInstance<DetailViewModel>(); 

The container will resolve dependencies for IAccountService and ITransactionService. But you will still have a dependency on the IOC structure (unless you use the CommonServiceLocator ).

Here's how I do it using the CommonServiceLocator:

 this.accountService = ServiceLocator.Current.GetInstance<IAccountService>(); this.transactionService = ServiceLocator.Current.GetInstancey<ITransactionService>(); 
0
source

I am trying to solve this problem myself. Here is an important blog article: http://www.primordialcode.com/blog/post/silverlight-mvvm-ioc-part-3

0
source

All Articles