Thin ViewModels

Introduction

I remember when I first started using MVVM I found myself putting not only view logic, but small pieces of business logic in my viewmodels.  It was testable and reduced the number of classes related to the front end.  These viewmodels were not blobs, but they did take on more responsibilities than a single-responsibility class would have.

Then in 2013 I changed my application design so that any business logic was relegated to a business logic class that was injected into the viewmodel.  My viewmodels were now leaner and followed the single-responsibility principal much closer.

Motivation for Thin View Models

Motivation for thin viewmodels is strong and simple: cross-platform.

Given the advent of Xamarin.Forms and its capability to author applications for UWP, IOS, Android, and OS X (Xamarin only), as an application architect I would strongly recommend that applications put their business logic in Portable Class Libraries (PCL) and keep their viewmodels thin.  If you do this, you can also share that same business logic with desktop platforms like WPF or Windows Forms.

If you have platform specific logic you can abstract it behind one or more interfaces and then inject it into the proper layer.

I’m excited about Xamarin.Forms and the potential it has for cross-platform development using .NET programming languages.

While Xamarin.Forms does not currently work with OS X, the PCL libraries you author will.  You’ll need to Xamarin Studio to author the OS X UI.

Using this architecture also gives you the advantage of unit and integration testing on a single UI agnostic shared code base.

Close

In the end its about giving yourself, your team, your company the option and capability to meet market driven cross-platform requirements without a rewrite.

Exciting times to be an architect and developer.

Hope this helps someone and have a great day

Just a grain of sand on the worlds beaches.

2 comments

  1. Mo

    Good insight and I agree. From a “best practices guide” perspective and based on your experiences with MVVM is it possible to enemurate the set of functionalities/behaviours that the VM should be responsible for while still remaining Thin?

    Thanks Carl.

    Like

    • Karl

      This is one of those “it depends” questions. What drives the difference for me is the type of app and the scenario the VM is handling.

      When I write custom tools, my view models can have much more code than a line of business application due to the nature of the requirements and implementation.

      For my Line of Bussiness apps my view models:
      provide properties for all view data requirements
      provide properties for all view commands
      calls business layer for CRUD operations, calculations, etc.
      calls into services for logging, showing a dialog or requesting navigation (I normally have methods in view model base classes that can handle most of this so the VM does not have to have the code)
      if required, uses the EventAggregator to post required messages to the system

      The above keeps the view model thin as requirements and new features get added.

      Thin can also be interpreted as “not complex.” When you see the view model methods getting long, you know it’s not thin.

      Hope this helps. Best to you,

      Karl

      Like

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s