C
C#3w ago
Kek

MVVM should model items have INotify?

Very basic question but I am conflicted. I have a viewmodel
public partial class HsSelectedItemViewModel : BaseViewModel
{
public long? BarCode { get; set; } = -1;
public ObservableCollection<HSItemFull> Items { get; set; } = new();
public partial class HsSelectedItemViewModel : BaseViewModel
{
public long? BarCode { get; set; } = -1;
public ObservableCollection<HSItemFull> Items { get; set; } = new();
The ui is a listview with items as itemsource and the items are shown with their details. Now when I add to Items it will update the ui ofc, but when I change a property of an item I also want to show it in the ui. I can do this by adding
public partial class HSItemFull : ObservableObject, INotifyPropertyChanged
{
public string UUID { get; set; } = "NOT SET";
DateTime expires;
[ObservableProperty]
DateTime accuired;
public partial class HSItemFull : ObservableObject, INotifyPropertyChanged
{
public string UUID { get; set; } = "NOT SET";
DateTime expires;
[ObservableProperty]
DateTime accuired;
My only logical issue is that the HSItemFull is in the model. Whats the common way to handl this, is it stupid to add Observable property to model classes? The other way I see is to copy this item into one at viewmodel which seems more stupid.
5 Replies
Denis
Denis3w ago
Some time ago I've asked the same question here.
sibber
sibber3w ago
create a viewmodel for HSItemFull
Denis
Denis3w ago
I can say that there's nothing wrong in making a model observable. The viewmodel must inherently be observable, but it also contains business logic, commands and so on. A model; however, should just represent data. So, no business logic, no commands, no methods (apart from equality, cloning, etc.). This doesn't mean you can't make the model observable. Otherwise you'd be forced write a ton of unnecessary viewmodels that'd shadow your models, only to add observability. In some cases this is a valid approach. E.g., a validated form for creating a record in the db
sibber
sibber3w ago
i wouldnt say a model has to just represent data its just whatever the actual appolication layer is a dto is what only represents data but yeah you can make the model observable i think thats fine altho id ask in #gui to see if they have another approach
Kek
Kek3w ago
Thanks for the answers
Want results from more Discord servers?
Add your server