Tagged: MVVM

Ocean Validation for Xamarin Forms

Introduction

This blog post is about an example Xamarin Forms project I wrote to show using Prism, Unity, and Ocean Validation in several scenarios.

I decided to make this a video blog post and have six videos for you.

You learn the following:

  • Using the sample Xamarin Forms app and understanding the line of business scenarios it covers.
  • Using Prism and Unity in a real-world Xamarin Forms App
  • Dependency injection in a real-world Xamarin Forms App
  • Abstracting Static services and Static Device information behind interfaces to promote testing and maintainable code
  • Using Prism NavigationService and PageDialogService
  • Async Await contrasted with the Promise pattern in a Xamarin Forms App
  • Ocean validation framework with Xamarin Forms
  • Using IDataErrorInfo to surface validation errors for properties or the entire object.  (Yes, IDataErrorInfo!)  This also works for UWP.  These two platform’s binding pipeline lack data validation.  No worries, it’s very easy to do now.
  • Using Ocean validation to validate an object in multiple states.  Multiple validation states imply that the object is going through a workflow, where an object can be valid for a certain state, but not valid for other states.  This is a common scenario in complex business applications like insurance claims or when an object is completed over several Xamarin Forms pages.
  • At the end of November 2016, the Xamarin Forms ListView view began to have some problems that cost me over a day.  In the last video, I explain the issues and what I did to get around them.  I also wrote up bugs and communicated with Microsoft about these issues.

Introduction to Ocean for Xamarin Forms

Prism and Dependency Injection in Ocean for Xamarin Forms

Async Await and Promise Pattern in Ocean for Xamarin Forms

Introduction to Ocean Validation Framework for Xamarin Forms

Ocean Multi-Object State Validation for Xamarin Forms

Xamarin Forms ListView Troubles (as of 1 Dec 2016)

Source Code

https://github.com/Oceanware/OceanForXamarinForms

Close

I hope these videos have helped with your understanding of Prism, Unity, and Ocean in Xamarin Forms.

Have a great day,

Just a grain of sand on the world’s beaches

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.

ViewModel Tuck Away for Visual Studio 2015

I recommend that you use this better extension instead of mine:  https://visualstudiogallery.msdn.microsoft.com/3ebde8fb-26d8-4374-a0eb-1e4e2665070c

 

vmtuckawayscreenshotBackground

I like to tuck my viewmodels beneath the view in the Visual Studio Solution Explorer. To me, it makes for nice organization and condenses the number of top level files I have to view in the Solution Explorer.

Most of my viewmodels have a one-to-one relationship with its view. For those that don’t, I don’t tuck them beneath.

I also like to name my views and viewmodels according to the pattern picture in this image. For me, it keeps things clean and consistent.

Operation

To tuck or nest a viewmodel under it’s corresponding view:

  • Select the view and viewmodel file in the solution explorer
  • Execute the ViewModel Tuck Away command

ViewModel Tuck Away will verify that:

  • Two files have been selected in the Solution Explorer
  • That one file has a .xaml file extension and contains the word “viewmodel”
  • That one file has a .cs file extension and contains the word “view”

ViewModel Tuck Away will then tuck the selected viewmodel beneath the selected view.

During the execution of the command, the Visual Studio Status Bar text will be updated to reflect the success or failure of the command.

I wrote this to enforce naming standards at my work and home projects.

I know that some (many) may not like how the naming is enforced. No problem, the source is on GitHub (see Source below) and you can very easily modify it, rebuild the solution and double click on your new VSIX file.

 ViewModel Tuck Away Command

You can execute the command in three ways:

  • Use the Tools Menu, ViewModel Tuck Away
  • Add the command to a Toolbar
  • Provide a shortcut key for the command

By default the command is added to the Tools Menu:

vmtuckawaymenu

To add the command to a Toolbar, right click the Toolbar and select Customize and then follow the numbers below. #1 select the Toolbar Radio Button.  #2 select the Toolbar you want to add the command to.  #3 click the Add Command button and the next dialog will appear.

vmtuckawayaddcommand

Select the ViewModel Tuck Away Command, click OK, and then close the above dialog.  The command will now be on the Toolbar.

vmtuckawayselectcommand

If you want to you can also use the Tools, Options dialog to add a keyboard shortcut for the command. #1 select Keyboard.  #2 search for “viewMo” #3 click the Press shortcut keys TextBox and then press the desired key combination.  #4 click the Assign button and then OK the dialog.

vmtuckawayaddkeyboardshortcut

Download from Visual Studio Gallery

ViewModel Tuck Away for Visual Studio 2013

ViewModel Tuck Away for Visual Studio 2015

GitHub Source

The source for ViewModel Tuck Away for Visual Studio 2015 is here.

Close

Hope this helps someone and have a great day

Just a grain of sand on the worlds beaches.