This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from Barsonax/feature/morecollectionssupport
Added support for arrays, lists and hashsets
- Loading branch information
Showing
13 changed files
with
263 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
src/Singularity/Graph/Resolvers/CollectionServiceBindingGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using Singularity.Collections; | ||
using Singularity.Expressions; | ||
|
||
namespace Singularity.Graph.Resolvers | ||
{ | ||
/// <summary> | ||
/// Creates bindings for resolving all services of a given type. | ||
/// </summary> | ||
public sealed class CollectionServiceBindingGenerator : IServiceBindingGenerator | ||
{ | ||
private static readonly MethodInfo GenericResolveMethod = typeof(CollectionServiceBindingGenerator).GetRuntimeMethods().Single(x => x.Name == nameof(Resolve) && x.ContainsGenericParameters); | ||
|
||
/// <inheritdoc /> | ||
public IEnumerable<ServiceBinding> Resolve(IResolverPipeline graph, Type type) | ||
{ | ||
Type? elementType = null; | ||
if (type.IsArray) | ||
{ | ||
elementType = type.GetElementType(); | ||
} | ||
else if (type.IsGenericType) | ||
{ | ||
Type openGenericCollectionType = type.GetGenericTypeDefinition(); | ||
|
||
Type[] collectionTypes = | ||
{ | ||
typeof(IEnumerable<>), | ||
typeof(IReadOnlyCollection<>), | ||
typeof(IReadOnlyList<>), | ||
|
||
typeof(List<>), | ||
typeof(ICollection<>), | ||
|
||
typeof(IList<>), | ||
|
||
typeof(HashSet<>), | ||
typeof(ISet<>), | ||
}; | ||
if (collectionTypes.Contains(openGenericCollectionType)) | ||
{ | ||
elementType = type.GenericTypeArguments[0]; | ||
|
||
} | ||
} | ||
|
||
if (elementType != null) | ||
{ | ||
MethodInfo resolveMethod = GenericResolveMethod.MakeGenericMethod(elementType); | ||
|
||
var bindings = (IEnumerable<ServiceBinding>)resolveMethod.Invoke(this, new object[] { graph, type }); | ||
foreach (var binding in bindings) | ||
{ | ||
yield return binding; | ||
} | ||
} | ||
} | ||
|
||
private IEnumerable<ServiceBinding> Resolve<TElement>(IResolverPipeline graph, Type type) | ||
{ | ||
Func<Scoped, TElement>[] instanceFactories = graph.TryResolveAll(typeof(TElement)).Select(x => (Func<Scoped, TElement>)(Delegate)x.Factory).ToArray(); | ||
|
||
yield return new ServiceBinding(new[] | ||
{ | ||
typeof(Func<Scoped, TElement>[]), | ||
}, BindingMetadata.GeneratedInstance, Expression.Constant(instanceFactories), typeof(Func<Scoped, TElement>[]), ConstructorResolvers.Default, Lifetimes.PerContainer); | ||
|
||
Expression expression = ConstructorResolvers.Default.ResolveConstructorExpression(typeof(InstanceFactoryList<TElement>))!; | ||
|
||
yield return new ServiceBinding(new[] | ||
{ | ||
typeof(IEnumerable<TElement>), | ||
typeof(IReadOnlyCollection<TElement>), | ||
typeof(IReadOnlyList<TElement>), | ||
}, BindingMetadata.GeneratedInstance, expression, expression.GetReturnType(), ConstructorResolvers.Default, Lifetimes.Transient); | ||
|
||
//lists | ||
Expression<Func<Scoped, List<TElement>>> listExpression = scope => CreateList(scope, instanceFactories); | ||
yield return new ServiceBinding(new[] | ||
{ | ||
typeof(List<TElement>), | ||
typeof(ICollection<TElement>), | ||
}, BindingMetadata.GeneratedInstance, listExpression, listExpression.GetReturnType(), ConstructorResolvers.Default, Lifetimes.Transient); | ||
|
||
//sets | ||
Expression<Func<Scoped, HashSet<TElement>>> setExpression = scope => CreateSet(scope, instanceFactories); | ||
yield return new ServiceBinding(new[] | ||
{ | ||
typeof(HashSet<TElement>), | ||
typeof(ISet<TElement>), | ||
}, BindingMetadata.GeneratedInstance, setExpression, setExpression.GetReturnType(), ConstructorResolvers.Default, Lifetimes.Transient); | ||
|
||
//arrays | ||
Expression<Func<Scoped, TElement[]>> arrayExpression = scope => CreateArray(scope, instanceFactories); | ||
yield return new ServiceBinding(new[] | ||
{ | ||
typeof(TElement[]), | ||
typeof(IList<TElement>), | ||
}, BindingMetadata.GeneratedInstance, arrayExpression, arrayExpression.GetReturnType(), ConstructorResolvers.Default, Lifetimes.Transient); | ||
} | ||
|
||
private static List<TElement> CreateList<TElement>(Scoped scope, Func<Scoped, TElement>[] instanceFactories) | ||
{ | ||
var list = new List<TElement>(instanceFactories.Length); | ||
|
||
foreach (Func<Scoped, TElement> instanceFactory in instanceFactories) | ||
{ | ||
list.Add(instanceFactory.Invoke(scope)); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
private static HashSet<TElement> CreateSet<TElement>(Scoped scope, Func<Scoped, TElement>[] instanceFactories) | ||
{ | ||
var list = new HashSet<TElement>(); | ||
|
||
foreach (Func<Scoped, TElement> instanceFactory in instanceFactories) | ||
{ | ||
list.Add(instanceFactory.Invoke(scope)); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
private static TElement[] CreateArray<TElement>(Scoped scope, Func<Scoped, TElement>[] instanceFactories) | ||
{ | ||
var list = new TElement[instanceFactories.Length]; | ||
|
||
for (int i = 0; i < instanceFactories.Length; i++) | ||
{ | ||
list[i] = instanceFactories[i].Invoke(scope); | ||
} | ||
|
||
return list; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 0 additions & 44 deletions
44
src/Singularity/Graph/Resolvers/EnumerableServiceBindingGenerator.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.