Skip to content

Commit dadb0ea

Browse files
Merge pull request #10 from ChaseDRedmon/try-keyed-attributes
TryAdd, Keyed, and TryAddKeyed Attributes
2 parents 0323a2d + 064ade9 commit dadb0ea

23 files changed

+1100
-18
lines changed

src/AutoRegisterInject.IntegrationTests/AutoRegisterInject.IntegrationTest.Project1/Class1.cs

Lines changed: 220 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,20 @@ public partial class PartialClassTest
2727

2828
}
2929

30-
[RegisterTransient, RegisterSingleton, RegisterScoped]
30+
[
31+
RegisterTransient,
32+
RegisterSingleton,
33+
RegisterScoped,
34+
TryRegisterScoped,
35+
TryRegisterSingleton,
36+
TryRegisterTransient,
37+
TryRegisterKeyedScoped(serviceKey: "TryRegisterKeyedScoped"),
38+
TryRegisterKeyedSingleton(serviceKey: "TryRegisterKeyedSingleton"),
39+
TryRegisterKeyedTransient(serviceKey: "TryRegisterKeyedScoped"),
40+
RegisterKeyedScoped(serviceKey: "RegisterKeyedScoped"),
41+
RegisterKeyedSingleton(serviceKey: "RegisterKeyedSingleton"),
42+
RegisterKeyedTransient(serviceKey: "RegisterKeyedTransient")
43+
]
3144
public class MultipleRegisterTest
3245
{
3346

@@ -49,6 +62,60 @@ public class SingletonTest
4962
public class TransientTest
5063
{
5164

65+
}
66+
67+
[TryRegisterScoped]
68+
public class TryScopedTest
69+
{
70+
71+
}
72+
73+
[TryRegisterSingleton]
74+
public class TrySingletonTest
75+
{
76+
77+
}
78+
79+
[TryRegisterTransient]
80+
public class TryTransientTest
81+
{
82+
83+
}
84+
85+
[TryRegisterKeyedScoped(serviceKey: "TryRegisterKeyedScoped")]
86+
public class TryKeyedScopedTest
87+
{
88+
89+
}
90+
91+
[TryRegisterKeyedSingleton(serviceKey: "TryRegisterKeyedSingleton")]
92+
public class TryKeyedSingletonTest
93+
{
94+
95+
}
96+
97+
[TryRegisterKeyedTransient(serviceKey: "TryRegisterKeyedScoped")]
98+
public class TryKeyedTransientTest
99+
{
100+
101+
}
102+
103+
[RegisterKeyedScoped(serviceKey: "RegisterKeyedScoped")]
104+
public class KeyedScopedTest
105+
{
106+
107+
}
108+
109+
[RegisterKeyedSingleton(serviceKey: "RegisterKeyedSingleton")]
110+
public class KeyedSingletonTest
111+
{
112+
113+
}
114+
115+
[RegisterKeyedTransient(serviceKey: "RegisterKeyedTransient")]
116+
public class KeyedTransientTest
117+
{
118+
52119
}
53120

54121
public interface IInterfaceTest
@@ -57,7 +124,25 @@ public interface IInterfaceTest
57124
}
58125

59126
[RegisterScoped]
60-
public class InterfaceTest : IInterfaceTest
127+
public class RegisterScopedInterfaceTest : IInterfaceTest
128+
{
129+
130+
}
131+
132+
[TryRegisterScoped]
133+
public class TryRegisterScopedInterfaceTest : IInterfaceTest
134+
{
135+
136+
}
137+
138+
[TryRegisterKeyedScoped(serviceKey: "TryRegisterKeyedScopedInterface")]
139+
public class TryRegisterKeyedScopedInterfaceTest : IInterfaceTest
140+
{
141+
142+
}
143+
144+
[RegisterKeyedScoped(serviceKey: "RegisterKeyedScopedInterface")]
145+
public class RegisterKeyedScopedInterfaceTest : IInterfaceTest
61146
{
62147

63148
}
@@ -68,7 +153,49 @@ public interface IMultiInterfaceTest
68153
}
69154

