Develop MVVM Mobile Apps faster with MVVM Atom

Develop MVVM Mobile Apps faster with MVVM Atom

With the clear separation of concerns and better testability, MVVM design pattern is gaining wider acceptance in the developer’s world. There are two common ways to develop your view model classes

1.   Write your base view model, other base classes and use them or

2.   Use an available framework such as MVVMCross, MvvmLight, etc.

Writing it yourself can take longer due to the learning curve and efforts to write them from the scratch. Available frameworks are large in size and there is a learning curve to learn them. On the top of it, due to large size, they take longer to start. Having multiple layers causes a delay in processing user actions. This can be acceptable in the case of desktop solutions, but not in the case of mobile apps where users expect a faster response.

MvvmAtom provides the middle ground by providing the base classes and wiring for most commonly used MVVM features. That results in a short learning curve with quicker development. Having fewer layers would make it more responsive.

Apart from the above, MvvmAtom provides a unique feature that enables a command to enable/disable itself when a property change occurs in the view model. This avoids having view model track relation between a property and the command(s) that may get affected due to change in its value. It provides clearer centralized decision making in commands rather than sparsely structured decision making in the view model.

Here is how the code would look in a typical framework vs MvvmAtom. (Shortened for brevity)

Typical

public String UserName
{
   set   {
    ...
    RaisePropertyChanged();
    CopyCommand.RaiseCanExecuteChanged();
    NavigateCommand.RaiseCanExecuteChanged();
   }
}
public String UserName
{
   set   {
    ...
    RaisePropertyChanged();
    NavigateCommand.RaiseCanExecuteChanged();
   }
}

MVVM Atom:

public String UserName
{
   set   {
    ...
    RaisePropertyChanged();
   }
}
public String UserName
{
   set   {
    ...
    RaisePropertyChanged();
   }
}

MvvmAtom classes and interfaces are explained here in brief:

AtomViewModelBase

This is a base view model class that provides an implementation of the INotifyPropertyChanged interface. The caller property need not specify its name when notifying listeners. The compiler takes care of extracting the name e.g. set will simply call RaisePropertyChanged();

Apart from it, the base class also has virtual methods that can get called with the view appears/disappears. These methods need to be explicitly called by the View.

AtomCommandBase

This is the base class for commands. This stores the parent view model as a member. So it is accessible during the lifetime. A command will register itself with the view model by calling AddPropertyChangeListener API. Any property change will be notified to the command. It will enable commands to take appropriate action(s).

It provides a virtual method “EvaluateCanExecuteChanged”. This method gets called by the view model when a property change occurs. The inherited class should override this method to make any decision based on the new property(ies) value(s). (E.g. Enable Login button when both UserName and Password have the minimum required length.)

Apart from it, the class provides standard methods to raise the CanExecuteChanged event, perform Execute and CanExecute.

(Note: The class registers itself with the ViewModel using IAtomCommandBase interface. So if ViewModel needs to be unit tested, it can be done using mock commands. This effectively avoiding dependency on the actual implementation.)

IAtomNavigationService

One of the major drawbacks on MVVM design pattern is that a view model cannot navigate a user to a different view as View Model is not aware of the View. In order to provide the functionality, the library provides an interface that specifies the target view model. The App can implement the interface to provide the required functionality specific to the platform.

Also, unit test can implement mock to test navigation functionality.

You can find the NuGet package here:

https://www.nuget.org/packages/MvvmAtom/1.0.0

You can find Xamarin Forms example here:
https://github.com/sameerkapps/MvvmAtom/tree/master/MvvmAtomSample

Code + sample is on the GitHub:

https://github.com/sameerkapps/MvvmAtom

If you have any questions, please post them in the comments.

I have used the MVVM pattern with Xamarin.Forms and find it relatively straight forward with a few base classes that I copy and paste between each project. However, I like the idea of using some lightweight library and yours looks like one of the few that truly is clean and lightweight for me to consider. From my understanding, I think that I could use MVVM atom to declare my MVVM classes independently of Xamarin Forms dependencies? For example, they have their own Command implementation that ties you to Xamarin.Forms, so I like the idea of using your Command assuming it works with Xamarin.Forms as-is too?

Like
Reply

Has there been any examples of using it in Xamarin.Forms UWP, Android, IOS Shared or PCL projects?

Like
Reply

To view or add a comment, sign in

More articles by Sameer K.

  • Functional Testing of Business Logic Workflows

    Problem Ever had this happen? Unit tests pass. Integration tests pass.

  • Getting Started With IoT using Python

    Python is an easy to learn language. If you use Python and want to get started in IoT, this series will help you get…

  • One Year After Rejoining Microsoft

    I rejoined Microsoft a year ago after working at other companies for 7+ years. The Microsoft that I rejoined is very…

    2 Comments
  • Build 2019 – New and Upcoming in Xamarin

    This is what I gathered from the talk “The Next Transformation in Mobile Development with Xamarin” from Build 2019. It…

  • Upgrade .csproj File

    Historically format of .csproj file has been complex and the developers dread to edit it.

  • Reflecting on 2018!

    Here is a brief reflection on 2018! Keeping it short and sweet. What I accomplished 100K+ downloads of secure storage…

  • Android download analysis using U-SQL

    When a developer obtains the reports of an Android App, it comes as a set of CSV files. "Downloads" file contains…

  • Resurrection of Netduino

    With the latest acquisition of the Netduino by Wilderness Labs, there is an attempt to resurrect Netduino. My…

    1 Comment
  • Unit Testing View Models that use Xamarin Plugin(s)

    Xamarin Plugins provide a wide range of cross-platform functionality libraries. The platform-specific functionality is…

  • What’s new in Secure Storage 2.0?

    Secure Storage NuGet package was created to provide a cross-platform library to store sensitive data. It has easy to…

Others also viewed

Explore content categories