开发者

viewModel vs model who has responsibility to communicate with business layer?

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 responsibility 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 responsibility 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)
            _cus开发者_StackOverflow社区tomerRepository.AddCustomer(_customer);

        base.OnPropertyChanged("DisplayName");
    }

Your views very much appreciated.


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


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


MVVM is a pattern for organizing your presentation layer. How your application persists its data is a different design decision.

It's probably not very nice if a model object is full of SQL statements that map it to database tables. But if these statements sit in a repository and the model knows the repository, that's a fine decision.

Or you can decide to make the model independent of the repository and make retrieving the model data a responsibility of the ViewModel. For small models this is probably cleaner. For larger models it may get tricky for the ViewModel to know how many objects to retrieve before it calls the business methods on the domain object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