Model Exposure with MVVM [closed]
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
-1
down vote
favorite
I have been starting to learn MVVM to give my WPF projects better structure.
I have to sample classes, DeviceModel
and DeviceViewModel
. Currently it is set up so that the model is exposed as a property within the view model. The model can then be bound to directly in the view.
public DeviceModel Device
get return device;
set
device = value;
OnPropertyChanged();
I am looking for feedback to taking this approach over alternative approaches in MVVM. Specifically more traditional approaches such as exposing each of the model's properties as properties within the view model that then just update the values stored in the model.
An example of this would look like the following.
public string DeviceName
get return device.DeviceName;
set
device.DeviceName = value;
OnPropertyChanged();
c# wpf mvvm
closed as off-topic by 200_success, Stephen Rauch, Ludisposed, Nikita B, t3chb0t Jul 30 at 17:32
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." â 200_success, Stephen Rauch, Ludisposed, Nikita B, t3chb0t
add a comment |Â
up vote
-1
down vote
favorite
I have been starting to learn MVVM to give my WPF projects better structure.
I have to sample classes, DeviceModel
and DeviceViewModel
. Currently it is set up so that the model is exposed as a property within the view model. The model can then be bound to directly in the view.
public DeviceModel Device
get return device;
set
device = value;
OnPropertyChanged();
I am looking for feedback to taking this approach over alternative approaches in MVVM. Specifically more traditional approaches such as exposing each of the model's properties as properties within the view model that then just update the values stored in the model.
An example of this would look like the following.
public string DeviceName
get return device.DeviceName;
set
device.DeviceName = value;
OnPropertyChanged();
c# wpf mvvm
closed as off-topic by 200_success, Stephen Rauch, Ludisposed, Nikita B, t3chb0t Jul 30 at 17:32
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." â 200_success, Stephen Rauch, Ludisposed, Nikita B, t3chb0t
I'm afraid I do not have enough rep to vote to take this question off hold but I believe I have edited the question so that it meets site requirements
â Dan
Jul 31 at 7:42
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have been starting to learn MVVM to give my WPF projects better structure.
I have to sample classes, DeviceModel
and DeviceViewModel
. Currently it is set up so that the model is exposed as a property within the view model. The model can then be bound to directly in the view.
public DeviceModel Device
get return device;
set
device = value;
OnPropertyChanged();
I am looking for feedback to taking this approach over alternative approaches in MVVM. Specifically more traditional approaches such as exposing each of the model's properties as properties within the view model that then just update the values stored in the model.
An example of this would look like the following.
public string DeviceName
get return device.DeviceName;
set
device.DeviceName = value;
OnPropertyChanged();
c# wpf mvvm
I have been starting to learn MVVM to give my WPF projects better structure.
I have to sample classes, DeviceModel
and DeviceViewModel
. Currently it is set up so that the model is exposed as a property within the view model. The model can then be bound to directly in the view.
public DeviceModel Device
get return device;
set
device = value;
OnPropertyChanged();
I am looking for feedback to taking this approach over alternative approaches in MVVM. Specifically more traditional approaches such as exposing each of the model's properties as properties within the view model that then just update the values stored in the model.
An example of this would look like the following.
public string DeviceName
get return device.DeviceName;
set
device.DeviceName = value;
OnPropertyChanged();
c# wpf mvvm
edited Jul 31 at 7:37
asked Jul 30 at 13:09
Dan
240129
240129
closed as off-topic by 200_success, Stephen Rauch, Ludisposed, Nikita B, t3chb0t Jul 30 at 17:32
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." â 200_success, Stephen Rauch, Ludisposed, Nikita B, t3chb0t
closed as off-topic by 200_success, Stephen Rauch, Ludisposed, Nikita B, t3chb0t Jul 30 at 17:32
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." â 200_success, Stephen Rauch, Ludisposed, Nikita B, t3chb0t
I'm afraid I do not have enough rep to vote to take this question off hold but I believe I have edited the question so that it meets site requirements
â Dan
Jul 31 at 7:42
add a comment |Â
I'm afraid I do not have enough rep to vote to take this question off hold but I believe I have edited the question so that it meets site requirements
â Dan
Jul 31 at 7:42
I'm afraid I do not have enough rep to vote to take this question off hold but I believe I have edited the question so that it meets site requirements
â Dan
Jul 31 at 7:42
I'm afraid I do not have enough rep to vote to take this question off hold but I believe I have edited the question so that it meets site requirements
â Dan
Jul 31 at 7:42
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Models typically do not implement INotifyPropertyChanged
interface. So binding directly to model properties has two main consequences:
1) It creates a memory leak.
2) It disables ViewModel -> View
notifications. Models do not fire PropertyChanged
event, so UI will not update itself when you change model properties from code. Updates coming in the opposite direction (from UI to your model) will still work though.
Your approach will work fine, if your DeviceModel
is immutable. Meaning that you update it as
//setter fires PropertyChanged event
Device = new DeviceModel(...);
Then you can use OneTime
binding in xaml to avoid memory leaks
<Border DataContext="Binding Device">
<TextBlock Text="Binding SomeDeviceProperty, Mode=OneTime"/>
</Border>
Thanks for the answer. That makes more sense now
â Dan
Jul 30 at 14:07
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Models typically do not implement INotifyPropertyChanged
interface. So binding directly to model properties has two main consequences:
1) It creates a memory leak.
2) It disables ViewModel -> View
notifications. Models do not fire PropertyChanged
event, so UI will not update itself when you change model properties from code. Updates coming in the opposite direction (from UI to your model) will still work though.
Your approach will work fine, if your DeviceModel
is immutable. Meaning that you update it as
//setter fires PropertyChanged event
Device = new DeviceModel(...);
Then you can use OneTime
binding in xaml to avoid memory leaks
<Border DataContext="Binding Device">
<TextBlock Text="Binding SomeDeviceProperty, Mode=OneTime"/>
</Border>
Thanks for the answer. That makes more sense now
â Dan
Jul 30 at 14:07
add a comment |Â
up vote
1
down vote
accepted
Models typically do not implement INotifyPropertyChanged
interface. So binding directly to model properties has two main consequences:
1) It creates a memory leak.
2) It disables ViewModel -> View
notifications. Models do not fire PropertyChanged
event, so UI will not update itself when you change model properties from code. Updates coming in the opposite direction (from UI to your model) will still work though.
Your approach will work fine, if your DeviceModel
is immutable. Meaning that you update it as
//setter fires PropertyChanged event
Device = new DeviceModel(...);
Then you can use OneTime
binding in xaml to avoid memory leaks
<Border DataContext="Binding Device">
<TextBlock Text="Binding SomeDeviceProperty, Mode=OneTime"/>
</Border>
Thanks for the answer. That makes more sense now
â Dan
Jul 30 at 14:07
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Models typically do not implement INotifyPropertyChanged
interface. So binding directly to model properties has two main consequences:
1) It creates a memory leak.
2) It disables ViewModel -> View
notifications. Models do not fire PropertyChanged
event, so UI will not update itself when you change model properties from code. Updates coming in the opposite direction (from UI to your model) will still work though.
Your approach will work fine, if your DeviceModel
is immutable. Meaning that you update it as
//setter fires PropertyChanged event
Device = new DeviceModel(...);
Then you can use OneTime
binding in xaml to avoid memory leaks
<Border DataContext="Binding Device">
<TextBlock Text="Binding SomeDeviceProperty, Mode=OneTime"/>
</Border>
Models typically do not implement INotifyPropertyChanged
interface. So binding directly to model properties has two main consequences:
1) It creates a memory leak.
2) It disables ViewModel -> View
notifications. Models do not fire PropertyChanged
event, so UI will not update itself when you change model properties from code. Updates coming in the opposite direction (from UI to your model) will still work though.
Your approach will work fine, if your DeviceModel
is immutable. Meaning that you update it as
//setter fires PropertyChanged event
Device = new DeviceModel(...);
Then you can use OneTime
binding in xaml to avoid memory leaks
<Border DataContext="Binding Device">
<TextBlock Text="Binding SomeDeviceProperty, Mode=OneTime"/>
</Border>
edited Jul 31 at 7:58
answered Jul 30 at 14:01
Nikita B
12.3k11551
12.3k11551
Thanks for the answer. That makes more sense now
â Dan
Jul 30 at 14:07
add a comment |Â
Thanks for the answer. That makes more sense now
â Dan
Jul 30 at 14:07
Thanks for the answer. That makes more sense now
â Dan
Jul 30 at 14:07
Thanks for the answer. That makes more sense now
â Dan
Jul 30 at 14:07
add a comment |Â
I'm afraid I do not have enough rep to vote to take this question off hold but I believe I have edited the question so that it meets site requirements
â Dan
Jul 31 at 7:42