I'm building a master-detail form. The master view model constructs instances of the details view model. These details view models have several dependencies which need to be satisfied with new class instances. (This is because they need service layers that operate in a separate data context from the master vm.)
What would be the best way to fulfill these dependencies?
Thank you,
Ben
The following approach would solve the problem. However, as it introduces hard-coded dependencies, using it is out of the question.
// in the master view model
var detailViewModel = new DetailViewModel(new AccountService(), new TransactionService());
Another option would be for the parent view model to hold a reference to an IoC framework. This approach introduces a master view model dependency on the IoC framewok.
// in the master view model
var detailViewModel = new DetailViewModel(resolver.GetNew<IAccountService>(), resolver.GetNew<IAccountService>());
class MasterViewModel {
public MasterViewModel(Func<Service.IAccountService> accountServiceFactory, Func<Service.ITransactionService> transactionServiceFactory) {
this.accountServiceFactory = accountServiceFactory;
this.transactionServiceFactory = transactionServiceFactory;
// instances for MasterViewModel's internal use
this.accountService = this.accountServiceFactory();
this.transactionService = this.transactionServiceFactory():
}
public SelectedItem {
set {
selectedItem = value;
DetailToEdit = new DetailViewModel(selectedItem.Id, accountServiceFactory(), transactionServiceFactory());
}
// ....