Nikhil Kothari at MIX'10 on MVVM/RIA/MEF
A couple of my colleagues, Erich and Nathan, have been trying to impress upon me the magnitude of Expression Blend and MVVM and the glorious byproduct of having to write less code. Nikhil illustrates time and again in this presentation many of the points my colleagues have advocated.
Another colleague tells me to "check out Nikhil Kothari's MIX'10 presentation. . .there's some WCF RIA Services stuff as it works with MVVM. . .interesting." He was right: it's a beautiful bright future for RIAs; this illustrates one aspect of why and more importantly how. Developers will be able to rapidly deploy applications in an extremely well patterned manner. When i say developers, i'm referring to both developers internal to and external to Microsoft Corporation. This has direct implications on Silverlight for Windows Phone 7
Series RIAs. I think that we're posing ourselves nicely for competition with iPhone. Personally, i'd kill to not have to write an iPhone App. i can't stand their developer experience; It's horrible. Between Expression Blend, MVVM, Visual Studio 2010, and a hundred other reasons, i'd love to pursue the possibility of switching from the iPhone.
ViewModel, RIA Services, and MEF Vision
- Separating your UI from your interesting Application Logic.
- Facilitate Designers and Developers working together.
- Encapsulates the state and operations of your application.
- Improve maintainability
- Facilitate testability
View: Presentation logic--XAML, UI Logic in the code-behind. In the XAML you define a d:DataContext (the design time data context) to the appropriate Type attribute value (and a flag for IsDesignTimeCreatable) lets the IDE know about what types of Properties and Methods so that the data binding picker works.
The IDE properties pane for the UI Element allows you to set the ItemSource; the presented options are Properties off of the d:DataContext Type definition. An alternative to handling Command Operation to Method Invocation mappings, by configuring Methods in the ViewModel using ICommandProperties Attributes, is to define such mappings in the Page Resources definitions in the View XAML. Once you have those definitions in the View XAML, the IDE allows the Page resource to apply Command operations as a resource in the Properties pane of the IDE. The CommandParameters attribute in the Properties pane can be bound to the another UI element, allowing the Command (Method Invoke) to take in parameters.
View Model: App State, Operations, Change Notifications. Any class that implements INotifyPropertyChanged can serve as a View Model, Nikhil demonstrated that he uses ViewModelBase. An Instance of the View Model serves as the DataContext for your UI/View, (hooked up immediately following initialization of the View); this is more or less a refactoring, more like obsolescence, of the code-behind file for the View.
States and Operations map to the ViewModel Class Methods and Properties.
Data Model and MEF: Services, Business Rules. RIA Services detects from your Data Model Class Attribute definitions for, optionally custom, validation rules. This partial, server side shared code, gets propagated to the client along with metadata intent for the validation rules. The Model Entity Framework handles propagating that server side validation rule to the client generated code stubs. Database field constraints in the schema (VARCHAR(10)) can cause these validation rules to show up in the client stub also. This constitutes client side validation. You're going to add other validations that can only be run server side. The ability to explicitly configure CRUD invocations using Attributes in the client illustrates how you can separate client to server communication logic from contextually named methods for readability. On EITHER the server side OR the client side partial classes allow individual for deviations/customizations of the Entity depending client vs. server side distinction subtleties. Entity State can be bound to a UI Element to indicate entity state in a visual indicator/decorator can be done with little code and can be done in the IDE rather than in boring ugly, and tightly coupled source code.