ViewModels or ViewBag?

Basically, Should everything be done through the viewmodel or is it Ok to also incorporate viewbag? Everything should be done inside a view model. That’s what a view model is. A class that you specifically define to meet the requirements of your view. Don’t mix ViewBags with ViewModels. It is no longer clear for the … Read more

ViewModel in Kotlin: Unresolved Reference

Include the following as a dependency: implementation “android.arch.lifecycle:extensions:1.1.1” The equivalent AndroidX dependency is: “androidx.lifecycle:lifecycle-extensions:VERSION” in where VERSION can be replaced for Current Stable, Beta or Alpha values given in this official link This dependency is for both ViewModel and LiveData and thus would not require you to give separate dependencies for the same either; i.e. … Read more

ASP.NET MVC – Database entities or ViewModels?

Definitely use view models in your views, and use something like AutoMapper to create view models from entities easily. Cons: Sometimes it feels like you are duplicating code, specifically, when the view model and the entity have the exact same properties Pros: You often need to represent an object in a simpler format (often called … Read more

WPF: how to signal an event from ViewModel to View without code in codebehind? [duplicate]

Some comments: You can use the weak event pattern to ensure that the view can be GC’d even if it is still attached to the view model’s event If you’re already switching multiple VMs in for the one view, wouldn’t that be the ideal place to attach/detach the handler? Depending on your exact scenario, you … Read more

Should a user control have its own view model?

Absolutely, positively NO Your UserControls should NOT have ViewModels designed specifically for them. This is, in fact, a code smell. It doesn’t break your application immediately, but it will cause you pain as you work with it. A UserControl is simply an easy way to create a Control using composition. UserControls are still Controls, and … Read more

How to write a ViewModelBase in MVVM

It’s worth nothing to use MVVM frameworks if you don’t know what’s going on inside. So let’s go step by step and build your own ViewModelBase class. ViewModelBase is class common for all your viewmodels. Let’s move all common logic to this class. Your ViewModels should implement INotifyPropertyChanged (do you understand why?) public abstract class … Read more

MVVM and collections of VMs

Your general approach is perfectly fine MVVM, having a ViewModel exposing a collection of other ViewModels is a very common scenario, which I use all over the place. I would not recommend exposing items directly in a ViewModel, like nicodemus13 said, as you end up with your view binding to models without ViewModels in between … Read more