Skip to content

Commit

Permalink
Support ReadOnlyCollection, Collection, ReadOnlySet
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Nov 28, 2024
1 parent 1d6f3cc commit f6e3364
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
15 changes: 11 additions & 4 deletions src/Pure.DI.Core/Features/Default.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,19 @@ private static void Setup()
.To(_ => global::System.Buffers.MemoryPool<TT>.Shared)
.Bind<global::System.Buffers.ArrayPool<TT>>()
.To(_ => global::System.Buffers.ArrayPool<TT>.Shared)
#endif
.Bind<global::System.Collections.Generic.ICollection<TT>>()
#endif
.Bind<global::System.Collections.Generic.IList<TT>>()
.Bind<global::System.Collections.Generic.List<TT>>()
.To((TT[] arr) => new global::System.Collections.Generic.List<TT>(arr))
.Bind<global::System.Collections.Generic.ICollection<TT>>()
.Bind<global::System.Collections.ObjectModel.Collection<TT>>()
.To((TT[] arr) => new global::System.Collections.ObjectModel.Collection<TT>(arr))
#if NETSTANDARD || NET || NETCOREAPP || NET45_OR_GREATER
.Bind<global::System.Collections.Generic.IReadOnlyCollection<TT>>()
.Bind<global::System.Collections.Generic.IReadOnlyList<TT>>()
.To((TT[] arr) => arr)
#endif
.Bind<global::System.Collections.ObjectModel.ReadOnlyCollection<TT>>()
.To((TT[] arr) => new global::System.Collections.ObjectModel.ReadOnlyCollection<TT>(arr))
#if NETSTANDARD1_1_OR_GREATER || NET || NETCOREAPP || NET40_OR_GREATER
.Bind<global::System.Collections.Concurrent.IProducerConsumerCollection<TT>>()
.Bind<global::System.Collections.Concurrent.ConcurrentBag<TT>>()
Expand All @@ -141,7 +144,11 @@ private static void Setup()
#if NETSTANDARD || NET || NETCOREAPP || NET45_OR_GREATER
.Bind<global::System.Collections.Generic.SortedSet<TT>>()
.To((TT[] arr) => new global::System.Collections.Generic.SortedSet<TT>(arr))
#endif
#endif
#if NET9_0_OR_GREATER
.Bind<global::System.Collections.ObjectModel.ReadOnlySet<TT>>()
.To((global::System.Collections.Generic.ISet<TT> val) => new global::System.Collections.ObjectModel.ReadOnlySet<TT>(val))
#endif
.Bind<global::System.Collections.Generic.Queue<TT>>()
.To((TT[] arr) => new global::System.Collections.Generic.Queue<TT>(arr))
.Bind<global::System.Collections.Generic.Stack<TT>>()
Expand Down
28 changes: 17 additions & 11 deletions tests/Pure.DI.IntegrationTests/BclInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class BclInjectionTests
[InlineData("System.Collections.Concurrent.ConcurrentQueue")]
[InlineData("System.Collections.Concurrent.ConcurrentStack")]
[InlineData("System.Collections.Concurrent.BlockingCollection")]
[InlineData("System.Collections.ObjectModel.ReadOnlySet")]
[InlineData("System.Collections.ObjectModel.Collection")]
[InlineData("System.Collections.ObjectModel.ReadOnlyCollection")]
public async Task ShouldSupportCollectionInjection(string collectionType, LanguageVersion languageVersion = LanguageVersion.CSharp9)
{
// Given
Expand All @@ -43,7 +46,7 @@ namespace Sample
{
struct Point
{
int X, Y;
int X, Y;
public Point(int x, int y)
{
Expand Down Expand Up @@ -75,7 +78,7 @@ class Service: IService
public Service(###CollectionType###<IDependency> deps)
{
Console.WriteLine("Service creating");
}
}
}
static class Setup
Expand Down Expand Up @@ -103,7 +106,7 @@ public static void Main()
{
var composition = new Composition();
var service = composition.Service;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Expand Down Expand Up @@ -151,6 +154,7 @@ public static void Main()
[InlineData("System.Collections.Concurrent.ConcurrentQueue")]
[InlineData("System.Collections.Concurrent.ConcurrentStack")]
[InlineData("System.Collections.Concurrent.BlockingCollection")]
[InlineData("System.Collections.ObjectModel.ReadOnlySet")]
public async Task ShouldSupportCollectionInjectionWhenGeneric(string collectionType, LanguageVersion languageVersion = LanguageVersion.CSharp9)
{
// Given
Expand All @@ -164,7 +168,7 @@ namespace Sample
{
struct Point
{
int X, Y;
int X, Y;
public Point(int x, int y)
{
Expand Down Expand Up @@ -196,7 +200,7 @@ class Service: IService
public Service(###CollectionType###<IDependency<int>> deps)
{
Console.WriteLine("Service creating");
}
}
}
static class Setup
Expand All @@ -223,7 +227,7 @@ public static void Main()
{
var composition = new Composition();
var service = composition.Service;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Expand Down Expand Up @@ -271,6 +275,8 @@ public static void Main()
[InlineData("System.Collections.Concurrent.ConcurrentQueue")]
[InlineData("System.Collections.Concurrent.ConcurrentStack")]
[InlineData("System.Collections.Concurrent.BlockingCollection")]
[InlineData("System.Collections.ObjectModel.Collection")]
[InlineData("System.Collections.ObjectModel.ReadOnlyCollection")]
public async Task ShouldSupportCollectionInjectionWhenHasNoBindings(string collectionType, LanguageVersion languageVersion = LanguageVersion.CSharp9)
{
// Given
Expand All @@ -293,7 +299,7 @@ class Service: IService
public Service(###CollectionType###<IDependency> deps)
{
Console.WriteLine("Service creating");
}
}
}
static class Setup
Expand All @@ -314,7 +320,7 @@ public static void Main()
{
var composition = new Composition();
var service = composition.Service;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Expand Down Expand Up @@ -350,7 +356,7 @@ namespace Sample
{
struct Point
{
int X, Y;
int X, Y;
public Point(int x, int y)
{
Expand Down Expand Up @@ -386,7 +392,7 @@ public Service(###CollectionType###<IDependency> deps)
}
Console.WriteLine("Service creating");
}
}
}
static class Setup
Expand All @@ -413,7 +419,7 @@ public static void Main()
{
var composition = new Composition();
var service = composition.Service;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Expand Down

0 comments on commit f6e3364

Please sign in to comment.