-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathProgram.cs
More file actions
65 lines (55 loc) · 1.62 KB
/
Copy pathProgram.cs
File metadata and controls
65 lines (55 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;
namespace CustomServices
{
#region Program
[Command(Name = "di", Description = "Dependency Injection sample project")]
[HelpOption]
class Program
{
public static int Main(string[] args)
{
var services = new ServiceCollection()
.AddSingleton<IMyService, MyServiceImplementation>()
.AddSingleton<IConsole>(PhysicalConsole.Singleton)
.BuildServiceProvider();
var app = new CommandLineApplication<Program>();
app.Conventions
.UseDefaultConventions()
.UseConstructorInjection(services);
return app.Execute(args);
}
private readonly IMyService _myService;
public Program(IMyService myService)
{
_myService = myService;
}
private void OnExecute()
{
_myService.Invoke();
}
}
#endregion
#region IMyService
interface IMyService
{
void Invoke();
}
#endregion
#region MyServiceImplementation
class MyServiceImplementation : IMyService
{
private readonly IConsole _console;
public MyServiceImplementation(IConsole console)
{
_console = console;
}
public void Invoke()
{
_console.WriteLine("Hello dependency injection!");
}
}
#endregion
}