Compze.Contracts
0.7.0
dotnet add package Compze.Contracts --version 0.7.0
NuGet\Install-Package Compze.Contracts -Version 0.7.0
<PackageReference Include="Compze.Contracts" Version="0.7.0" />
<PackageVersion Include="Compze.Contracts" Version="0.7.0" />
<PackageReference Include="Compze.Contracts" />
paket add Compze.Contracts --version 0.7.0
#r "nuget: Compze.Contracts, 0.7.0"
#:package Compze.Contracts@0.7.0
#addin nuget:?package=Compze.Contracts&version=0.7.0
#tool nuget:?package=Compze.Contracts&version=0.7.0
Compze.Contracts
Fluent, chainable runtime assertions for preconditions, invariants, and state checks — with CallerArgumentExpression support for clear failure messages.
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) =>
Argument.NotNull2(from, to).Assert(amount > 0).State.NotDisposed(_disposed, this).__(() => {
//method implementation here
});
Note, the __ method is from Compze.Underscore. 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
}
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
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);
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
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 (30)
Showing the top 5 NuGet packages that depend on Compze.Contracts:
| Package | Downloads |
|---|---|
|
Compze.Core
Core abstractions for the Compze framework including entity IDs, messaging contracts, and event sourcing interfaces. |
|
|
Compze.Tessaging
Messaging infrastructure for the Compze framework including command, query, and event handling. |
|
|
Compze.Tessaging.Hosting.AspNetCore
ASP.NET Core hosting integration for Compze messaging. |
|
|
Compze.Tessaging.Teventive.TeventStore
Event store implementation for Compze event sourcing. |
|
|
Compze.Tessaging.Hosting.Testing
Testing support for Compze messaging hosting, including test host and database pool integration. |
GitHub repositories
This package is not used by any popular GitHub repositories.