user19303083
user19303083

Reputation:

WPF - MVVM big Models or big View Models

I have spent some time searching about MVVM in WPF. I'm familiar with C# and WPF. I also understand MVVM and its benefits. I don't really know how to go about my view models and models as the multiple examples I've seen are not always the same. I know MVVM is flexible and that you can "adapt" it to your needs.

I'll explain what my application is about and then ask my questions.

I'll simplify to Deliveries and Items. Both are tables from SQL. I can make my own SQL requests and put the rows in a list or an observablecollection without a problem. In the application, you have a list of deliveries and when you select one, all its items appear on another list next to it. You can Add/Delete a delivery, Add/Delete an Item. You can modify the quantity of the Item. Note that all the actions modify the Database instantly. There is a lot of verification for each actions.

I've already built the application in a not-so-good way (one MainWindow and all the rest in the code-behind) and I'd like to rebuild it using MVVM even if it's hard because it would be the right way.

So what I'd do is have 2 models and 2 view models (Deliveries and Items). My old application had 2 observablecollections so I'd like to re-use that to help me transfer. I'd put INotifyPropertyChanged in the view models or maybe have a base view model for that.

Questions:

Should I have an observablecollections in the model and one in the view model? Only one in the view model? Simple lists would be better?

This kinda depends on the first question: should I gather the data from my DB in my ViewModel and put it in an observablecollection? Is it better to do that in the model? I've seen that it should be in the model or even another layer.

This kinda depends on the first question too: where should I put my methods, view model or model? Let's say AddDelivery(). What I did was add the delivery to my observablecollection and update the DB at the same time. So now if I have only one observablecollection in my View Model, I'd need to have my methods at the same place? What if you tell me to make 2 observablecollections.

Edit

With the help I got I managed to start my new app!

I'm stuck at one part now, how can one view model communicate with another one? I have INotifyPropertyChanged in both of those models. I need to change my observableCollection of Item when I select a delivery. I already have the NotifyPropertyChanged when I select I only need to Subscribe to the event from my ItemViewModel or send a message to it?

Models:

    public class Delivery
    {
        public string name { get; set; }
        public string noDelivery{ get; set; }
    }


    public class Item
    {
        public string name { get; set; }
        public string noItem { get; set; }
    }

View Models:

    public abstract class ViewModelBase : INotifyPropertyChanged 
    {
  

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")  
        {  
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }  
    }
    
    public class DeliveryViewModel : ViewModelBase
    {
        public ObservableCollection<Delivery> Deliveries{ get; private set;}
        //Some Functions
    }


    public class ItemViewModel
    {
        public ObservableCollection<Item> Items { get; private set; }

        //Some Functions
    }

I call NotifyPropertyChanged in my DeliveryViewModel but I don't know how can the other view model do something with it. In other words I want to call a function from ItemViewModel when an event happens in DeliveryViewModel.

Upvotes: 0

Views: 565

Answers (1)

shisin
shisin

Reputation: 112

Answers:

  1. You should have an observablecollections in the view model only as per the best practices. Because your viewModel is going to connect to View and not the model.

  2. You should always gather the data from DB in Model first. That is how the separation of concerns work right. Then you can add the same in ObservableCollection of Your ViewModel.

  3. You methods which will directly interact with your DB should be placed in Models. The final list after adding the delivery details shall be re-fetched or updated in ViewModel ObservableCollection with new Id assigned to it.

Always Follow Separation Of Concerns Principle. Because MVVM is all about loosely Coupling.

Thanks.

Upvotes: 3

Related Questions