QuestionnaireViewModel-basic questions

Topics: Prism v4 - Silverlight 4
Dec 13, 2010 at 10:34 AM



QuestionnaireViewModel() :this(new QuestionnaireRepository





public QuestionnaireViewModel(IQuestionnaireRepository questionnaireRepository)



this.questionnaireRepository = questionnaireRepository;


Please explain why 2 constructors, one use concrete class and one an interface

Is the first constructor intended for XAML instancing of the class and the second for

Unit testing and code instancing of the class?

What is the benefit in passing IQuestionnaireRepository to the constructor instead of

Registering into the unity container?

If a view module can work with a concrete class –hence dependent on an implementation

Isn’t it breaking the basis on which prism is standing on?


Dec 13, 2010 at 8:24 PM


As you've mentioned, the first constructor, also called default constructor is intended to instantiate the view model in the XAML. You could read more about this in the MVVM Quickstart. The second constructor is there to allow the injection in runtime of the class that implements IQuestionnaireRepository, to support unit testing and code instancing of the class providing the desired implementation of IQuestionnaireRepository.

The MVVM Quickstart is not using any Dependency Injection mechanism, as its purpose is solely to "(...)demonstrate how to build an application that implements the MVVM presentation pattern". You might find the MVVM Reference Implementation useful, as it portrays a full MVVM scenario, including the use of MEF as a Dependency Injection mechanism.

I hope you find this information useful.


Miguel Bronzovic