Skip to content

Latest commit

 

History

History
74 lines (57 loc) · 2.49 KB

README.md

File metadata and controls

74 lines (57 loc) · 2.49 KB

Constant

Visit the Constant website for more information.

NuGet Badge NuGet Badge

Windows Linux
AppVeyor Build status Travis CI Build Status
Build history Build history

What is it?

Constant enables developers to create smart enums in C# and is implemented using .NET portable class libraries.

Basic use

Creating constants:

private class Producttype : Constant<Producttype>
{
	[DefaultKey]
	public static readonly Producttype Default = new Producttype("default", false);
	public static readonly Producttype Box = new Producttype("box", true);

	private TestableConstant(string key, bool isValid)
	{
		this.Add(key, this);
		IsValid = isValid;
	}

	public bool ValidType { get; private set; }
}

Using constants:

public class Product
{
	private Producttype producttype;
	
	public Product(string type)
	{
		producttype = ProductType.GetOrDefaultFor(type);
	}	

	public void Validate()
	{
		if(prodtuctype.IsValid)
		{
			return true;
		}

		// Just to illustrate that it can be used as a normal enum as well
		if(producttype == Producttype.Default)
		{
			return true;
		}

		return false;
	}	
}

Motivation

The project was created because I found I had needs for this in quite a few projects and got tired of moving the code around. Enums in themselves can end up hiding businesslogic so this is the logical next step. Making it to a NuGet package made using this much easier.

Installation

The project can be installed easiliy via NuGet or by downloading and compiling the source yourself.

License

See Lisence.txt