/
ContainerNetCoreExtensions.cs
154 lines (131 loc) · 5.79 KB
/
ContainerNetCoreExtensions.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using Funq;
namespace Funq
{
public partial class Container : IServiceProvider
{
public object GetService(Type serviceType)
{
return this.TryResolve(serviceType);
}
}
}
namespace ServiceStack
{
/// <summary>
/// Wrappers for improving consistency with .NET Core Conventions
/// </summary>
public static class ContainerNetCoreExtensions
{
// Request Scoped dependencies
public static Container AddScoped(this Container services, Type serviceType)
{
services.RegisterAutoWiredType(serviceType, ReuseScope.Request);
return services;
}
public static Container AddScoped(this Container services, Type serviceType, Type implementationType)
{
services.RegisterAutoWiredType(implementationType, serviceType, ReuseScope.Request);
return services;
}
public static Container AddScoped<TService>(this Container services) where TService : class
{
services.RegisterAutoWired<TService>().ReusedWithin(ReuseScope.Request);
return services;
}
public static Container AddScoped<TService>(this Container services, Func<Container, TService> implementationFactory) where TService : class
{
services.Register(implementationFactory).ReusedWithin(ReuseScope.Request);
return services;
}
public static Container AddScoped<TService, TImplementation>(this Container services)
where TService : class
where TImplementation : class, TService
{
services.RegisterAutoWiredAs<TImplementation, TService>().ReusedWithin(ReuseScope.Request);
return services;
}
public static Container AddScoped<TService, TImplementation>(this Container services, Func<Container, TImplementation> implementationFactory)
where TService : class
where TImplementation : class, TService
{
services.Register<TService>(implementationFactory).ReusedWithin(ReuseScope.Request);
return services;
}
// Singleton
public static Container AddSingleton(this Container services, Type serviceType)
{
services.RegisterAutoWiredType(serviceType, ReuseScope.Container);
return services;
}
public static Container AddSingleton(this Container services, Type serviceType, Type implementationType)
{
services.RegisterAutoWiredType(implementationType, serviceType, ReuseScope.Container);
return services;
}
public static Container AddSingleton<TService>(this Container services) where TService : class
{
services.RegisterAutoWired<TService>().ReusedWithin(ReuseScope.Container);
return services;
}
public static Container AddSingleton<TService>(this Container services, Func<Container, TService> implementationFactory) where TService : class
{
services.Register(implementationFactory).ReusedWithin(ReuseScope.Container);
return services;
}
public static Container AddSingleton<TService, TImplementation>(this Container services)
where TService : class
where TImplementation : class, TService
{
services.RegisterAutoWiredAs<TImplementation, TService>().ReusedWithin(ReuseScope.Container);
return services;
}
public static Container AddSingleton<TService>(this Container services, TService implementationInstance) where TService : class
{
services.Register(implementationInstance);
return services;
}
public static Container AddSingleton<TService, TImplementation>(this Container services, Func<Container, TImplementation> implementationFactory)
where TService : class
where TImplementation : class, TService
{
services.Register<TService>(implementationFactory).ReusedWithin(ReuseScope.Container);
return services;
}
// Transient / No Reuse
public static Container AddTransient(this Container services, Type serviceType)
{
services.RegisterAutoWiredType(serviceType, ReuseScope.None);
return services;
}
public static Container AddTransient(this Container services, Type serviceType, Type implementationType)
{
services.RegisterAutoWiredType(implementationType, serviceType, ReuseScope.None);
return services;
}
public static Container AddTransient<TService>(this Container services) where TService : class
{
services.RegisterAutoWired<TService>().ReusedWithin(ReuseScope.None);
return services;
}
public static Container AddTransient<TService>(this Container services, Func<Container, TService> implementationFactory) where TService : class
{
services.Register(implementationFactory).ReusedWithin(ReuseScope.None);
return services;
}
public static Container AddTransient<TService, TImplementation>(this Container services)
where TService : class
where TImplementation : class, TService
{
services.RegisterAutoWiredAs<TImplementation, TService>().ReusedWithin(ReuseScope.None);
return services;
}
public static Container AddTransient<TService, TImplementation>(this Container services, Func<Container, TImplementation> implementationFactory)
where TService : class
where TImplementation : class, TService
{
services.Register<TService>(implementationFactory).ReusedWithin(ReuseScope.None);
return services;
}
}
}