Skip to content

Guard Modernization (Retire Contract class) #129

Open

Description

@JeremyCaney

The library has its own Contract.Requires(...) and Contract.Assume(...) helpers, left over as part of the migration away from the legacy .NET Frameworks Code Contracts (RIP). The .NET base class library (BCL) now provides equivalent (and more idiomatic) ThrowIfNull and ThrowIf guards directly on exception types, and C# 14 closes the one remaining gap. This allows us to retire the custom helper in favor of the modern, standard pattern. While this is mostly invisible to consumers, a handful of validation errors will become more precisely typed as part of this.

Tasks

  • Add InvalidOperationException ThrowIf() and ThrowIfNull() extension guards (the BCL doesn't cover this)
  • Add matching ThrowIf*() guards to each custom exception type (InvalidKeyException, TopicNotFoundException, &c.)
  • Migrate key-validation calls from TopicFactory.ValidateKey() to the new InvalidKeyException guards
  • Migrate the 150+ null-check call sites from Contract.Requires<>() to BCL ?? throw guards
  • Migrate the remaining generic-exception call sites (ArgumentOutOfRangeException, ArgumentException, &c.)
  • Evaluate and convert the 20+ boolean Contract.Requires() sites to ThrowIf(...) (these require care)
    • The condition inverts, as the old helper throws on false; ThrowIf() throws on true
  • Delete Contract.cs, its test file, and the Microsoft.VisualStudio.Validation package dependency
  • Create unit tests to validate the above logic

Activity

  1. self-assigned this
    on Jul 25, 2026
  2. added theissue type on Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions