Reputation: 4351
In ReactiveUI
, if I have a model (INotifyPropertyChanged
) behind a ViewModel (ReactiveObject
) what's the best practice? Do I use the model in the getter and setter of a property:
private Model model;
public string Minky
{
get { return model.Minky; }
set
{
model.Minky = value;
this.PropertyChanged();
}
}
Or should I bind individual properties:
private string minky;
public string Minky
{
get { return minky; }
set { this.RaiseAndSetIfChanged(ref minky, value); }
}
public ViewModel( Model model )
{
if ( model != null )
{
model.WhenAnyValue( x => x.Minky ).BindTo( this, x => x.Minky );
}
}
The second version seems like a good idea (I can still set properties when there is no model). Is there any reason why this is a bad idea?
Upvotes: 1
Views: 1316
Reputation: 3949
In any MVVM binding pattern best practice is to bind the "View" to the "ViewModel" and no model is used here. Of course models are then used in data access and business layers to do stuff like record in db or pass to web services but not in binding.
By any chance if you want to use your encapsulated models for binding reasons you should make your models as bindable objects with their own PropertyChanged
notifiers and then bind to them. Like this:
You have a Model
in your ViewModel
private Model model;
public Model Model
{
get { return model; }
set
{
model = value;
this.PropertyChanged();
}
}
And then bind to properties
this.Bind(ViewModel, x => x.Name, x => x.Model.Minky);
So to summarize: It's a bad idea. But if you want to do it bind directly to a model instance inside your ViewMdel.
Upvotes: 2