All Genesis.* projects are formalizations of small pieces of functionality I find myself copying from project to project. Some are small to the point of triviality, but are time-savers nonetheless. They have a particular focus on performance with respect to mobile development, but are certainly applicable outside this domain.
Genesis.Ensure is a simple library for application authors (not library authors) to facilitate asserting runtime expectations. It is delivered as a netstandard 1.0 binary.
Code Contracts delivers similar (though much richer) functionality than Genesis.Ensure. The reasons you might consider Genesis.Ensure over Code Contracts include:
- Code Contracts requires extraneous tooling to translate contracts into executable code. This tooling is not supported on all platforms, such as Xamarin
- Genesis.Ensure has a simpler API
The easiest way to get Genesis.Ensure is via NuGet:
Install-Package Genesis.Ensure
Genesis.Ensure is super simple to use. It provides only a single, static
class called Ensure
. Using the Ensure
class, you can validate arguments and other conditions.
Here are some examples:
public void SomeMethod(string key, object value)
{
Ensure.ArgumentNotNullOrEmpty(key, nameof(key));
Ensure.ArgumentNotNull(value, nameof(value));
...
}
public void SomeMethod(DayOfWeek day)
{
Ensure.ArgumentIsValidEnum(day, nameof(day));
}
IMPORTANT: unless your consuming assembly defines the ENSURE
compiler symbol, all calls to Ensure
will be elided from the built assembly. This makes it very simple to optimize out these calls for performance-critical scenarios, such as mobile applications and embedded development.
Genesis.Ensure is created and maintained by Kent Boogaart. Issues and pull requests are welcome.