surpavan
surpavan

Reputation: 1412

Simple MVVM with InotifyPropertyChanged at different levels

I am new to deep coding like MVVM, on the page MSDN Simple MVVM page, Model class is defined without INotifyPropertyChanged, only the VIEWModelBase it is used.

Main Code is as follows (CODE is copied from the mentioned page):

Namespace SimpleMVVM.Model

    Public Class Customer
        Public Property CustomerID() As Integer 
        Public Property FullName() As String 
        Public Property Phone() As String 
    End Class 
End Namespace

Namespace SimpleMVVM.Model
    Public Class CustomerRepository
        Private _customers As List(Of Customer)

        Public Sub New()
            _customers = New List(Of Customer) From
            {
                New Customer() With {.CustomerID = 1, .FullName = "Dana Birkby", .Phone = "394-555-0181"},
                New Customer() With {.CustomerID = 2, .FullName = "Adriana Giorgi", .Phone = "117-555-0119"},
                New Customer() With {.CustomerID = 3, .FullName = "Wei Yu", .Phone = "798-555-0118"}
            }
        End Sub 

        Public Function GetCustomers() As List(Of Customer)
            Return _customers
        End Function 

        Public Sub UpdateCustomer(SelectedCustomer As Customer)
            Dim customerToChange = _customers.Single(Function(c) c.CustomerID = SelectedCustomer.CustomerID)
            customerToChange = SelectedCustomer
        End Sub 
    End Class 
End Namespace


Imports System.ComponentModel

Namespace SimpleMVVM.ViewModel

    Public MustInherit Class ViewModelBase
        Implements INotifyPropertyChanged

        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

        Protected Overridable Sub OnPropertyChanged(propname As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propname))
        End Sub 
    End Class 
End Namespace

Imports System.Collections.Generic
Imports SimpleMVVM.Model

Namespace SimpleMVVM.ViewModel

    Public Class CustomerViewModel
        Inherits ViewModelBase

        Private _customers As List(Of Customer)
        Private _currentCustomer As Customer
        Private _repository As CustomerRepository
        Private _updateCustomerCommand As RelayCommand

        Public Sub New()
            _repository = New CustomerRepository()
            _customers = _repository.GetCustomers()

            WireCommands()
        End Sub 

        Private Sub WireCommands()
            UpdateCustomerCommand = New RelayCommand(AddressOf UpdateCustomer)
        End Sub 

        Public Property UpdateCustomerCommand() As RelayCommand
            Get 
                Return _updateCustomerCommand
            End Get 
            Private Set(value As RelayCommand)
                _updateCustomerCommand = value
            End Set 
        End Property 

        Public Property Customers() As List(Of Customer)
            Get 
                Return _customers
            End Get 
            Set(value As List(Of Customer))
                _customers = value
            End Set 
        End Property 

        Public Property CurrentCustomer() As Customer
            Get 
                Return _currentCustomer
            End Get 
            Set(value As Customer)
                If _currentCustomer.Equals(value) Then
                    _currentCustomer = value
                    OnPropertyChanged("CurrentCustomer")
                    UpdateCustomerCommand.IsEnabled = True 
                End If 
            End Set 
        End Property 

        Public Sub UpdateCustomer()
            _repository.UpdateCustomer(CurrentCustomer)
        End Sub 
    End Class 
End Namespace
  1. My question is does the effect of INotifyPropertyChanged effect be the same for INotifypropertychanged being implemented on each property within the class customer compared to that Inotifyproperty implemented on CustomerOBject in viewmodelbase.

  2. In the second case when the customer object only one property value is modified like CurrentCustomer.FullName="asdasd" does raise Inotifypropertychanged event per property?

  3. Can Viewmodelclass be sent over WCF like a datacontract, or datacontract is only for Modelclass without InotifyPropertyChanged

Thank you.

Upvotes: 1

Views: 414

Answers (1)

Big Daddy
Big Daddy

Reputation: 5224

I'll try to answer your questions:

  1. Implement INotifyPropertyChanged on your class/view model. All property's that you want to send out a notification for must call OnPropertyChanged("YourPropertyName");

  2. No, only the property that was changed is effected, provided it calls OnPropertyChanged("YourPropertyName");

  3. You can send a view-model provided it's attributed with [DataContract], but you really should be doing that with your models.

Upvotes: 2

Related Questions