× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Benefit of IoC container in MVVMLight

Gayot Fow
Feb 05, 2015
<p>Instantiating the view model in Xaml is absolutely fine, but there are a few things to take in to account...</p> <ul> <li>When you use Xaml for the view model, the VM constructor is called during the <code>InitializeComponent</code> method of the main window. It means the VM will be created on the UI thread. Some developers like that, others do not.</li> <li>Also when you use Xaml, it's like 'hard-coding' the VM and this defeats the late binding and module discovery that plays an integral role in large composite applications</li> <li>Somewhat related to the previous point, some teams like to decouple everything and use a configuration file to tell which vm should be instantiated. This approach is especially useful if you want to do isolation testing.</li> </ul> <p>For small, one-off WPF applications that want to use MVVM, the Xaml approach is fine; but as the app matures, eventually a more industrial strength approach will be called for. And that's where the IoC container is essential.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/23491938/Benefit%20of%20IoC%20container%20in%20MVVMLight/23492230">Stack Overflow</a>.</p>
comments powered by Disqus