Metalama.Patterns.Contracts
v2025.0.1-previewPre-release
.NETFramework4.7.1net6.0.NETStandard2.0
Contract-Based Programming (or Design-by-Contract) with custom attributes such as [NotNull] or [Url] thanks to Metalama: pre-conditions, post-conditions and invariants.
Published
Oct 14, 2024
2y ago
Total Downloads
145.6K
145,635
Install
dotnet add package Metalama.Patterns.Contracts --version 2025.0.1-preview
| Product | Versions |
|---|---|
| .NET | |
| .NET Standard | |
| Other |
README
About
The Metalama.Patterns.Contracts package implements the concept of contract-based programming, encompassing preconditions, postconditions, and invariants.
Key Features
- Implementation of preconditions and postconditions through a wide range of numeric or string-based contracts.
- Verification of invariants.
Main Types
Below is a list of available contract attributes for your selection:
Nullability Contracts
[NotNull]verifies that the assigned value is notnull.[Required]contract verifies that the value is notnulland requires the string to be non-empty.
String Contracts
[NotEmpty]requires the string to be non-empty. Note that this contract does not validate the string against being null. If you want to prohibit both null and empty strings, use[Required].[CreditCard]validates that the string is a valid credit card number.[Email],[Phone], and[Url]validate strings against well-known regular expressions.[Regex]validates a string against a custom regular expression.[StringLength]validates that the length of a string falls within a specified range.
Enum Contracts
[EnumDataType]contract can validate values of typestring,object, or of any integer type. It throws an exception if the value is not valid for the givenenumtype.
Numeric Contracts
The following contracts can be used to verify that a value falls within a specified range:
[LessThan]verifies that the value is less than or equal to the specified maximum.[GreaterThan]verifies that the value is greater than or equal to the specified minimum.[Negative]verifies that the value is less than or equal to zero.[Positive]verifies that the value is greater than or equal to zero.[Range]verifies that the value is greater than or equal to a specified minimum and less than or equal to a specified maximum.[StrictlyLessThan]verifies that the value is strictly less than the specified maximum.[StrictlyGreaterThan]verifies that the value is strictly greater than the specified minimum.[StrictlyNegative]verifies that the value is strictly less than zero.[StrictlyPositive]verifies that the value is strictly greater than zero.[StrictRange]verifies that the value is strictly greater than a specified minimum and strictly less than a specified maximum.
Collections Contracts
[NotEmpty]contract can be used on any collection, including arrays or immutable arrays. It requires the collection or the array to contain at least one element.
Invariants
[Invariant]causes the target method (a parameterlessvoidmethod) to be invoked after each public method or property setter.[DoNotCheckInvariants]exempts the target method from enforcing invariants, but does not exempt any method or property setter used by this method.[SuspendInvariant]exempts the target method from enforcing invariants, including any method or property setter used by this method.
Additional Documentation
- Conceptual documentation:
- API documentation
Changes from Previous Version
No structural changes between v2024.2.32 and v2025.0.1-preview.
Adoption Assessment
A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.
This version is marked prerelease. Validate its API and upgrade path before production use.No SPDX license expression is published. Review the license text and your organisation's policy before redistribution.Compatibility metadata lists 3 target frameworks; confirm the exact target used by your application.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.