C
C#2y ago
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 = "");
// 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;
}
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.
10 Replies
JakenVeina
JakenVeina2y ago
the Microsoft.Extensions.Options libarary offers more functionality than just pull options from DI most notably, it offers a system of change notification although, I will readily admit that the last time I looked into it, it was confusing I don't fully remember why something to do with mutability/immutability? if you haven't definitely give this a read
JakenVeina
JakenVeina2y ago
Options pattern - .NET
Learn the options pattern to represent groups of related settings in .NET apps. The options pattern uses classes to provide strongly-typed access to settings.
JakenVeina
JakenVeina2y ago
it offers some other points regarding this same question oooh, "named options" is another big one
Kiel
KielOP2y ago
This sounds extremely overengineered for what I'm aiming for here...but then again, who am I to tell users how they can and cant configure the service, I guess
JakenVeina
JakenVeina2y ago
definitely don't use it if you don't think it's useful re-reading, I realize now you weren't asking about this library, specifically so.... yeah but the article's still a good read, the problems it talks about that are solved by the library are the same kinda problems you might care about
Kiel
KielOP2y ago
I don't like "not using things" because I don't understand them, but I also don't like imposing confusing or overkill things on end-users 😅 I'll give it a read for sure
JakenVeina
JakenVeina2y ago
it also might be a good idea to use that library, if your intention is that users will be using this within ASP.NET it'll definitely make for less setup
Kiel
KielOP2y ago
There will be a hosting option for it with ASP.NET, but also for those without, at least in the future
JakenVeina
JakenVeina2y ago
alternatively, you could keep it minimal in your library, and publish a second package that provides extensions for using your lib, under the Microsoft umbrella
Kiel
KielOP2y ago
I'm starting with asp.net and branching out into others as people say "hey I want to host this on [x]"
Want results from more Discord servers?
Add your server