GiusepeCasagrande / KissSpecifications

A KISS approach for specification pattern.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

KissSpecifications

A KISS approach for specification pattern. http://en.wikipedia.org/wiki/Specification_pattern

Setup

NuGet

PM> Install-Package KissSpecifications

Using

  • Create your specification classes:
public class CustomerNameSpecification : SpecificationBase<string>
{

		#region Constants
		public const int MinCustomerName = 5;
		public const int MaxCustomerName = 30;
		#endregion

		#region Methods
		public override bool IsSatisfiedBy(string target)
		{
			if (String.IsNullOrWhiteSpace(target))
			{
				NotSatisfiedReason = "Customer name should be specified.";
				return false;
			}

			if (target.Length < MinCustomerName)
			{
				NotSatisfiedReason = String.Format(CultureInfo.CurrentUICulture, "The minimum length for customer name is {0} chars.", MinCustomerName);
				return false;
			}

			if (target.Length > MaxCustomerName)
			{
				NotSatisfiedReason = String.Format(CultureInfo.CurrentUICulture, "The maximum length for customer name is {0} chars.", MaxCustomerName);
				return false;
			}

			return true;
		}
		#endregion
}
public class CustomerNameSpecification : SpecificationBase<string>
{

	#region Constants
	public const int MinCustomerName = 5;
	public const int MaxCustomerName = 30;
	#endregion

	#region Methods
	public override bool IsSatisfiedBy(string target)
	{
		if (String.IsNullOrWhiteSpace(target))
		{
			NotSatisfiedReason = "Customer name should be specified.";
			return false;
		}

		if (target.Length < MinCustomerName)
		{
			NotSatisfiedReason = String.Format(CultureInfo.CurrentUICulture, "The minimum length for customer name is {0} chars.", MinCustomerName);
			return false;
		}

		if (target.Length > MaxCustomerName)
		{
			NotSatisfiedReason = String.Format(CultureInfo.CurrentUICulture, "The maximum length for customer name is {0} chars.", MaxCustomerName);
			return false;
		}

		return true;
	}
	#endregion
}
  • Call the SpecificationService on your Domain method:
public static class CustomerService
{

	public static void CreateCustomer(Customer customer)
	{
		SpecificationService.ThrowIfAnySpecificationIsNotSatisfiedBy(customer, new CustomerCreationSpecification());

		// TODO: Logic to create customer...
	}
}

About

A KISS approach for specification pattern.


Languages

Language:JavaScript 96.2%Language:C# 3.8%Language:ASP 0.0%