Click here to Skip to main content
15,887,676 members
Articles / Programming Languages / C# 4.0

RiaTasks: Central Silverlight Business Rules Validation

Rate me:
Please Sign up or sign in to vote.
4.98/5 (28 votes)
13 Jul 2010Ms-PL4 min read 79.9K   781   43  
Using Fluent Validation on the website to validate business rules in a Silverlight application
using FluentValidation;
using System;

namespace RIATasks.Web
{
    // This validator will be used for all operations on the Tasks table
    public class TaskValidator : AbstractValidator<Task>
    {
        // Thses rules ensure that you always have a Name and Description for a Task
        public TaskValidator()
        {
            RuleFor(Task => Task.TaskName).NotEmpty()
                .WithMessage("Please specify Task Name");
            RuleFor(Task => Task.TaskDescription).NotEmpty()
                .WithMessage("Please specify Task Description");            
        }
    }

    // This validator will only be used for Insert operations on the Tasks table
    public class TaskInsertValidator : AbstractValidator<Task>
    {
        public TaskInsertValidator()
        {
            // When inserting the date must be null or in the future
            RuleFor(Task => Task.DueDate).Must(BeADateInTheFuture)
                .WithMessage("Please specify a date that has not already passed");
        }

        // If a non-null date is entered make sure it is in the future
        private bool BeADateInTheFuture(DateTime? dtCurrentDate)
        {
            DateTime dtDateInTheFuture = DateTime.Now.AddDays(1);
            return ((dtCurrentDate ?? dtDateInTheFuture) >= DateTime.Now.AddDays(-1));
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) http://ADefWebserver.com
United States United States
Michael Washington is a Microsoft MVP. He is a ASP.NET and
C# programmer.
He is the founder of
AiHelpWebsite.com,
LightSwitchHelpWebsite.com, and
HoloLensHelpWebsite.com.

He has a son, Zachary and resides in Los Angeles with his wife Valerie.

He is the Author of:

Comments and Discussions