Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to using old syntax for Array creation to support older .net versions #17

Merged
merged 6 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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