Skip to content

Small library that enforce design by contract approach in declarative manner

License

Notifications You must be signed in to change notification settings

ArturLavrov/DeclarativeContracts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DeclarativeContracts

Build codecov Nuget

Lightweight library that enforce design by contract approach in clean and declarative manner

Design by contract is not a brand new, but really powerful technique that could make software reliable and safe from bugs. If you not familiar with this idea, please check this link.

Precondition

Precondition helps to verify that client's of your public API does not violate declared contract that you declared. For this purpose use class Require

For example:

public void SaveClientInfo(Client client)
{
    Require.That(client, (client) => clinet != null);

    //method body implementation code

}

The following lines is a little bit clumsy and hard to read. For solving this issue DeclarativeContracts contains a lot of out of the box functions that incapsulate required functionality

For instance, above checks could be done in the following manner:

public void CheckClientInfo(Client client)
{
    Require.That(clinet, Is.NotNull);

    //TODO: method code

}

Moreover Is and Has classes provide a lot of other useful methods such as:

Is.Null

Is.NotNull

Is.Default

Is.NotDefault

Is.NotOrEmptyString

Is.Positive

Is.Negative

Is.False

Is.True

Is.Odd

Is.Even

Postcondition

Postcondition helps to be sure that you public method always returns expected result and client could be sure that call if thees methods is always save

To perform postcondition check use class: Ensure.

For example:

public VerificationResult VerifyClient(Client client)
{
    Require.That(clinet, Is.NotNull);

    var verificationResult = _verificationService.VerifyClient(client);

    Ensure.That(verificationResult, Is.NotNull);
}

As you could see, this class could be easily combine with build your custom delegates or build in functions.

Q&A