Xamarin MVC Project Separation & Fluent Validation – Part III

Continuing from Part II:

Finally, I’ll show you the actual validator implementation and that BaseType<T> you saw earlier.

Validator 

 

public class UserProfileValidator : AbstractValidator&lt;UserProfile&gt;
{
	public UserProfileValidator()
	{
		RuleFor(a =&gt; a.FirstName)
		.NotEmpty()
		.WithMessage("First Name required.");

		RuleFor(a =&gt; a.LastName)
		.NotEmpty()
		.WithMessage("Last name required.");
	}
}

Continue reading “Xamarin MVC Project Separation & Fluent Validation – Part III”

Xamarin MVC Project Separation & Fluent Validation – Part II

Continuing from Part I:

Fluent Validation 

So now we’ve got a project setup that shared our models and services, it’d be great to same the same validation for inputs or fields with minimum effort across multi-platforms.  Meaning if a “FirstName” is required, it would be great to not code that three separate times (Web, Android, and iOS).  With Fluent Validation we can achieve this with a little bit of setup on the mobile side.  On an .NET MVC site integrating FluentValidation is as easy as installing the correct NuGet package for your version of MVC.  It will integrate with un-obtrusive jQuery Validation automatically.  With Xamarin.Forms currently there is no solution to integrate so nicely.  However, I’ve created a framework that allows you to do something similar and have it work across the three platforms.

So one of the things you need in order to show validation is a Label, which will serve as our “Html.ValidationMessageFor” control in the web world and that label needs to be associated with some type of input.  Within the .NET MVC Platform this is achieved by a naming convention, however, we can’t necessarily achieve the same thing in Xamarin.Forms when we create our views via code, not XAML; but we can create our own naming convention!

Before we dive into the code, you should know that under normal circumstances you don’t new up a “Validator” (term for class that extends AbstractValidator) but you can! When you do there is a method you’d call called “Validate()”.  This method returns a “ValidationResult” with two properties: “IsValid” and “Errors” and “Errors” is a collection of “ValidationFailure”, which contains the Name and ErrorMessage for a particular property.  Knowing that we can create our own “unobtrusive” validation setup.  Lets begin!

Continue reading “Xamarin MVC Project Separation & Fluent Validation – Part II”

Xamarin MVC Project Separation & Fluent Validation – Part I

Recently at my job, we’ve decided to make a new project base for our company. We currently have an MVC 3 base, but considering MVC 5 is the new norm, it is time we get caught up with the latest and “greatest” (greatest used loosely). We decided that we were going to change our strategy with communicating with the database. Currently we use an ORM provided by Microsoft called Entity Framework. Entity Framework is a great ORM, however, when dealing with multi-million(s) set of records we’ve found it to be sub-par. I leave that for another topic at a later date.

The goal of this semi tutorial is to outline a project setup that has a model, service, and data layer, with the model and service layer being reusable from a MVC site to an Android and iOS app. However, with this project setup you could easily implement a WPF application, Silverlight, or even console apps. Resulting in a true “N-tier” architecture.

Below are some of the technologies that are going to be talked about and I’m going to assume some knowledge on them:
1.) AutoFac
2.) FluentValidation
3.) SqLite.NET
4.) Xamarin
5.) NHibernate

Project Setup

First thing we need to do is create a new solution and add two Solution Folders.  Create one for “Web” and one for “Mobile”, we separate these just for our convenience.  Then we need to start adding projects to the new solution.  Using Visual Studio 2015 you should have everything you need built in to your templates if not you may need to download Xamarin from here and of course make sure you have NuGet installed in Visual Studio.  Next you’re going to add four Portable Class Libraries and suffix them with “*.Configuration”, “*.Models”, “*.Services”, “*.Validation”. It is imperative that these be a “Portable Class Library” in order to get this to work correctly.  Below are the options you will want to set for all four PCLs.

PCL Settings

Continue reading “Xamarin MVC Project Separation & Fluent Validation – Part I”