Viklund Random thoughts


Configuration Is a Port

If your use cases take IOptions<T>, this is an argument for handing them a value object through a port instead. If you don’t use ports, skip to two kinds of valid for two TimeSpan validation traps.

In most .NET code, configuration is bound to an options class and injected as IOptions<T> wherever it’s needed. In a ports and adapters application, that includes the use cases. But a settings file is part of the outside world, as much as a database or another service is, and the application is supposed to reach the outside world only through ports it declares itself.

So treat configuration the same way. When a use case needs a setting, it asks a driven port for the value, as a value object in its own terms. An adapter in Infrastructure decides where the value comes from.

A use case needs a value, not a configuration file. Where the value comes from is the adapter’s problem.

An example

Say a customer can hold a seat for a while before paying, and how long is a setting. The domain gets a value object, so the rule about what a hold duration can be lives in one place:

public sealed record HoldDuration
{
    private readonly TimeSpan _duration;

    private HoldDuration(TimeSpan duration) => _duration = duration;

    public static HoldDuration From(TimeSpan duration)
    {
        ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(duration, TimeSpan.Zero);
        return new HoldDuration(duration);
    }

    public DateTimeOffset ExpiresAt(DateTimeOffset heldAt) => heldAt + _duration;
}

The application declares what it needs, and nothing about settings:

public interface IForProvidingHoldDuration
{
    HoldDuration Provide();
}

Infrastructure owns everything else: the options class, its section name, its validation and the adapter that turns it into the value object:

public sealed class ReservationOptions
{
    public const string SectionName = "Reservations";

    [Range(typeof(TimeSpan), "00:05:00", "01:00:00")]
    public TimeSpan HoldDuration { get; init; }
}

internal sealed class HoldDurationAdapter(IOptions<ReservationOptions> options) : IForProvidingHoldDuration
{
    public HoldDuration Provide() => HoldDuration.From(options.Value.HoldDuration);
}

Why not just IOptions?

IOptions<T> is already an abstraction, so why add another one? Because it hides how settings are loaded, not what they are. A use case that takes IOptions<ReservationOptions> still depends on the shape of a settings section, and on a framework to bind it. What it needs is a hold duration.

With the port, the use case depends on what the setting means. The source can change, from a settings file to a database table that an administrator edits, without the use case noticing.

Two kinds of valid

The split also separates two kinds of validation that tend to end up on the same property. The value object demands a positive duration, because a hold of zero or less means nothing. That’s a rule of the domain, and it holds wherever the value comes from. The settings demand between five minutes and an hour, because that’s what’s sensible to run with. That’s an operating limit, and it belongs with the settings.

Operating limits are also where the format of a setting matters, and it’s easy to get wrong. TimeSpan parses "24:00:00" as 24 days, not 24 hours. So [Range(typeof(TimeSpan), "00:01:00", "24:00:00")], meant as “up to a day”, accepts anything up to 24 days. One day is written "1.00:00:00".

A missing value has a trap of its own. [Required] never fails on a TimeSpan, because it isn’t nullable: a missing setting binds to zero, not null, and zero counts as a value. In the example, the range catches it, because zero is below five minutes. Without the range, zero would only be rejected by the value object, on the first reservation instead of at startup.

Validate the settings when the app starts, and don’t fall back to defaults. A missing value should stop the app, not quietly become a guess:

services.AddOptions<ReservationOptions>()
    .BindConfiguration(ReservationOptions.SectionName)
    .ValidateDataAnnotations()
    .ValidateOnStart();

Tests don’t need configuration

A use case test doesn’t build options or read settings. It returns the value object straight from the port:

_holdDuration.Provide().Returns(HoldDuration.From(TimeSpan.FromMinutes(15)));

The test says exactly what the use case sees, and nothing about where it came from. The options class gets tests of its own in Infrastructure, and that’s where a bound like "24:00:00" gets caught. A use case test never would, because it never reads a settings string.

Not every setting needs a port

Most settings never reach a use case. A connection string, an SMTP host or a retry count for an HTTP client is configuration for an adapter, and it stays with the adapter that uses it. The port is for settings that drive a business decision, the ones a use case or the domain would otherwise have to read.

What it costs

Each such setting costs a port and an adapter, a few lines each. Related settings can share one value object to keep the number down.

And a port doesn’t make runtime changes free. An adapter that reads from a database still has to decide how fresh its values need to be. The port only makes sure that decision is made in one place, not in every use case.