Skip to content

Commit 1e911b3

Browse files
committed
Add non-generic decoration overloads and keyed support
1 parent 00ec523 commit 1e911b3

21 files changed

Lines changed: 938 additions & 250 deletions

src/Injectio.Generators/ServiceRegistrationWriter.cs

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private static void WriteRegistration(
148148
if (serviceRegistration.Tags.Count > 0)
149149
{
150150
codeBuilder
151-
.Append("if (tagSet.Count == 0 || tagSet.Intersect(new[] { ");
151+
.Append("if (tagSet.Count == 0 || global::System.Linq.Enumerable.Any(global::System.Linq.Enumerable.Intersect(tagSet, new[] { ");
152152

153153
bool wroteTag = false;
154154
foreach (var tag in serviceRegistration.Tags)
@@ -165,7 +165,7 @@ private static void WriteRegistration(
165165
}
166166

167167
codeBuilder
168-
.AppendLine(" }).Any())")
168+
.AppendLine(" })))")
169169
.AppendLine("{")
170170
.IncrementIndent();
171171
}
@@ -343,7 +343,7 @@ private static void WriteDecorator(
343343
if (decorator.Tags.Count > 0)
344344
{
345345
codeBuilder
346-
.Append("if (tagSet.Count == 0 || tagSet.Intersect(new[] { ");
346+
.Append("if (tagSet.Count == 0 || global::System.Linq.Enumerable.Any(global::System.Linq.Enumerable.Intersect(tagSet, new[] { ");
347347

348348
bool wroteTag = false;
349349
foreach (var tag in decorator.Tags)
@@ -360,7 +360,7 @@ private static void WriteDecorator(
360360
}
361361

362362
codeBuilder
363-
.AppendLine(" }).Any())")
363+
.AppendLine(" })))")
364364
.AppendLine("{")
365365
.IncrementIndent();
366366
}
@@ -393,7 +393,9 @@ private static void WriteDecorator(
393393
.Append("typeof(")
394394
.AppendIf("global::", !decoratorType.StartsWith("global::"))
395395
.Append(decoratorType)
396-
.AppendLine(")")
396+
.Append(")")
397+
.AppendLine(",")
398+
.AppendLine(keyExpression)
397399
.DecrementIndent()
398400
.AppendLine(");")
399401
.AppendLine();
@@ -404,16 +406,26 @@ private static void WriteDecorator(
404406
.Append("global::Injectio.Extensions.DecorationExtensions.DecorateKeyed<")
405407
.AppendIf("global::", !serviceType.StartsWith("global::"))
406408
.Append(serviceType)
409+
.AppendIf(", ", decorator.Factory.IsNullOrEmpty())
410+
.AppendIf("global::", decorator.Factory.IsNullOrEmpty() && !decoratorType.StartsWith("global::"))
411+
.AppendIf(decoratorType, decorator.Factory.IsNullOrEmpty())
407412
.AppendLine(">(")
408413
.IncrementIndent()
409414
.AppendLine("serviceCollection,")
410-
.Append(keyExpression)
411-
.AppendLine(",");
415+
.Append(keyExpression);
412416

413-
WriteDecoratorFactory(codeBuilder, decorator, isKeyed: true);
417+
if (decorator.Factory.HasValue())
418+
{
419+
codeBuilder.AppendLine(",");
420+
WriteDecoratorFactory(codeBuilder, decorator, isKeyed: true);
421+
codeBuilder.AppendLine();
422+
}
423+
else
424+
{
425+
codeBuilder.AppendLine();
426+
}
414427

415428
codeBuilder
416-
.AppendLine()
417429
.DecrementIndent()
418430
.AppendLine(");")
419431
.AppendLine();
@@ -424,14 +436,25 @@ private static void WriteDecorator(
424436
.Append("global::Injectio.Extensions.DecorationExtensions.Decorate<")
425437
.AppendIf("global::", !serviceType.StartsWith("global::"))
426438
.Append(serviceType)
439+
.AppendIf(", ", decorator.Factory.IsNullOrEmpty())
440+
.AppendIf("global::", decorator.Factory.IsNullOrEmpty() && !decoratorType.StartsWith("global::"))
441+
.AppendIf(decoratorType, decorator.Factory.IsNullOrEmpty())
427442
.AppendLine(">(")
428443
.IncrementIndent()
429-
.AppendLine("serviceCollection,");
444+
.Append("serviceCollection");
430445

431-
WriteDecoratorFactory(codeBuilder, decorator, isKeyed: false);
446+
if (decorator.Factory.HasValue())
447+
{
448+
codeBuilder.AppendLine(",");
449+
WriteDecoratorFactory(codeBuilder, decorator, isKeyed: false);
450+
codeBuilder.AppendLine();
451+
}
452+
else
453+
{
454+
codeBuilder.AppendLine();
455+
}
432456

433457
codeBuilder
434-
.AppendLine()
435458
.DecrementIndent()
436459
.AppendLine(");")
437460
.AppendLine();
@@ -451,47 +474,25 @@ private static void WriteDecoratorFactory(
451474
DecoratorRegistration decorator,
452475
bool isKeyed)
453476
{
454-
var serviceType = decorator.ServiceType;
455477
var decoratorType = decorator.DecoratorType;
456-
var qualifiedService = serviceType.StartsWith("global::") ? serviceType : "global::" + serviceType;
457478
var qualifiedDecorator = decoratorType.StartsWith("global::") ? decoratorType : "global::" + decoratorType;
458479

459-
if (decorator.Factory.HasValue())
460-
{
461-
bool hasNamespace = decorator.Factory!.Contains(".");
462-
var factoryTarget = hasNamespace ? decorator.Factory! : qualifiedDecorator + "." + decorator.Factory;
480+
bool hasNamespace = decorator.Factory!.Contains(".");
481+
var factoryTarget = hasNamespace ? decorator.Factory! : qualifiedDecorator + "." + decorator.Factory;
463482

464-
if (isKeyed)
465-
{
466-
codeBuilder
467-
.Append("static (serviceProvider, serviceKey, inner) => ")
468-
.Append(factoryTarget)
469-
.Append("(serviceProvider, serviceKey, inner)");
470-
}
471-
else
472-
{
473-
codeBuilder
474-
.Append("static (serviceProvider, inner) => ")
475-
.Append(factoryTarget)
476-
.Append("(serviceProvider, inner)");
477-
}
483+
if (isKeyed)
484+
{
485+
codeBuilder
486+
.Append("static (serviceProvider, serviceKey, inner) => ")
487+
.Append(factoryTarget)
488+
.Append("(serviceProvider, serviceKey, inner)");
478489
}
479490
else
480491
{
481-
if (isKeyed)
482-
{
483-
codeBuilder
484-
.Append("static (serviceProvider, serviceKey, inner) => global::Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance<")
485-
.Append(qualifiedDecorator)
486-
.Append(">(serviceProvider, inner)");
487-
}
488-
else
489-
{
490-
codeBuilder
491-
.Append("static (serviceProvider, inner) => global::Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance<")
492-
.Append(qualifiedDecorator)
493-
.Append(">(serviceProvider, inner)");
494-
}
492+
codeBuilder
493+
.Append("static (serviceProvider, inner) => ")
494+
.Append(factoryTarget)
495+
.Append("(serviceProvider, inner)");
495496
}
496497
}
497498

0 commit comments

Comments
 (0)