-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
66 lines (45 loc) · 2.23 KB
/
Program.cs
File metadata and controls
66 lines (45 loc) · 2.23 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
using Example;
using LazyDependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
var services = new ServiceCollection()
.AddTransient<IExampleService1, ExampleService1>()
.AddKeyedTransient<IExampleService1, ExampleService1>("test")
.AddTransient<IExampleService2, ExampleService2>()
.AddTransient(typeof(IExampleService3<>), typeof(ExampleService3<>))
.AddTransient<IExampleService3<ContractAB>, ExampleService3<ContractAB>>()
.AddTransient<IExampleDisposableService, ExampleDisposableService>()
.AddTransient<ServiceWithManyUnusedDeps>()
// DEFAULT: Adds for any service that has dependencies
// and injections into other services with multiple methods and dependencies
.AddLazyProxy()
//// OR: Default analog with additional assembly condition for services
//.AddLazyProxy(Assembly.GetExecutingAssembly())
//// OR: Adds only for specific services
//.AddLazyProxy(typeof(IExampleService1), typeof(IExampleService2), typeof(IExampleService3<>))
//// OR: Adds with your custom filter
//.AddLazyProxy(x => !x.Descriptor.IsKeyedService && x.Descriptor.HasDependenciesCountGreaterThan(0))
//// OR: Adds for any service that has dependencies and injections into specific services
//.AddLazyProxy(ServiceFilters.HasDependencies()
// .And(ServiceFilters.IsInjectedTo(typeof(ServiceWithManyUnusedDeps))))
//// OR: Adds only for specific services that have dependencies
//.AddLazyProxy(ServiceFilters.HasDependencies()
// .And(ServiceFilters.IsServiceType(typeof(IExampleService2), typeof(IExampleService3<>))))
//// OR: Adds for all
//.AddLazyProxy(x => true)
.BuildServiceProvider();
Console.WriteLine("Result: " +
services.GetRequiredService<ServiceWithManyUnusedDeps>()
.Method2("some args"));
Console.WriteLine("Result: " +
services.GetRequiredService<IExampleService1>()
.Method1(555));
using (var scope = services.CreateScope())
{
Console.WriteLine("Result: " +
scope.ServiceProvider.GetRequiredService<IExampleDisposableService>()
.Method2(777));
}
Console.WriteLine("Result: " +
services.GetRequiredService<IExampleService3<ContractAB>>()
.Method3(new()));