-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.cs
54 lines (41 loc) · 1.25 KB
/
Application.cs
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
using Autofac.Features.Indexed;
namespace DIContainers
{
public interface IApplication
{
public ComponentA A { get; }
public ComponentB B { get; }
public void RunStrategy(string strategyName);
}
public class DemoAppWithMEDI : IApplication
{
private readonly IEnumerable<IStrategy> _strategies;
public ComponentA A { get; init; }
public ComponentB B { get; init; }
public DemoAppWithMEDI(IEnumerable<IStrategy> strategies)
{
_strategies = strategies;
}
public void RunStrategy(string strategyName)
{
_strategies.FirstOrDefault(s => s.Name == strategyName)?.Run();
}
}
public class DemoAppWithAutofac : IApplication
{
private readonly IIndex<string, IStrategy> _strategies;
public ComponentA A { get; init; }
public ComponentB B { get; init; }
public DemoAppWithAutofac(IIndex<string, IStrategy> strategies)
{
_strategies = strategies;
}
public void RunStrategy(string strategyName)
{
if (_strategies.TryGetValue(strategyName, out IStrategy strategy))
{
strategy.Run();
}
}
}
}