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.");
	}
}

Now, this is a simple validation class using Fluent Validation. You’ll notice that it expects some class to validation when extending from AbstractValidator. This presented a problem at first when creating my initial ViewModel for our MVVM pattern because I didn’t want to have to create another model and view model, so I designed the “IBaseType” which you saw earlier.
There is only one purpose of this class and it is to be implemented on a base view model that is used for the MVVM pattern. When following the MVVM pattern it has become a standard that you create a “Base” class that implements INotifyPropertyChanged, so I simply evolved this idea for Xamarin.Forms and Fluent Validation. If I make my base class take a generic and implement “IBaseType” and pass my Model from our “*.Model” project as the “T” (the generic) then I will have access to the actual class that needs validation. Now the trick to getting this to work is to have a “SetField” method. So that we can use a bit of reflection and set the property value of our base type. Lets look closer:

public interface IBaseType where T : class, new()
{
	T BaseType { get; set; } 
}

public abstract class BaseAnnotation : IBaseType, INotifyPropertyChanged where T : class, new()
{
	public T BaseType { get; set; }

	protected BaseAnnotation(T model)
	{
		BaseType = model;
	}

	public event PropertyChangedEventHandler PropertyChanged;

	[NotifyPropertyChangedInvocator]
	protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
	{
		PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
	}

	protected bool SetField(ref Titem field, Titem value, [CallerMemberName] string propertyName = null)
	{
		if (EqualityComparer.Default.Equals(field, value))
		return false;

		field = value;

		BaseType.GetType().GetRuntimeProperty(propertyName).SetValue(BaseType, value);

		OnPropertyChanged(propertyName);
		return true;
	}

	public override string ToString()
	{
	return BaseType.ToString();
	}
}

And that’s it! This might seem like a bit of overhead for a simple project and it probably is, but if you’re planning on building something large and would like to achieve as much code reuse as possible. Until next time, happy coding!

 

 

UPDATE 2-18-2016

I’ve uploaded the code to GitHub, check it out HERE!

Leave a Reply

Your email address will not be published. Required fields are marked *