Compze.Contracts
0.6.0
dotnet add package Compze.Contracts --version 0.6.0
NuGet\Install-Package Compze.Contracts -Version 0.6.0
<PackageReference Include="Compze.Contracts" Version="0.6.0" />
<PackageVersion Include="Compze.Contracts" Version="0.6.0" />
<PackageReference Include="Compze.Contracts" />
paket add Compze.Contracts --version 0.6.0
#r "nuget: Compze.Contracts, 0.6.0"
#:package Compze.Contracts@0.6.0
#addin nuget:?package=Compze.Contracts&version=0.6.0
#tool nuget:?package=Compze.Contracts&version=0.6.0
Compze.Contracts
Fluent, chainable runtime assertions for preconditions, invariants, and state checks — with CallerArgumentExpression support for clear failure messages.
Assertion types each supporting every assert method
| Entry point | Throws on failure | Use for |
|---|---|---|
Contract.Argument |
ArgumentAssertionFailedException / ArgumentNullException |
Method parameter validation |
Contract.State |
StateAssertionFailedException |
Enforcing state requirements for the requested operation |
Contract.Invariant |
InvariantAssertionFailedException |
Class invariant enforcement |
All entry points return a ContractAsserter that supports fluent chaining.
Assertion methods
| Method | Description |
|---|---|
.Assert(condition, ...) |
Boolean checks |
.Assert(condition, messageFactory) |
Boolean check with lazy message creation on failure |
.NotNull(value) |
Null check (also NotNull2, NotNull3, NotNull4 for multiple values) |
.NotDefault(value) |
Default-value check for structs (also 2/3/4-value overloads) |
.NotNullOrEmpty(string) |
Rejects null or empty strings |
.NotNullEmptyOrWhitespace(string) |
Rejects null, empty, or whitespace-only strings |
.NotDisposed(isDisposed, instance) |
Throws ObjectDisposedException |
All the assertion methods that do not take an explicit separate argument for the message use CallerArgumentExpression to ensure that you can know exactly what assertion failed
Pipeline assertions
For inline assertions, avoiding the need for duplicate lines and breaking out of the fluent style:
var name = GetName()._assert(it => it.Length > 0); // predicate with auto-generated message
var id = GetId()._assert(it => it != Guid.Empty, it => $"Invalid id: {it}"); // predicate with custom message factory
var conn = GetConnection()._assert().NotNull();
Pipeline overloads:
value._assert()— returnsPipeAssertTarget<T>for.NotNull()/.NotDefault()chainsvalue._assert(predicate)— throwsAssertionFailedException("value._assert(predicate)")on failurevalue._assert(predicate, messageFactory)— throwsAssertionFailedExceptionwith custom messagevalue._assert(predicate, exceptionFactory)— throws custom exception
All the assertion methods that do not take an explicit separate argument for the message use CallerArgumentExpression to ensure that you can know exactly what assertion failed
Quick start
Fluent with static use. No wasted lines. The contract reads like part of the method declaration.
void Transfer(Account from, Account to, decimal amount) =>
Contract.Argument.NotNull2(from, to).Assert(amount > 0).State.NotDisposed(_disposed, this)._then(() => {
//method implementation here
});
Note, the _then method is from Compze.Fluent. You may want to check it out.
Classic style. Pretty verbose in our opinion
void Transfer(Account from, Account to, decimal amount)
{
Contract.Argument.NotNull2(from, to);
Contract.Argument.Assert(amount > 0);
Contract.State.NotDisposed(_disposed, this);
//method implementation here
}
Mixing it up with Compze.Fluent
public OperationResult SomeBusinessMethod(Guid userId) =>
userId
._assert().NotDefault()
._(LoadFromDatabase)
._tap(it => { /*log*/ })
._assert(MayExecuteThisOperation)
._(ActualOperationLogic)
._tap(it => { /*log*/ })
._assert(ResultIsWhatWeExpected);
Custom assertion extensions
Both ContractAsserter and PipeAssertTarget<T> are designed to be extended. All built-in assertions are themselves extension methods.
ContractAsserter extension
public static class MyContractExtensions
{
extension(ContractAsserter @this)
{
public ContractAsserter IsValidEmail(string? value,
[CallerArgumentExpression(nameof(value))] string expression = "")
{
if(!value.IsValidEmail()) @this.ThrowFailed(expression);
return @this;
}
}
}
//throws: ArgumentAssertionFailedException("Argument.IsValidEmail(userEmail)") if invalid.
Contract.Argument.IsValidEmail(userEmail);
PipeAssertTarget extension (pipeline)
public static class MyPipelineExtensions
{
public static string IsValidEmail(this PipeAssertTarget<string?> target)
{
if(!target.Value.IsValidEmail()) target.ThrowAssertionFailed();
return target.Value!;
}
}
// throws: AssertionFailedException("GetUserEmail()._assert().IsValidEmail()") if invalid.
var email = GetUserEmail()._assert().IsValidEmail();
License
Apache-2.0
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (21)
Showing the top 5 NuGet packages that depend on Compze.Contracts:
| Package | Downloads |
|---|---|
|
Compze.Utilities.SystemCE
Package Description |
|
|
Compze.Utilities.Testing.Must
Fluent assertion library for Compze testing. |
|
|
Compze.Utilities.SystemCE.ThreadingCE
Package Description |
|
|
Compze.Utilities.Testing.XUnit
xUnit testing utilities and attributes for the Compze framework. |
|
|
Compze.Utilities.DependencyInjection
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.6.0 | 157 | 2/28/2026 |