70155
[RegisterScoped]
71-
public class MultiInterfaceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable
156+
public class RegisterMultiInterfaceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable
157+
{
158+
public void Dispose()
159+
{
160+
// TODO release managed resources here
161+
}
162+
163+
public ValueTask DisposeAsync()
164+
{
165+
return new ValueTask();
166+
}
167+
}
168+
169+
[TryRegisterScoped]
170+
public class TryRegisterMultiInterfaceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable
171+
{
172+
public void Dispose()
173+
{
174+
// TODO release managed resources here
175+
}
176+
177+
public ValueTask DisposeAsync()
178+
{
179+
return new ValueTask();
180+
}
181+
}
182+
183+
[TryRegisterKeyedScoped(serviceKey: "TryRegisterKeyedScopedMultipleInterface")]
184+
public class TryRegisterKeyedMultiInterfaceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable
185+
{
186+
public void Dispose()
187+
{
188+
// TODO release managed resources here
189+
}
190+
191+
public ValueTask DisposeAsync()
192+
{
193+
return new ValueTask();
194+
}
195+
}
196+
197+
[RegisterKeyedScoped(serviceKey: "RegisterKeyedScopedMultipleInterface")]
198+
public class RegisterKeyedMultiInterfaceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable
72199
{
73200
public void Dispose()
74201
{
@@ -86,8 +213,9 @@ public interface IIgnore
86213

87214
}
88215

216+
// Multiple Interface Single Ignorance
89217
[RegisterScoped(onlyRegisterAs: typeof(IIgnore))]
90-
public class MultiInterfaceIgnoranceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable, IIgnore
218+
public class RegisterScopedMultiInterfaceIgnoranceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable, IIgnore
91219
{
92220
public void Dispose()
93221
{
@@ -99,9 +227,95 @@ public ValueTask DisposeAsync()
99227
return new ValueTask();
100228
}
101229
}
230+
231+
[TryRegisterScoped(onlyRegisterAs: typeof(IIgnore))]
232+
public class TryRegisterScopedMultiInterfaceIgnoranceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable, IIgnore
233+
{
234+
public void Dispose()
235+
{
236+
// TODO release managed resources here
237+
}
102238

239+
public ValueTask DisposeAsync()
240+
{
241+
return new ValueTask();
242+
}
243+
}
244+
245+
[TryRegisterKeyedScoped(serviceKey: "TryRegisterKeyedScopedMultipleInterfaceSingleIgnore", onlyRegisterAs: typeof(IIgnore))]
246+
public class TryRegisterKeyedScopedMultiInterfaceIgnoranceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable, IIgnore
247+
{
248+
public void Dispose()
249+
{
250+
// TODO release managed resources here
251+
}
252+
253+
public ValueTask DisposeAsync()
254+
{
255+
return new ValueTask();
256+
}
257+
}
258+
259+
[RegisterKeyedScoped(serviceKey: "RegisterKeyedScopedMultipleInterfaceSingleIgnore", onlyRegisterAs: typeof(IIgnore))]
260+
public class RegisterKeyedScopedMultiInterfaceIgnoranceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable, IIgnore
261+
{
262+
public void Dispose()
263+
{
264+
// TODO release managed resources here
265+
}
266+
267+
public ValueTask DisposeAsync()
268+
{
269+
return new ValueTask();
270+
}
271+
}
272+
273+
274+
// Multiple Interface Multiple Ignorance
103275
[RegisterScoped(typeof(IIgnore), typeof(IInterfaceTest))]
104-
public class MultiInterfaceMultiIgnoranceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable, IIgnore
276+
public class RegisterScopedMultiInterfaceMultiIgnoranceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable, IIgnore
277+
{
278+
public void Dispose()
279+
{
280+
// TODO release managed resources here
281+
}
282+
283+
public ValueTask DisposeAsync()
284+
{
285+
return new ValueTask();
286+
}
287+
}
288+
289+
[TryRegisterScoped(typeof(IIgnore), typeof(IInterfaceTest))]
290+
public class TryRegisterScopedMultiInterfaceMultiIgnoranceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable, IIgnore
291+
{
292+
public void Dispose()
293+
{
294+
// TODO release managed resources here
295+
}
296+
297+
public ValueTask DisposeAsync()
298+
{
299+
return new ValueTask();
300+
}
301+
}
302+
303+
[TryRegisterKeyedScoped(serviceKey: "TryRegisterKeyedScopedMultipleInterfaceMultipleIgnore", typeof(IIgnore), typeof(IInterfaceTest))]
304+
public class TryRegisterKeyedScopedMultiInterfaceMultiIgnoranceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable, IIgnore
305+
{
306+
public void Dispose()
307+
{
308+
// TODO release managed resources here
309+
}
310+
311+
public ValueTask DisposeAsync()
312+
{
313+
return new ValueTask();
314+
}
315+
}
316+
317+
[RegisterKeyedScoped(serviceKey: "RegisterKeyedScopedMultipleInterfaceMultipleIgnore", typeof(IIgnore), typeof(IInterfaceTest))]
318+
public class RegisterKeyedScopedMultiInterfaceMultiIgnoranceTest : IInterfaceTest, IMultiInterfaceTest, IDisposable, IAsyncDisposable, IIgnore
105319
{
106320
public void Dispose()
107321
{
@@ -113,4 +327,4 @@ public ValueTask DisposeAsync()
113327
return new ValueTask();
114328
}
115329
}
116-
}
330+
}

src/AutoRegisterInject.Tests/GenerationTests.Cases.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public class Baz { }
2727
[RegisterSingleton]
2828
public class Bang : IBaz { }
2929
30+
[TryRegisterScoped]
31+
public class Far { }
32+
33+
[RegisterKeyedTransient(serviceKey: ""MyFazKey"")]
34+
public class Faz { }
35+
36+
[TryRegisterKeyedSingleton(serviceKey: ""MyFangKey"")]
37+
public class Fang : IBaz { }
38+
3039
public interface IBaz { }
3140
";
3241

@@ -35,6 +44,7 @@ public interface IBaz { }
3544
// Changes made to this file may be lost and may cause undesirable behaviour.
3645
// </auto-generated>
3746
using Microsoft.Extensions.DependencyInjection;
47+
using Microsoft.Extensions.DependencyInjection.Extensions;
3848
public static class AutoRegisterInjectServiceCollectionExtension
3949
{
4050
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromTestProject(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
@@ -48,6 +58,9 @@ internal static Microsoft.Extensions.DependencyInjection.IServiceCollection Auto
4858
serviceCollection.AddScoped<Bar>();
4959
serviceCollection.AddTransient<Baz>();
5060
serviceCollection.AddSingleton<IBaz, Bang>();
61+
serviceCollection.TryAddScoped<Far>();
62+
serviceCollection.AddKeyedTransient<Faz>(""MyFazKey"");
63+
serviceCollection.TryAddKeyedSingleton<IBaz, Fang>(""MyFangKey"");
5164
return serviceCollection;
5265
}
5366
}";

src/AutoRegisterInject.Tests/GenerationTests.Hosted.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class Foo : IHostedService
2323
// Changes made to this file may be lost and may cause undesirable behaviour.
2424
// </auto-generated>
2525
using Microsoft.Extensions.DependencyInjection;
26+
using Microsoft.Extensions.DependencyInjection.Extensions;
2627
public static class AutoRegisterInjectServiceCollectionExtension
2728
{
2829
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromTestProject(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
@@ -61,6 +62,7 @@ public interface IFoo { }
6162
// Changes made to this file may be lost and may cause undesirable behaviour.
6263
// </auto-generated>
6364
using Microsoft.Extensions.DependencyInjection;
65+
using Microsoft.Extensions.DependencyInjection.Extensions;
6466
public static class AutoRegisterInjectServiceCollectionExtension
6567
{
6668
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromTestProject(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
@@ -97,6 +99,7 @@ public class Foo : BackgroundService
9799
// Changes made to this file may be lost and may cause undesirable behaviour.
98100
// </auto-generated>
99101
using Microsoft.Extensions.DependencyInjection;
102+
using Microsoft.Extensions.DependencyInjection.Extensions;
100103
public static class AutoRegisterInjectServiceCollectionExtension
101104
{
102105
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromTestProject(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)

src/AutoRegisterInject.Tests/GenerationTests.Inheritance.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Bar : Foo { }
1717
// Changes made to this file may be lost and may cause undesirable behaviour.
1818
// </auto-generated>
1919
using Microsoft.Extensions.DependencyInjection;
20+
using Microsoft.Extensions.DependencyInjection.Extensions;
2021
public static class AutoRegisterInjectServiceCollectionExtension
2122
{
2223
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromTestProject(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
@@ -48,6 +49,7 @@ public class Bar : Foo { }
4849
// Changes made to this file may be lost and may cause undesirable behaviour.
4950
// </auto-generated>
5051
using Microsoft.Extensions.DependencyInjection;
52+
using Microsoft.Extensions.DependencyInjection.Extensions;
5153
public static class AutoRegisterInjectServiceCollectionExtension
5254
{
5355
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromTestProject(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
@@ -81,6 +83,7 @@ public interface IBar { }
8183
// Changes made to this file may be lost and may cause undesirable behaviour.
8284
// </auto-generated>
8385
using Microsoft.Extensions.DependencyInjection;
86+
using Microsoft.Extensions.DependencyInjection.Extensions;
8487
public static class AutoRegisterInjectServiceCollectionExtension
8588
{
8689
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromTestProject(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)

0 commit comments

Comments
 (0)