Skip to content

GiusepeCasagrande/KissSpecifications

 
 

Repository files navigation

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.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 96.2%
  • C# 3.8%