Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM Pattern Confusion

Tags:

c#

.net

mvvm

wpf

i got confusion in MVVM Pattern. Actually i have ViewModel in which i have a ObservableCollection here AssignmentClass is same as that of model assignment but AssignmentClass has NotifypropertyChanged in it. so my question is can i implement INotifypropertyChanged on model so that i don't have to make another class(AssignmentClass), is it right according to MVVM Pattern.

my model is

public class assignment
{
    public int id { get; set; }
    public string title { get; set; }
    public string app_id { get; set; }
    public string student_asg_status { get; set; }
    public string teacher_id { get; set; }
    public string teacher_name { get; set; }
    public string asg_status { get; set; }
    public string sdate { get; set; }
    public string edate { get; set; }
    public string creation_date { get; set; }
    public string mod_date { get; set; }
    public string app_title { get; set; }
    public int category_id { get; set; }
    public string app_category { get; set; }
    public string cover { get; set; }
    public string icon { get; set; }
    public string settings { get; set; }


    public string isenddatedefault { get; set; }
    public string isstartdatedefault { get; set; }
    public bool isdraft { get; set; }

}

and my AssignmentClass has all the same properties but has NotifyPropertyChanged..on property id is..

      private int _id;
        public int id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
                RaisePropertyChanged("id");
            }
         }

in my viewmodel i make collection like thi..

 AssignmentCollection = new ObservableCollection<AssignmentClass>();
            var lst = await App.conn.QueryAsync<assignment>("SELECT * FROM assignment;");
            for (int i = 0; i < 6; i++)
            {
                AssignmentClass asd = new AssignmentClass();
                asd.id= lst[i].id;

                AssignmentCollection.Add(asd);


            }

can i make my collection like this on model assignment..if it implement propertychanged..

AssignmentCollection = new ObservableCollection<assignment>();
like image 556
loop Avatar asked Jan 26 '26 22:01

loop


1 Answers

Implementing the MVVM Pattern

Yes. You can implement the notification either in the Model or ViewModel class.

The View Model Class

  • The view model is a non-visual class and does not derive from any WPF or Silverlight base class. It encapsulates the presentation logic required to support a use case or user task in the application. The view model is testable independently of the view and the model.

  • The view model typically does not directly reference the view. It implements properties and commands to which the view can data bind. It notifies the view of any state changes via change notification events via the INotifyPropertyChanged and INotifyCollectionChanged interfaces.

The Model Class

  • Model classes are non-visual classes that encapsulate the application's data and business logic. They are responsible for managing the application's data and for ensuring its consistency and validity by encapsulating the required business rules and data validation logic.

  • The model classes do not directly reference the view or view model classes and have no dependency on how they are implemented.

  • The model classes typically provide property and collection change notification events through the INotifyPropertyChanged and INotifyCollectionChanged interfaces. This allows them to be easily data bound in the view. Model classes that represent collections of objects typically derive from the ObservableCollection class.

like image 163
Kurubaran Avatar answered Jan 29 '26 10:01

Kurubaran