-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAspNetCoreContainer.cs
More file actions
122 lines (107 loc) · 4.68 KB
/
Copy pathAspNetCoreContainer.cs
File metadata and controls
122 lines (107 loc) · 4.68 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using Tact.Diagnostics;
using Tact.Practices.Base;
using Tact.Practices.ResolutionHandlers;
namespace Tact.Practices.Implementation
{
public sealed class AspNetCoreContainer : ContainerBase, IServiceProvider, ISupportRequiredService, IServiceScopeFactory, IServiceScope
{
private const bool IncludeUnkeyedInResolveAll = true;
public AspNetCoreContainer(
ILog log,
int? maxDisposeParallelization = null,
bool resolveLazy = true,
bool resolveFunc = true,
bool resolveUnregistered = true,
bool resolveEnumerable = true,
bool resolveCollection = true,
bool resolveList = true,
bool resolveGenerics = true)
: base(log, maxDisposeParallelization, IncludeUnkeyedInResolveAll)
{
ResolutionHandlers = CreateDefaultHandlers(
resolveLazy,
resolveFunc,
resolveUnregistered,
resolveEnumerable,
resolveCollection,
resolveList,
resolveGenerics);
}
public AspNetCoreContainer(
ILog log,
IReadOnlyList<IResolutionHandler> resolutionHandlers,
int? maxDisposeParallelization = null)
: base(log, maxDisposeParallelization, IncludeUnkeyedInResolveAll)
{
ResolutionHandlers = resolutionHandlers ?? throw new ArgumentNullException(nameof(resolutionHandlers));
}
private AspNetCoreContainer(
ILog log,
IReadOnlyList<IResolutionHandler> resolutionHandlers,
int? maxDisposeParallelization,
AspNetCoreContainer parentScope)
: base(log, maxDisposeParallelization, IncludeUnkeyedInResolveAll, parentScope)
{
ResolutionHandlers = resolutionHandlers;
}
protected override IReadOnlyList<IResolutionHandler> ResolutionHandlers { get; }
IServiceProvider IServiceScope.ServiceProvider => this;
public override IContainer BeginScope()
{
return CreateScope();
}
object IServiceProvider.GetService(Type serviceType)
{
TryResolve(out object service, serviceType);
return service;
}
object ISupportRequiredService.GetRequiredService(Type serviceType)
{
return Resolve(serviceType);
}
IServiceScope IServiceScopeFactory.CreateScope()
{
return CreateScope();
}
public void RegisterCollection(IServiceCollection services)
{
this.RegisterProxy<IServiceProvider>(r => (IServiceProvider)r);
this.RegisterProxy<ISupportRequiredService>(r => (ISupportRequiredService)r);
this.RegisterProxy<IServiceScopeFactory>(r => (IServiceScopeFactory)r);
this.RegisterProxy<IServiceScope>(r => (IServiceScope)r);
foreach (var service in services)
{
switch (service.Lifetime)
{
case ServiceLifetime.Singleton:
if (service.ImplementationInstance != null)
this.RegisterInstance(service.ServiceType, service.ImplementationInstance);
else if (service.ImplementationFactory != null)
this.RegisterSingleton(service.ServiceType, factory: r => service.ImplementationFactory(this));
else
this.RegisterSingleton(service.ServiceType, service.ImplementationType);
break;
case ServiceLifetime.Scoped:
if (service.ImplementationFactory != null)
this.RegisterPerScope(service.ServiceType, factory: r => service.ImplementationFactory(this));
else
this.RegisterPerScope(service.ServiceType, service.ImplementationType);
break;
case ServiceLifetime.Transient:
if (service.ImplementationFactory != null)
this.RegisterTransient(service.ServiceType, factory: r => service.ImplementationFactory(this));
else
this.RegisterTransient(service.ServiceType, service.ImplementationType);
break;
}
}
}
private AspNetCoreContainer CreateScope()
{
return new AspNetCoreContainer(Log, ResolutionHandlers, MaxDisposeParallelization, this);
}
}
}