Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MVVM pattern what is the recommended order of instantiation? [closed]

Tags:

c#

mvvm

wpf

I got this asked in an interview recently . He wanted to know the order of instantiation of Model View and ViewModel what the precise order of instantiation would be ?

I thought the view is always instantiated first and then comes the viewmodel and then comes the model. was i wrong ??

like image 871
Nick Avatar asked Jan 16 '26 21:01

Nick


1 Answers

I thought the view is always instantiated first and then comes the viewmodel and then comes the model. was i wrong ??

There is no single standard. There are, in general, two approaches:

  1. View-First - The View will be instantiated first, and in turn instantiate the ViewModel, which will likely create the underlying model. This typically means the order of instantiation is View->ViewModel->Model.

  2. ViewModel-First - The ViewModel is created, which in turn instantiates the Model. The View is generated by the system based on DataTemplates after the ViewModel. This would mean the order of instantiation would be ViewModel->Model, then View (indirectly from XAML).

Most frameworks which are geared heavily towards a designer-first approach tend to do View-First construction. This makes it easier to work with the designer (in general).

Many frameworks which are geared heavily towards developer-focused scenarios will often do ViewModel first. This approach can actually lead to even less coupling, and simpler "code-only" construction of everything from the ViewModel level.

like image 196
Reed Copsey Avatar answered Jan 19 '26 20:01

Reed Copsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!