Functional validation using lambda expression : Happy Coding

So you can use look Linq for validations as well. In our example i am using an Email object and i am doing two types of basic validations on that so here is the code

public class EmailAddress
{
	public string Email;
}

So the conventional validations would be

public class NonFuncationalValidation
	{
		public bool Validate(EmailAddress emailAddress)
		{
			if (!emailAddress.Email.Contains("@"))
				return false;
			if (!emailAddress.Email.Contains(".com"))
				return false;
			return true;
		}
	}

Lets transform it to functional validation. If you see here i have converted the list of ifs to set of rules. By the end i am using the method All which will start varifying the rules and the moment it encountered with a rule that is voilated it will stop and exit.

public class FuncationalValidation
{
	public bool Validate(EmailAddress emailAddress)
	{
		Func<EmailAddress, bool>[] rules = 
		{
			em => !emailAddress.Email.Contains("@"),
			em => !emailAddress.Email.Contains(".com")
		};
  		return rules.All(x => x(emailAddress) == false);
	}
}