Skip to content
Marek Fišera edited this page Sep 2, 2016 · 4 revisions

Static class Ensure defines three types of method parameter checking, ensuring its values and other code conditions. These are defined in reusable manner. The class is placed inside root namespace. So is it simply visible in all Neptuo projects without any using (because all Neptuo projects are placed in sub-namespace of Neptuo).

Base parameter checking

Directly on Ensure there are defined static method for base parameter checking. These include NotNull, NotNullOrEmpty, positive, negative or zero and all combinations of these. These methods are used very offset and the set is nearly closed.

These methods provides validation rules and throws exception if this validation is not satified. E.g:

public void SaveText(string text)
{
    Ensure.NotNullOrEmpty(text, "text");

    ...
}

Exception extensions

Class Ensure defines helper object Exception on which are defined extension methods for throwing exceptions. All methods here should return instance of exception. Example usage:

public void CreateFile(string filePath)
{
    if(File.Exists(filePath))
        throw Ensure.Exception.ArgumentOutOfRange("filePath", "Argument '{0}' must be valid path to not-existing path", filePath);

    ...
}

These methods typically provides (when takes string message as parameter) automatic string formatting like in the example above.

Condition extensions

Like for 'exception extensions', class Ensure defines helper object for defining more advanced (in comparison to NotNull and others) parameter validation methods. For e.g., in file system, there are methods for requiring valid file and directory paths. These methods can be module/library specific, so they are defines using extension methods. Like static methods on Ensure, these also provides some validation logic and if it is not satisfied, exception is thrown. For e.g.:

public string ReadFile(string filePath)
{
    Ensure.Condition.FileExists(filePath, "filePath");

    ...
}

Extensible exception handling

There is package Neptuo.Exceptions for exception handling. The concepts are based on providing exception handlers based on exceptions and other filtering criteria. These structures and used in placed where we can't simply place try-catch blocks. Example:

// Create instance of 'dispatcher'
ExceptionHandlerBuilder builder = new ExceptionHandlerBuilder();

// Create exception handlers
ArgumentExceptionHandler handler1 = new ArgumentExceptionHandler();
InnerExceptionIsNullHandler handler2 = new InnerExceptionIsNullHandler();
MessageLongerThanTenHandler handler3 = new MessageLongerThanTenHandler();
MessageLongerThanTenHandler handler4 = new MessageLongerThanTenHandler();

// handler1 is used for all exceptions inheriting from ArgumentException.
builder
    .Handler(handler1);

// handler2 is used for all exceptions with inner exception.
// handler3 is used for all exceptions with inner exception AND Message length than 10ch.
builder
    .Filter(a => a.InnerException == null)
    .Handler(handler2)
    .Filter(a => a.Message.Length > 10)
    .Handler(handler3);

// handler4 is used for all exceptions with Message length than 10ch.
builder
    .Filter(a => a.Message.Length > 10)
    .Handler(handler4);

// Create handler collection with 'dispatcher'.
IExceptionHandlerCollection collection = new DefaultExceptionHandlerCollection()
    .Add(builder);

// Handle exceptions
collection.Handle(new ArgumentException("Very long message", new Exception()));
Clone this wiki locally