Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Commands Explicitly to ServiceCollection #391

Open
Kahbazi opened this issue Apr 19, 2020 · 0 comments
Open

Add Commands Explicitly to ServiceCollection #391

Kahbazi opened this issue Apr 19, 2020 · 0 comments

Comments

@Kahbazi
Copy link

Kahbazi commented Apr 19, 2020

Hello @csharpfritz .
Right now all IBasicCommand and IExtendedCommand are got via scanning Assembly and added to IServiceCollection with singleton lifetime.

public static void RegisterCommands(IServiceCollection services)
{
// Register basic commands
foreach (var type in typeof(FritzBot).Assembly.GetTypes()
.Where(t => typeof(IBasicCommand)
.IsAssignableFrom(t) && !t.IsAbstract && t.IsClass))
{
services.AddSingleton(typeof(IBasicCommand), type);
}
// Register extended commands
foreach (var type in typeof(FritzBot).Assembly.GetTypes()
.Where(t => typeof(IExtendedCommand)
.IsAssignableFrom(t) && !t.IsAbstract && t.IsClass))
{
services.AddSingleton(typeof(IExtendedCommand), type);
}
}

I'm suggesting changing this behavior to adding commands explicitly in startup for two reasons :

  1. In order to inject a scoped service like DbContext, the command needs to be scoped or transient.
  2. We could prevent adding some command in startup based on configuration and prevent running a command (even CanExecute).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant