Posts Tagged ‘MVC’

Composite WPF Patterns

Saturday, July 19th, 2008

Microsoft’sComposite Application Guidance for WPF (Composite WPF) gives WPF a lightweight yet effective application block with which to build exciting applications. However, the biggest struggle is deciding on a pattern for applying the library. The Model-View-ViewModel (MVVM), mentioned across the web as the perfect pattern for implementing MVC with WPF, was for some reason not specifically mentioned in the Composite WPF documentation. Nevertheless, the MVVM pattern is many times used and alluded to under the name PresentationModel and is arguably the best approach in most cases.

Dan Crevier posted a series of excellent articles defining the pattern and giving examples of the implementation of MVVM in WPF. These were written several years ago, before development of the Composite WPF block, but they are simple to understand and can give added understanding to the objects included in Composite WPF. For example, his post on encapsulating commands is very similar to the DelegateCommand object provided with CompositeWPF, though the latter uses lambdas to define Execute and CanExecute methods, whereas the former uses a base class and inheritance to define the methods. Using Dan’s example, the equivalent DelegateCommand, contained within the DelegateCommand’s definition, would look something like this:

public DelegateCommand<object> MyCommand { get; private set; }

public Window1()
{
    InitializeComponent();
    MyCommand = new DelegateCommand<object>(
        p => {
                 string text = p as string;
                 // Do something with text
             },
        p => !string.IsNullOrEmpty(p as string));
}

Dan’s definitions of the Model, which he calls DataModel, View and ViewModel match very closely with Composite WPF’s PresentationModel pattern. In this case, the DataModel is a representative object that is fit for display within the View. Using the entities generated for Linq to SQL, you might add properties to the partial classes create the necessary properties to which to bind. These classes should also implement INotifyPropertyChanged so that other DataModels can respond to changes from another, related View and update their data accordingly.

The View should use bindings for everything. The beauty of WPF is truly in its DataBinding functionality. The observer pattern is built right into the Binding, so that whenever one view changes, other views are notified by their bindings to their DataModels. Even the actions to be performed on a user interaction with the UI can be removed to the ViewModel, DataModel, or in the case of Composite WPF, a controller, Shell, or App level, as appropriate. This leaves the View looking very passive, indeed, which greatly helps when unit testing.

The ViewModel is the actual PresentationModel. It hosts the DataModel, as well as the View’s commands. ViewModel should be the object used to bind to the View’s DataContext and should implement INotifyPropertyChanged if any of the properties could change. The QuickStarts and RIStockTrader examples provide excellent samples of the PresentationModel, though be warned that the examples often use different patterns, if for no other reason than to show other pattern implementations.

That covers the MVVM pattern. However, there’s one more aspect that needs to be covered: composite regions. Composite WPF provides regions for managing the display of different modules in the Shell, but what if you need to display multiple modules or views within a “summary” view that is already on the Shell? In this case, you can use a Controller to manage the child use cases. The UIComposition QuickStart shows a terrific example of creating a controller in a ViewModel for the purposes of managing child use cases. This removes the dependencies and depth of a View that includes a tab control containing a large number of child use cases. (The UIComposition QuickStart uses a Supervising Controller pattern instead of the PresentationModel/MVVM pattern, so you’ll have to adjust it accordingly, but it works beautifully.)

The MVVM pattern is a great solution for WPF applications by allowing WPF’s DataBinding and Commanding to remove much of the logic formerly found in the View layer out to a unit testable ViewModel. The architecture stays clean and also fairly shallow in that an additional Presenter class is unnecessary (as opposed to the Supervising Controller pattern.)

Additional Resources:

Macro- vs. Micro-MVC Architectures

Wednesday, December 5th, 2007

I was reading today on SQLServerCentral.com about SQL Server’s XQuery capabilities. I was quite fascinated. Microsoft has seemed somewhat slow on the XML uptake, so including XQuery in SQL Server 2005 was somewhat of a shock.

However, I was struck with an idea after reading the article. This type of functionality puts SQL Server on a level with eXist as an XML database server. By this I mean that lots of opportunities now open up to serving XML to a client application instead of using a lot of database resources. (For those unfamiliar with eXist, it has built in REST, SOAP, and WebDav services available out of the box; SQL Server has SOAP through the CREATE ENDPOINT statement.)

This led me to thinking again about the MVC design pattern and an ideal way in which to set up that pattern. I came to the conclusion that there are macro- and micro- versions of the pattern. Most of the time MVC is mentioned, it is in the context of keeping everything relatively within one language (e.g. ASP.NET MVC, Ruby on Rails or XForms). That would be an example of a micro-MVC pattern.

A macro-MVC pattern could be the separation of XML documents (models), Javascript (controller), and HTML + CSS + XSLT (views) as found in the freja framework. Most of those components are often seen in web development but not often considered when discussing MVC. Typically, only the language that produces the XML would be considered worthy of an MVC designation, if it also produced the other pieces.

Now to the point: one might use SQL Server to produce the XML models, C# classes to perform the duties of the controller (i.e. parsing URLs, XML, etc. and feeding the views), and either XForms or the HTML + CSS + XSLT trio for views. The best thing about this combination over using views with eXist is that SQL Server still stores its data in a relational database. A DBA could create the stored procedures necessary to create the XML documents and update the underlying tables, thereby keeping all of the business logic in one domain and truly separating models from controllers and views.

I’m going to make an attempt at using this methodology soon, probably with XForms as the view layer. I will be sure to post my experiences when I get further along.

Multiple-Model Forms in Rails

Monday, December 3rd, 2007

I don’t know how many others have tried this, but trying to create a form in Rails that uses multiple models is quite a challenge. It can certainly be done, but every method I’ve come across so far requires Javascript. What’s wrong with that? Nothing, unless some of your users have Javascript disabled. If you are not concerned with progressive enhancement (or even graceful degradation), you can try Ryan Bates’s excellent three-part tutorial at Railscasts (part 1, part 2, part 3). If such things matter to you, however, you appear to have two options: MCC or XForms.

I’ve so far tried Ryan Bates’s method to great effect, but it’s just not the route I want to go, and I can’t find an elegant way to start without Javascript. MCC looks really interesting, but now I’ve got to go to another layer of abstraction. Of course, with XForms, I will need a browser plugin, but I really like XForms, so I’m going to try it and see what I can make of it.

(Those familiar with XForms will likely point out that I’m now adding multiple levels of abstraction with XForms since it is, like Rails, also built on an MVC pattern. However, I’m choosing to look at this as constructing Rails web services and using another “app” to handle user interaction. Either way, they are probably right. I just want to have fun with it.)


Panes of Glass is using WP-Gravatar

Panes of Glass is Digg proof thanks to caching by WP Super Cache!