liveBook · Manning

Fluent interface

As explained in ASP.NET Core in Action, 3e

A fluent API is a chainable programming interface that configures entity mappings through method calls rather than declarative attributes.

A fluent interface builds configuration through chained method calls. Instead of placing rules in attributes or separate declarative metadata, you express related operations as a readable sequence. section 32.4.1

In .NET applications, fluent interfaces configure several kinds of behavior: EF Core entity mappings, options through an OptionsBuilder<T>, and validation rules in FluentValidation. Chaining keeps a rule close to the property or setting it modifies. section 12.6 section 31.4

Chaining validation rules

A validator can require a currency value, constrain its length, check an allowed-value list, and provide an error message in one chain: RuleFor(x => x.CurrencyTo) .NotEmpty() .Length(3) .MustBeCurrencyCode() .WithMessage("Not a valid currency code"); The chain can also use Must() for cross-property rules, such as rejecting a conversion whose source and destination currencies are identical. Reusable rules can be extracted into extension methods, while validators can receive dependencies through constructor injection. section 32.4.1

Listing 32.11 A FluentValidation validator for the currency converter binding model
public class CurrencyConverterModelValidator                       #1
    : AbstractValidator<CurrencyConverterModel>                    #1
{
    private readonly string[] _allowedValues                       #2
        = new []{ "GBP", "USD", "CAD", "EUR" };                    #2
 
    public CurrencyConverterModelValidator()                       #3
    {
        RuleFor(x => x.CurrencyFrom)                               #4
            .NotEmpty()                                            #5
            .Length(3)                                             #5
            .Must(value => _allowedValues.Contains(value))         #6
            .WithMessage("Not a valid currency code");             #6
 
        RuleFor(x => x.CurrencyTo)
            .NotEmpty()
            .Length(3)
            .Must(value => _allowedValues.Contains(value))
            .WithMessage("Not a valid currency code");
 
        RuleFor(x => x.Quantity)
            .NotNull()
            .InclusiveBetween(1, 1000);                            #7
    }
}

Shows a complete FluentValidation chain with property selection, built-in rules, a custom predicate, and an error message. section 32.4.1

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines chaining, demonstrates validation and reusable extensions, and stops short of explaining fluent-interface implementation internals.
Examples
Uses currency conversion validators, allowed currency codes, cross-property checks, EF Core mappings, and OptionsBuilder<T> configuration.
What it assumes
Assumes basic C# method calls and properties while demonstrating validation rules, extension methods, and dependency injection in context.
What you can do after
Apply it via FluentValidation to compose property rules, custom predicates, reusable extensions, and dependency-backed checks.
How it connects
Connects method chaining with EF Core, DataAnnotations, FluentValidation, dependency injection, extension methods, and options configuration.

Also explained in

Compare how 4 books explain fluent interface →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.