Model Exposure with MVVM [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash 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();








share|improve this question













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
If this question can be reworded to fit the rules in the help center, please edit the question.












  • 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
















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();








share|improve this question













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
If this question can be reworded to fit the rules in the help center, please edit the question.












  • 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












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();








share|improve this question













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();










share|improve this question












share|improve this question




share|improve this question








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
If this question can be reworded to fit the rules in the help center, please edit the question.




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
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 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




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










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>





share|improve this answer























  • Thanks for the answer. That makes more sense now
    – Dan
    Jul 30 at 14:07

















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>





share|improve this answer























  • Thanks for the answer. That makes more sense now
    – Dan
    Jul 30 at 14:07














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>





share|improve this answer























  • Thanks for the answer. That makes more sense now
    – Dan
    Jul 30 at 14:07












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>





share|improve this answer















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>






share|improve this answer















share|improve this answer



share|improve this answer








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
















  • 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


Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation