0
votes

I have a huge WPF project. It uses:

  • MVVM Light
  • Microsoft.Unity container as a ViewModelLocator

The MainView content depends from other UserControls:

<ContentControl  Visibility="Visible"
                 Content="{Binding ContentViewModel}" />

Each ViewModel for content creates by ConcreateViewModelFactory:

 ContentViewModel =(SimpleViewModelBase)  _extendedSearchVMFactory.CreateInstance();

All ViewModels factories has interfaces that registered in ViewModelLocator. Factory example:

public class MainViewModelFactory : IMainViewModelFactory
{
    private IUnityContainer _container;

    public MainViewModelFactory(IUnityContainer container)
    {
        _container = container;
    }

    public MainViewModel CreateInstance(df_role[] currentRoles)
    {
        var vm = _container.Resolve<MainViewModel>(
            new ParameterOverride("currentRoles", currentRoles));
        return vm;
    }
}

MainViewModel costructor has about 40 parameters to keep all Interfaces for ViewModel constructors:

public MainViewModel(IDataService dataService,
        IDialogService dialogService,
        ISearchByIdViewModelFactory searchViewModelFactory,
        ISearchByPhonesViewModelFactory searchByPhonesViewModelFactory,
        IDoubleSearcherViewModelFactory doubleSearcherViewModelFactory,
        IAnalysisOfDoubleSearchAlgorythmViewModelFactory analysisOfDoubleSearchAlgorythmViewModelFactory,...

My question: How can I refactor this? If you have helpful information about DI+MVVM I will be grateful.

P.S. Sorry for my english.

1
If it takes 40 parameters probably it does a lot of work. You need to separate the responsibilities and move group of objects to a separate class. Then you can inject the newly created collaborator to your MainViewModel.Sriram Sakthivel
Well going by your title "Is it normal to pass many interfaces in ViewModel constructor using DI container in WPF" - WPF has no dependency (no pun intended) on DI so "Is it normal" would have to depend on your choice of a DI system. I don't know much about the DI you are using, but if you were using Microsoft MEF then your constructor would be normally parameter-less. MEF injects values into properties, not into constructors. 40 parameters would strike me as a sign of bad design on your partMickyD
As the others have said, separate the responsibility of the view model into services, and have those injected instead. That way, you may only need to inject a max of 5 services. This should also make the view model easier to unit test.Falgantil
I agree with @SriramSakthivel. Check out this article for some guidance.EagleBeak
@EagleBeak thanks for link!Vladislav

1 Answers

1
votes

MainViewModel constructor has many parameters. MainViewModel violates "Single Responsibility Principle". Ideally any class should not have many responsibility.

Please go through the links below to understand Single Responsibility Principle.

  1. http://www.codeproject.com/Articles/703634/SOLID-architecture-principles using-simple-Csharp

    The link #1 talks about SOLID Architecture Principle

    SOLID stands for: "S" - SRP (Single responsibility principle) “O” - Open closed principle “L”- LSP (Liskov substitution principle) “I” - ISP (Interface Segregation principle) “D” - Dependency inversion principle.

MVVM: To understand MVVM, please go through the link below

https://msdn.microsoft.com/en-us/magazine/dd419663.aspx