Open sandboxFocus

Enum ContractDirection

Specifies the direction of the data flow to which a contract applies. Contracts can validate or transform values at different points in the data flow: when values are received (input/preconditions), when values are returned (output/postconditions), or both.

Namespace: Metalama.Framework.Aspects
Assembly: Metalama.Framework.dll
Syntax
[RunTimeOrCompileTime]
public enum ContractDirection
Remarks

Understanding contract directions is crucial for implementing effective validation:

  • Input contracts validate values coming into a method or being assigned to a property (preconditions).
  • Output contracts validate values being returned from a method or property getter (postconditions).
  • Both applies validation in both directions, useful for ref parameters.

The Default direction automatically selects the appropriate direction based on the target declaration type.

Fields

Name Description
Both

Applies to both Input and Output data flows. The contract validates:

  • Values coming in (preconditions)
  • Values going out (postconditions)
This is particularly useful for ref parameters where the value flows in both directions.
Default

Uses the default direction based on the target declaration type:

  • For input and ref parameters: equivalent to Input (validates the value passed by the caller).
  • For fields and properties: equivalent to Input (validates the value being assigned via the setter).
  • For out parameters and return values: equivalent to Output (validates the value being returned to the caller).
  • For read-only properties or indexers: equivalent to Output (validates the value returned by the getter).
Input

Validates the input data flow (precondition):

  • For parameters: validates the value before the method executes (caller must provide valid value).
  • For properties, indexers, and fields: validates the value before it is assigned (caller must assign valid value).
This is commonly used for parameter validation (e.g., null checks, range validation).
None

Disables the contract. The contract will not be applied.

Output

Validates the output data flow (postcondition):

  • For out or ref parameters: validates the value after the method executes (method must set valid value).
  • For return values: validates the value being returned (method must return valid value).
  • For property or indexer getters: validates the value being retrieved (getter must return valid value).
  • For fields: validates the value when the field is read (field must contain valid value).
This is commonly used for return value validation and ensuring methods fulfill their contracts.
See Also