Description
Current Behavior
Currently, Console.Routing provides its own dependency injection mechanism through the RouterBuilder class with methods like .AddService<TService>(). This works well for basic scenarios but doesn't integrate with existing dependency injection containers that may already be configured elsewhere in the application.
Proposed Solution
Enhance Console.Routing to accept an already configured IServiceCollection (from Microsoft.Extensions.DependencyInjection) that would be used to resolve dependencies. This would allow Console.Routing to better integrate with applications that are already using the standard .NET dependency injection container.
Use Case
In larger applications where Console.Routing is just one component, we often have a central dependency injection container configured with all services. Having to register services again specifically for Console.Routing leads to:
- Duplicate service registration
- Potential inconsistencies between services
- Inability to leverage scoped services properly
- Additional maintenance burden when services change
Implementation Suggestion
Add a new method to the RouterBuilder class:
public RouterBuilder UseServiceCollection(IServiceCollection services)
{
// Implementation that would use the provided service collection
// instead of the internal service registry
return this;
}
Or alternatively:
public RouterBuilder UseServiceProvider(IServiceProvider serviceProvider)
{
// Implementation that would use the provided service provider
// for resolving dependencies
return this;
}
This would allow code like:
// Existing DI setup
var services = new ServiceCollection();
services.AddSingleton<IMyService, MyService>();
services.AddScoped<IUserService, UserService>();
var serviceProvider = services.BuildServiceProvider();
// Console.Routing setup using existing services
var router = new RouterBuilder()
.AddAssemblyOf<Program>()
.UseServiceProvider(serviceProvider)
.Build();
Routing.Handle(args, router);
Additional Benefits
- Access to the full Microsoft.Extensions.DependencyInjection feature set
- Better integration with ASP.NET Core and other .NET applications
- Support for scoped lifetime services
- Consistency with modern .NET dependency injection practices
References
Description
Current Behavior
Currently, Console.Routing provides its own dependency injection mechanism through the RouterBuilder class with methods like
.AddService<TService>(). This works well for basic scenarios but doesn't integrate with existing dependency injection containers that may already be configured elsewhere in the application.Proposed Solution
Enhance Console.Routing to accept an already configured
IServiceCollection(from Microsoft.Extensions.DependencyInjection) that would be used to resolve dependencies. This would allow Console.Routing to better integrate with applications that are already using the standard .NET dependency injection container.Use Case
In larger applications where Console.Routing is just one component, we often have a central dependency injection container configured with all services. Having to register services again specifically for Console.Routing leads to:
Implementation Suggestion
Add a new method to the RouterBuilder class:
Or alternatively:
This would allow code like:
Additional Benefits
References