Much Ado about Nothing

While the above heading might refer to the comedy by William Shakespeare about two pairs of lovers in 16th century Sicily, it also describes my feelings about Model View View-Model (MVVM) frameworks. Now before I get accused of blasphemy in the high court of WPF and Silverlight programmers, let me explain myself. For those of you who know me and have read my previous blogs, you know that I am a strong supporter of MVVM. In fact, I believe that no serious WPF or Silverlight application should be written without using the MVVM pattern.

There is a different between the MVVM pattern and MVVM frameworks. The pattern is there to descibe the concepts of how the application should be built and how the various pieces of the application should communicate back and forth. A framework is just a set of software modules that attempt to make using the pattern easier. it is like building a house from scratch or using a pre-fab. Both methods produce a house. Supposedly the pre-fab is easier to build because the pieces are all there in a package and precut with instructions on how to assemble them together. But you pay a price for using the pre-fab, the building process becomes a cookie cutter technique and all the pre-fab houses of the same model look the same. In addition, you are limited to how the house is designed because the pieces are already pre-cut and you receive just what you need and no more.

Now having said all that, last weekend, I began a new Silverlight/WPF project. I thought that I would try using one of the MVVM frameworks to make the programming process easier. Hmmm which one to use? There are many good ones out there and some not so good ones. I looked at three or four of the more popular ones. I noticed several things about each of them. They are all free. Now I have no problem with free. I will gladly take free over spending money any day. But free also means that you get what you pay for. These frameworks are being developed by individuals in their spare time while they have other jobs which pay their salaries and allow them to survive. I could find no evidence of companies developing any of the more popular frameworks. In the past, Microsoft had attempted to develop several frameworks and they swiftly died out leaving developers out on a limb. All of the frameworks are in various stages of development. None are fully functional yet. They are in various stages of being built. None have fully published documentation or full blown real-life examples.

Now don’t get me wrong. I am not putting down any of the frameworks nor the developers working on them. They might work very well for them and their needs. But last weekend showed me that the existing frameworks as is are not for me at this time. Maybe in the furture, if the frameworks become fully functional, the documentation is complete and real life examples are available, I might change my mind and use one. As I tried learning to use the one that I selected, I quickly became frustrated because of the lack of documentation and good examples. I realized that in addition to learning the ins and outs of programming the application design, learning the intricacies of the control set and the entity framework, there would also be a steep learning curve for the framework and my application fits into the framwork. The question that struck me, was it worth it? I decided no. I would build my own “framework”.

Building my own framework did not mean that I was going to develop a framework kit like the ones that I was rejecting. It meant that i would figure out how to use the MVVM pattern without using a framework software kit. Fortunately I had chosen two 3rd party control packages that would make my work easier. For the entity framework and backend services, I am using DevForce Universal from IdeaBlade. For the UI controls, I am using ClientUI from Intersoft. I discovered this toolset last fall and it remains my favorite among WPF/Silverlight controls. In fact, it is the only set of controls in my toolbox for Visual Studio 2010. In the past I have used Infragistics, Syncfusion, ComponentOne, and Telerik. But the Intersoft has all the controls I need. Well, there is one I wish they would create and that is a schedule/Calendar control. But that is beside the point. As for DevForce Universal, there is no way that I would consider creating a database applcation with using it. While there is the Entity Framework from microsoft and various other ORMs like EntitySpaces and Telerik’s ORM, they pale in comparison to DevForce. Using DevForce makes developing the entity model a relatively easy task and makes commmunication with the database server so much easier than having to use WCF-RIA services.

So now I have the UI controls and I have software for developing the entity framework and the backend services. With ClientUI, I am able to build the view classes. With DevForce, I am able to build the data model, the repository class(es), and communicate with the back end through a piece of software called the Business Object Server. Using C# code, I am able to build the view model classes that provide the application state and access the repository for data requests. Now I needed a way to communicate commands from the view class to the view model class. As an example, on the view. I have a Save button.

In the past, when the user had finished entering data on the view and clicked on the Save button, I had a clicked event that was triggered and code in the code behind module that would receive the clicked event, do some initial processing and call a controller method to complete the save process. But that meant that I had code in the view module, even if it was the code behind portion, and the view was tightly bound to the view model. What if I wanted to use the same view model in say a Windows Phone 7 application. My view would have to change to fit the size and controls of the phone, but i would want the code in my view model and calls to the repository to stay the same. But if the view nad view model were tightly bound togehter that might be difficult to do.

Thanks to the foresight of the developers at Intersoft, the answer was right in front of me. All the Intersoft controls are built from the ground up with the MVVM pattern. For example, the UXButton has a commmond property. So in my xaml, my definition of the button includes this syntax

<Intersoft:UXButton Content="Save" Width="80" IsDefault="True" DialogResult="None" Command="{Binding SaveCommand}"/>

In the view model for this view, I have the following code.

public DelegateCommand SaveCommand {get; set;}

In my constructor I instantiate the SaveCommand object.

SaveCommand = new DelegateCommand(Save, CanSave);

Now I create two methods, CanSave() which takes as an argument object parameter and returns a bool and determines if the button is available to the user or disabled. My second method is Save() which returns a void and takes an object parameter as a parameter. This is the method that actually will do the save.

Here is an example from the current project for the CanSave() and Save() methods for the Member class.

       private bool CanSaveMember(object parameter)
        {
            if (_isNewMember && !_isDirty)
                return false;

            if (_requireValidate)
            {
                _lastValidateResult = this.Validate();
                return _lastValidateResult;
            }

            return _lastValidateResult;
        }

        private void SaveMember(object parameter)
        {
            DetachEventHandlers();
            this.IsBusy = true; // make page busy during save
            Repository.Save(SaveSucceeded, SaveFailed);
        }

        private void SaveSucceeded(IEnumerable entities)
        {
            this.IsBusy = false;
            DetachEventHandlers();
            this.MemberEditDialogResult = DialogResult.OK;
            CloseEditView();
        }

        private void SaveFailed(Exception error)
        {
            this.IsBusy = false;

            string message = error == null
                                 ? "Save Cancelled!"
                                 : "Error: " + error.Message;

            DisplayMessage(message);
        }

All of the Intersoft controls have a mechanism for sending a notification to the view model that something has taken place on the view. The View Model listens for the notification, acts upon it and then using the INotifyPropertyChanged interface, the view is alerted that the state has changed in the view model and the view needs to refreshed. Having this pattern built into the controls, I have no code in the code behind class. My view knows nothing about the view model and my view model knows relatively little or nothing about the view. And with the recently added Event Aggregator class from Intersoft, I have pretty much everything that the MVVM frameworks have without the learning curve.

Instead of spending this week learning how to use a framework, I spent this week producing code. In fact, coding the new application along with all the other things that I have to do for the business, I was able to produce about 7,000 lines of code according to the metrics count of the solution. Rather than spending time learning the framework which doesn’t really help me reach my goal of deploying the application, I am 7,000 lines closer to deployment. As I said, maybe in the future, my views about MVVM frameworks will change and I might learn how to use one. In the meantime, I feel they are “much ado about nothing”.

I am not an employee of IdeaBlade or Intersoft. I am the president of a company that develops software for labor unions. I am constanly on the lookout for tools that make my job more productive and efficient. Both IdeaBlade and Intersoft fulfill that purpose. I cannot say enough good things about them. I am proably one of their biggest cheerleaders.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

6 Responses to Much Ado about Nothing

  1. This post is a little over the top. Did you by any chance take a look at Caliburn.Micro when doing your research? It is hardly cookie cutter at all, extremely customizable, you can cut out or fully replace pieces wherever it doesn’t work for you and will save you an untold amount of hours. Tons of excellent documentation, tightly and beautifully integrated with MEF, and available on Nuget to have to set up in 2 minutes flat. All coming in under 80kb. I would rarely ever build another mvvm project without it.

    • Bill Gower says:

      I took a look at that one and several others and just decided that frameworks were not for me at this point in time. My current app is working nicely under MVVM and i would bet that it is just as easily maintainable and scalable as one using a frameowrk. A frame work is not required to write apps with the MVVM patterm.

      • I suppose, I went through a number of different frameworks and nothing came close to matching the sheer time saving power of caliburn.micro. Things like the ability to bind to and run commands from any VM in the MEF container, lightweight scripting to pass all sorts of the parameters from the view to the Commands, to the automatic convention based binding system that is 100% extensible. I strongly suggest that it would be worth your time to at least take a look at the documentation and what is possible. Your current app may work nicely but I happen to be a huge fan of pumping my applications out at fast as possible while dealing with the least amount of BS and boilerplate I can.

  2. Derek says:

    I use to think the same until I discovered Caliburn Micro.

  3. jimmyps says:

    FYI, Intersoft will release its next-generation, MVVM-ready scheduler lineups in the upcoming R2 release.

    Stay tuned!

  4. Pingback: New Silverlight Tutorials: Building business applications with ClientUI and DevForce | Intersoft Solutions Corporate Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s