Skip to content

v8.0.0

Choose a tag to compare

@github-actions github-actions released this 28 Jun 17:57
· 149 commits to main since this release
168443f

Motiv 8.0.0

Motiv 8.0.0 is a major release headlined by expression-tree propositions — write an ordinary boolean lambda and let Motiv decompose it into explainable, composable clauses — alongside new side-effect observers, a sharper evaluation API, and first-class IDE tooling that nudges you away from boolean blindness as you type.

✨ Highlights

Expression-tree propositions — Spec.From(...)

Build a proposition straight from a boolean expression. Motiv walks the expression tree and turns each sub-expression into its own clause, so a single lambda yields a full, de-noised explanation of why a result was true or false — no manual .And()/.Or() wiring required.

var canAccess = Spec
    .From((User user) =>
        user.Age >= 18 &
        user.HasValidId &
        (user.Country == "US" || user.HasInternationalPermit) &
        !user.IsRestricted)
    .Create("can access");

var result = canAccess.Evaluate(user);
result.Satisfied;   // false
result.Assertions;  // e.g. ["user.Age < 18", "user.HasValidId == false"]

What you get inside a Spec.From(...) lambda:

  • Automatic clause decomposition with auto-negated assertions for the unsatisfied branch.
  • Inline Any / All — numbers.Any(n => n % 2 == 0) and numbers.All(n => n > 0) become higher-order propositions.
  • Compose existing specs — reference a SpecBase directly inside the lambda and its assertions fold into the result.
  • The full builder chain — WhenTrue, WhenFalse, WhenTrueYield, WhenFalseYield all work after Spec.From(...), exactly as with Spec.Build(...).
  • Display.AsValue(...) — surface a sub-expression's runtime value in the assertion (e.g. user.Age = 15) instead of just the identifier.

A Expression<Func<TModel, bool>>.ToSpec() extension is also available for converting an already-materialized expression.

Side-effect observers — Tap, TapWhenTrue, TapWhenFalse

Attach callbacks (logging, metrics, auditing) to a proposition without changing how it evaluates or what it reports. Reason, Assertions, Justification, and Description pass through unchanged.

var observed = isEligible
    .TapWhenTrue((customer, result) => logger.LogInformation("eligible: {Reason}", result.Reason))
    .TapWhenFalse((customer, result) => logger.LogWarning("ineligible: {Reason}", result.Reason));

Callbacks fire on Evaluate(...). The lightweight Matches(...) path intentionally does not invoke them.

🛠️ IDE tooling (ships in the box)

The Motiv.Analyzer and Motiv.CodeFix Roslyn components are bundled inside the Motiv package — no separate install. They activate automatically when you reference Motiv.

  • MOTIV0001 analyzer — an Info-level hint flagging raw boolean expressions (comparisons, &&/||/!, is/pattern checks) that could become Motiv propositions. It stays quiet inside existing Spec.Build(...) lambdas and SpecBase-derived types, so Motiv-aware code isn't nagged.
  • Code fix: "Convert to Motiv specification" — generates a Spec.From(...)-based proposition from the flagged expression, with context-aware clause naming. Handles instance/static method calls, nameof(), pattern variables, and nested expressions, and supports Fix-All.
  • Code fix: "Convert to Motiv specification (with debug output)" — the same conversion wrapped with .Tap() + Debug.WriteLine (conditional on DEBUG, so it's stripped from Release builds).

⚠️ API changes & migration

  • IsSatisfiedBy(model) is now [Obsolete] in favor of Evaluate(model) (returns the rich BooleanResultBase). The old method still compiles as a shim but emits a warning.
    - var result = spec.IsSatisfiedBy(model);
    + var result = spec.Evaluate(model);
  • Matches(model) is the new canonical boolean-only path — use it where you previously wrote .IsSatisfiedBy(model).Satisfied. It skips result-object allocation and does not trigger Tap callbacks. SpecBase<TModel> also exposes an implicit conversion to Func<TModel, bool> backed by Matches.
    - if (spec.IsSatisfiedBy(model).Satisfied) { ... }
    + if (spec.Matches(model)) { ... }

These are the only changes 7.x consumers need to act on — and both old forms still work, so you can migrate at your own pace.

🚀 Platform & packaging

  • .NET 10 support added — the package now targets net8.0, net9.0, net10.0, and netstandard2.0.
  • Symbol packages + SourceLink — .snupkg symbols ship alongside the package and SourceLink is enabled, so you can step straight into Motiv source while debugging.
  • Package icon now displays on nuget.org.

⚡ Performance

Result and description properties (Reason, Justification, Detailed, Statement, and the underlying result collections) are now lazily computed and cached, reducing work for evaluations that don't inspect every property.

🔧 Internal

  • The source-generator attributes that produce the fluent builder overloads moved from a bundled package to the public Converj 2.0.0 dependency. This is a build-time change only — Motiv's public API is unchanged.

Full Changelog: https://github.com/karlssberg/Motiv/commits/v8.0.0