tags:

views:

47

answers:

2

Hi,

New to wpf and MVVM .I am kind of confused what belongs to what.

Lets suppose we have a view with a btnSave.

When saving who's the responsability to call the Business Layer ?

My understanding is that Model is just holding properties and no methods ViewModel is actually implementing EG " DelegateCommand SaveCommand and therefore calling the business layer. However I have been told that is actually responsability of the Model to call the business layer.

example taken from josh smith on msdn

Extract from there

      public ICommand SaveCommand
        {
        get
        {
            if (_saveCommand == null)
            {
                _saveCommand = new RelayCommand(param => Save(),param => CanSave);
            }
            return _saveCommand;
        }
    }

    /// <summary>
    /// Saves the customer to the repository.  This method is invoked by the SaveCommand.
    /// </summary>
    private void Save()
    {
        if (!_customer.IsValid)
            throw new InvalidOperationException(Strings.CustomerViewModel_Exception_CannotSave);

        if (this.IsNewCustomer)
            _customerRepository.AddCustomer(_customer);

        base.OnPropertyChanged("DisplayName");
    }

Your views very much appreciated.

+2  A: 

The model is generally considered part of, if not the entire, business layer. So, the ViewModel should call methods in the Model (business layer).

ElectricDialect
Exactly what I was going to say..
Reed Copsey
So,The ViewModel implements the SaveCommand but actually calls the Model.Save .mmmmmBut then the josh Smith example on msdn does the opposite!!!As he Calls the CustomerRepository from the ViewModel not from the model.Look at the post above I have added code there as Could not add it here
The ViewModel works (does the CRUD) with the Business Layer. An excellent article can be found here - http://msdn.microsoft.com/en-us/magazine/dd419663.aspxThis is why I am confused!!
That is not contradicting what I said. The ViewModel accesses methods and properties in the Model. What you're referring to as the Business Layer is just another name for the Model in this case.
ElectricDialect
hiFirst of all thanks for your time for replying.in Josh Smith example in msdn he doesnt use the model to call the customerRepository but the viewModel calls the customerRepository.I have googled a bit more and I can find is some people saying one thing and others saying the opposite.Not a clean cut as i can see.I was hoping it was ,that is why i put a uqestionh in this forum.Any more suggestions?
A: 

The view typically binds to properties in the ViewModel. The ViewModel works (does the CRUD) with the Business Layer. An excellent article can be found here - http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

soulia
Hi,So you are saying what I was thinking but ElectricDialect and Reed Coopsey above think the opposite.I am a newbie on wpf and confused!!!
Hi. Yes, the terminology can be confusing. The referenced article helps clear this up - mvvm is an extension of Model-View-Presenter, which is a variation of Model-View-Controller. The Model is the data. This is consistent across MVVM, MVP, MVC. The View is as dumb as possible. Again this is consistent. The Controller is the heart of the MVC. Its what ties the View to the Model. The Controller receives messages when the user interacts with the View. In the MVP pattern, the Presenter is analogous to the Controller. In the MVVM, the ViewModel does not need a reference to the view, unlike MVP.
soulia