Skip to content

Explore options immutability #43359

@davidfowl

Description

@davidfowl

Today the IOptions<TOptions> pattern is built around the mutability of the TOptions until first use (though nothing prevents later mutation). We should investigate what it would mean to consume immutable TOptions in the options system. Today, the system constructs TOptions using Activator.CreateInstance then executes a series of delegates on top of that instead to produce the "final" instance (see

)

An alternative model would be to do the same thing but execute delegates that return a new instance of the options object instead of mutating the current instance. We would need to figure out how to support both side by side but it would allow designing immutable options objects.

Here's an example of what we what it would look like:

public record MyImmutableOption(string Name, int Size);

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<MyImmutableOption>(o =>
    {
        return o with { Name = "Foo" };
    });
}

cc @ericstj @eerhardt @maryamariyan

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions