Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What changes were introduced in MvvmCross v3 regarding ViewModel construction


What changes were introduced in MvvmCross v3 regarding ViewModel construction


In MvvmCross v3, several significant changes were introduced regarding ViewModel construction, which aimed to enhance testability and usability for developers. Here are the key changes:

1. Constructor-Based Dependency Injection: One of the major changes was the introduction of constructor-based Dependency Injection. This allowed developers to inject dependencies directly into the ViewModel's constructor, making it easier to manage dependencies and improve testability. Prior to this, dependencies were often managed through other means, which could be less straightforward.

2. Navigation Using Typed Navigation Classes: MvvmCross v3 introduced a new navigation system that used typed navigation classes. This meant that instead of navigating directly to Views, developers could navigate between ViewModels using strongly-typed classes. This approach simplified cross-platform navigation by abstracting away platform-specific details.

3. Saving and Reloading ViewModel State for Tombstoning: Another significant feature introduced in v3 was the ability to save and reload ViewModel state. This was particularly useful for handling "tombstoning" scenarios, where an application might be terminated by the operating system due to memory constraints, and then restored when the user returns to it. By saving the ViewModel state, applications could maintain their current state even after being terminated and restarted.

4. Navigation Parameters Passed to `Init()` Method: In older versions of MvvmCross, navigation parameters were passed directly to the ViewModel's constructor. However, starting with v3, these parameters were instead passed to the `Init()` method. This change allowed the constructor to be used solely for Dependency Injection, making the code cleaner and more maintainable. The `Init()` method became the place where navigation parameters were processed, allowing for more flexible initialization logic.

Overall, these changes in MvvmCross v3 provided a more structured and maintainable way of constructing and managing ViewModels, enhancing the overall development experience for cross-platform applications.

Citations:
[1] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle
[2] https://github.com/MvvmCross/MvvmCross/releases
[3] https://www.reddit.com/r/csharp/comments/12akrcw/mvvm_question_how_do_you_manage_the_interaction/
[4] https://www.mvvmcross.com/documentation/advanced/customizing-using-App-and-Setup
[5] https://www.mvvmcross.com/blog/
[6] https://softwareengineering.stackexchange.com/questions/389086/do-we-need-models-if-we-are-using-viewmodels
[7] https://stackoverflow.com/questions/16723078/mvvmcross-does-showviewmodel-always-construct-new-instances
[8] https://stackoverflow.com/questions/39587974/changes-in-mvvmcross-bindings
[9] https://www.mvvmcross.com/documentation/fundamentals/navigation
[10] https://www.mvvmcross.com/documentation/fundamentals/data-binding
[11] https://github.com/MvvmCross/MvvmCross/blob/develop/CHANGELOG.md