C#C
C#3y ago
21 replies
Kiel

✅ Options pattern - why have a separate XOptions type?

// What's the benefit of having...
services.Configure<GitHubLfsAuthenticatorOptions>(o => o.BaseAddress = "");

// instead of just...?
services.Configure<GitHubLfsAuthenticator>(o => o.BaseAddress = "");


Assuming my "options type" has no intent of being used outside of configuring GitHubLfsAuthenticator.

What I'm currently doing, completely without the Options pattern, is this via an extension method:
public static IServiceCollection AddGitHubAuthenticator(this IServiceCollection services,
    string organization, string repository, string baseAddress = "https://api.github.com/")
{
    services.AddSingleton(new GitHubLfsAuthenticatorOptions
    {
        BaseAddress = new Uri(baseAddress),
        Organization = organization,
        Repository = repository
    });
        
    services.AddSingleton<GitHubLfsAuthenticator>();
    services.AddSingleton<ILfsAuthenticator>(static x => x.GetRequiredService<GitHubLfsAuthenticator>());
    return services;
}

However, I feel like a user who wanted to do something other than this might be confused, so I thought maybe the Options pattern was the solution, but it doesn't really seem to provide any benefit over what I have now.
Was this page helpful?