Skip to content

Commit b38cb1a

Browse files
authored
Merge pull request #764 from jonsequitur/improve-AddService
Improve BindingContext.AddService
2 parents 703b2cd + c7c6251 commit b38cb1a

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

src/System.CommandLine.Hosting/HostingExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static CommandLineBuilder UseHost(this CommandLineBuilder builder,
4141

4242
using (var host = hostBuilder.Build())
4343
{
44-
invocation.BindingContext.AddService(typeof(IHost), () => host);
44+
invocation.BindingContext.AddService(typeof(IHost), _ => host);
4545

4646
await host.StartAsync();
4747

src/System.CommandLine/Binding/BindingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public IConsole Console
4646

4747
internal ServiceProvider ServiceProvider { get; }
4848

49-
public void AddService(Type serviceType, Func<object> factory)
49+
public void AddService(Type serviceType, Func<IServiceProvider, object> factory)
5050
{
5151
if (serviceType == null)
5252
{

src/System.CommandLine/Invocation/InvocationContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public InvocationContext(
1919
IConsole console = null)
2020
{
2121
BindingContext = new BindingContext(parseResult, console);
22-
BindingContext.ServiceProvider.AddService(GetCancellationToken);
23-
BindingContext.ServiceProvider.AddService(() => this);
22+
BindingContext.ServiceProvider.AddService(_ => GetCancellationToken());
23+
BindingContext.ServiceProvider.AddService(_ => this);
2424
}
2525

2626
public IConsole Console => BindingContext.Console;

src/System.CommandLine/Invocation/ServiceProvider.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,33 @@ internal class ServiceProvider : IServiceProvider
1313
{
1414
private readonly BindingContext _bindingContext;
1515

16-
private readonly Dictionary<Type, Func<object>> _services;
16+
private readonly Dictionary<Type, Func<IServiceProvider, object>> _services;
1717

1818
public ServiceProvider(BindingContext bindingContext)
1919
{
2020
_bindingContext = bindingContext ?? throw new ArgumentNullException(nameof(bindingContext));
2121

22-
_services = new Dictionary<Type, Func<object>>
22+
_services = new Dictionary<Type, Func<IServiceProvider, object>>
2323
{
24-
[typeof(ParseResult)] = () => _bindingContext.ParseResult,
25-
[typeof(IConsole)] = () => _bindingContext.Console,
26-
[typeof(CancellationToken)] = () => CancellationToken.None,
27-
[typeof(IHelpBuilder)] = () => _bindingContext.ParseResult.Parser.Configuration.HelpBuilderFactory(_bindingContext),
28-
[typeof(BindingContext)] = () => _bindingContext
24+
[typeof(ParseResult)] = _ => _bindingContext.ParseResult,
25+
[typeof(IConsole)] = _ => _bindingContext.Console,
26+
[typeof(CancellationToken)] = _ => CancellationToken.None,
27+
[typeof(IHelpBuilder)] = _ => _bindingContext.ParseResult.Parser.Configuration.HelpBuilderFactory(_bindingContext),
28+
[typeof(BindingContext)] = _ => _bindingContext
2929
};
3030
}
3131

32-
public void AddService<T>(Func<T> factory) => _services[typeof(T)] = () => factory();
32+
public void AddService<T>(Func<IServiceProvider, T> factory) => _services[typeof(T)] = p => factory(p);
3333

34-
public void AddService(Type serviceType, Func<object> factory) => _services[serviceType] = factory;
34+
public void AddService(Type serviceType, Func<IServiceProvider, object> factory) => _services[serviceType] = factory;
3535

3636
public IReadOnlyCollection<Type> AvailableServiceTypes => _services.Keys;
3737

3838
public object GetService(Type serviceType)
3939
{
4040
if (_services.TryGetValue(serviceType, out var factory))
4141
{
42-
return factory();
42+
return factory(this);
4343
}
4444

4545
return null;

0 commit comments

Comments
 (0)