Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
MVVM Survival Guide for Enterprise Architectures in Silverlight and WPF

You're reading from  MVVM Survival Guide for Enterprise Architectures in Silverlight and WPF

Product type Book
Published in Aug 2012
Publisher Packt
ISBN-13 9781849683425
Pages 490 pages
Edition 1st Edition
Languages

Table of Contents (21) Chapters

MVVM Survival Guide for Enterprise Architectures in Silverlight and WPF
Credits
Foreword
About the Authors
About the Reviewer
www.PacktPub.com
Preface
1. Presentation Patterns 2. Introduction to MVVM 3. Northwind – Foundations 4. Northwind—Services and Persistence Ignorance 5. Northwind—Commands and User Inputs 6. Northwind—Hierarchical View Model and IoC 7. Dialogs and MVVM 8. Workflow-based MVVM Applications 9. Validation 10. Using Non-MVVM Third-party Controls 11. MVVM Application Performance MVVM Frameworks
Binding at a Glance Index

Chapter 5. Northwind—Commands and User Inputs

By Ryan Vice

Getting user input from the view to the view model is one of the many challenges of implementing MVVM especially when you want to keep it pure and go with a no code-behind approach. In this chapter, we will look at how the command infrastructure helps make this task easier and where the command infrastructure falls short. We will look at several ways of dealing with the shortcomings of the commanding infrastructure, examining both code-behind and no code-behind approaches as well as looking at how the MVVM Light framework can make our life easier.

Pure MVVM


Currently, we are wiring up our event handlers in the code behind instead of taking advantage of the command infrastructure to pass user input from the view to the view model. There's a lot of talk in the development community as to how much code is alright in the code behind and I won't attempt to end that debate here, instead what I will do is, provide the tools and techniques that allow for taking the pure approach. This will allow you to decide what the best approach for your project is and use whatever level of purity makes the most sense.

That said, in my projects I prefer to keep it as pure as possible and I am yet to find a situation where I had to put code in the code behind. The following are a few reasons that I favor the pure approach apart from just being a bit of a purist at heart:

  • Enforcement: On a large project, you will have developers with differing levels of skill and ambition and what happens without fail is that if you allow for the code-behind approach, it...

Making it easier with frameworks


We can save ourselves a little effort by taking advantage of one of the many MVVM frameworks freely available on the Web.

Tip

See Appendix A, MVVM Frameworks for a list of frameworks.

We are now going to update our code to use the MVVM Light framework by following the steps mentioned next:

Tip

If you haven't downloaded the MVVM Light framework then download the framework. See Chapter 2, Introduction to MVVM for details. Also note that there is now an MVVM Light Nuget package (http://nuget.org/packages/mvvmLight) available, which would be the preferred way to install the framework..

  1. Copy GalaSoft.MvvmLight.WPF4.dll to the Lib directory in the solution, as shown in the following screenshot. This assembly contains the RelayCommand class that we will be using.

  2. Add browse reference from Northwind.ViewModel to GalaSoft.MvvmLight.WPF4.dll.

  3. Delete the Command.cs file from Northwind.Application.

  4. Open MainWindowViewModel.cs, add a using statement for GalaSoft.MvvmLight.Command...

Updating customer details


Let's go ahead and add some code to allow for updating customer details by following these steps:

  1. Update ICustomerService to add the operation shown in the following code:

    [OperationContract]
    void Update(Customer customer);
  2. Update CustomerService to implement the new operation as shown in the following code:

    public void Update(Customer customer)
    {
        Data.Customer customerEntity
            = _northwindEntities
                .Customers.Single(
                    c => c.CustomerID == customer.CustomerID);
        customerEntity.CompanyName = customer.CompanyName;
        customerEntity.ContactName = customer.ContactName;
        customerEntity.Address = customer.Address;
        customerEntity.City = customer.City;
        customerEntity.Country = customer.Country;
        customerEntity.Region = customer.Region;
        customerEntity.PostalCode = customer.PostalCode;
        customerEntity.Phone = customer.Phone;
        _northwindEntities.SaveChanges();
    }
  3. Build and then right-click on the CustomerService...

Gestures, events, and commands


Classes that expose a command property in WPF and Silverlight are implementing the ICommandSource interface that is shown in the following code:

//     Defines an object that knows how to invoke a command.
public interface ICommandSource
{
    //     The command that will be executed when the command 
           source is invoked.
    ICommand Command { get; }
    //     Represents a user defined data value that can be 
           passed to the command when it is executed.
    object CommandParameter { get; }
    //     The object that the command is being executed on.
    IInputElement CommandTarget { get; }
}

One major limitation of the commanding infrastructure is that ICommandSource only allows for one action on a command source to be associated with a command. So, for example, if you want to have commands executed for both left-click and right-click on a button, you wouldn't be able to accomplish that using the Button.Command property. This limitation is...

Summary


We are now well-equipped to handle user interactions. We have reviewed a variety of approaches for dealing with the shortcomings of the commanding infrastructure. On your own projects you will need to figure out what is the best approach for your team and requirements.

lock icon The rest of the chapter is locked
You have been reading a chapter from
MVVM Survival Guide for Enterprise Architectures in Silverlight and WPF
Published in: Aug 2012 Publisher: Packt ISBN-13: 9781849683425
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime}