Skip to content

Commit

Permalink
Merge pull request #17 from westermo/feature/support-old-syntax
Browse files Browse the repository at this point in the history
Moved to using old syntax for Array creation to support older .net versions
  • Loading branch information
carl-andersson-at-westermo authored May 24, 2024
2 parents a290ecb + c257aa0 commit ae7bd86
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Benchmarking/Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Running;
using FactoryGenerator;
using Inherited;
Expand All @@ -25,6 +26,9 @@ public class ResolveBenchmarks
[Benchmark]
public IOverridable ResolveTransient() => m_container.Resolve<IOverridable>();

[Benchmark]
public List<IRequestedArray> ResolveArray() => (List<IRequestedArray>) m_container.Resolve<IEnumerable<IRequestedArray>>();

[Benchmark]
public IContainer Create() => new DependencyInjectionContainer(default, default, default!);
}
Expand Down
20 changes: 11 additions & 9 deletions FactoryGenerator/FactoryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private static IEnumerable<string> GenerateCode(ImmutableArray<Injection> dataIn
log.Log(LogLevel.Debug, "Starting Code Generation");
var usingStatements = $@"
using System;
using System.Linq;
using System.Collections.Generic;
using FactoryGenerator;
using System.CodeDom.Compiler;
Expand Down Expand Up @@ -583,25 +584,26 @@ public partial class {className}
private static void MakeArray(Dictionary<string, string> declarations, string name, INamedTypeSymbol type,
Dictionary<INamedTypeSymbol, List<Injection>> interfaceInjectors, bool function = false)
{
var factoryName = "[]";
var factoryName = $"new {type}[0]";
var factory = string.Empty;
if (interfaceInjectors.TryGetValue(type, out var injections))
{
factoryName = $"Create{name}()".Replace("_", "");
factory = @$"
{type}[] {factoryName}
IEnumerable<{type}> {factoryName}
{{
{type}[] source = [{string.Join(", ", injections.Where(i => i.BooleanInjection == null).Select(i => i.Name))}];
{string.Join("\n\t\t\t", injections.Where(b => b.BooleanInjection != null)
.Select(i => $"if({i.BooleanInjection!.Key}) source = [..source, {i.Name}];"))}
List<{type}> source = new List<{type}> {{
{string.Join(",\n\t\t\t", injections.Where(i => i.BooleanInjection == null).Select(i => i.Name))}
}};
{string.Join("\n\t\t\t", injections.Where(b => b.BooleanInjection != null).Select(i => $"if({i.BooleanInjection!.Key}) source.Add({i.Name});"))}
return source;
}}";
}

if (function)
{
declarations[name] = $@"
internal {type}[] {name}()
internal IEnumerable<{type}> {name}()
{{
if (m_{name} != null)
return m_{name};
Expand All @@ -613,12 +615,12 @@ private static void MakeArray(Dictionary<string, string> declarations, string na
return m_{name} = {factoryName};
}}
}}
internal {type}[]? m_{name};" + factory;
internal IEnumerable<{type}>? m_{name};" + factory;
}
else
{
declarations[name] = $@"
internal {type}[] {name}
internal IEnumerable<{type}> {name}
{{
get
{{
Expand All @@ -633,7 +635,7 @@ private static void MakeArray(Dictionary<string, string> declarations, string na
}}
}}
}}
internal {type}[]? m_{name};" + factory;
internal IEnumerable<{type}>? m_{name};" + factory;
}
}

Expand Down

0 comments on commit ae7bd86

Please sign in to comment.