From f6e33640f1fd256a93ae74c4b9006041dc05aa4d Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Thu, 28 Nov 2024 15:24:17 +0300 Subject: [PATCH] Support ReadOnlyCollection, Collection, ReadOnlySet --- src/Pure.DI.Core/Features/Default.g.cs | 15 +++++++--- .../BclInjectionTests.cs | 28 +++++++++++-------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/Pure.DI.Core/Features/Default.g.cs b/src/Pure.DI.Core/Features/Default.g.cs index 9abd4203..71e942c1 100644 --- a/src/Pure.DI.Core/Features/Default.g.cs +++ b/src/Pure.DI.Core/Features/Default.g.cs @@ -109,16 +109,19 @@ private static void Setup() .To(_ => global::System.Buffers.MemoryPool.Shared) .Bind>() .To(_ => global::System.Buffers.ArrayPool.Shared) -#endif - .Bind>() +#endif .Bind>() .Bind>() .To((TT[] arr) => new global::System.Collections.Generic.List(arr)) + .Bind>() + .Bind>() + .To((TT[] arr) => new global::System.Collections.ObjectModel.Collection(arr)) #if NETSTANDARD || NET || NETCOREAPP || NET45_OR_GREATER .Bind>() .Bind>() - .To((TT[] arr) => arr) #endif + .Bind>() + .To((TT[] arr) => new global::System.Collections.ObjectModel.ReadOnlyCollection(arr)) #if NETSTANDARD1_1_OR_GREATER || NET || NETCOREAPP || NET40_OR_GREATER .Bind>() .Bind>() @@ -141,7 +144,11 @@ private static void Setup() #if NETSTANDARD || NET || NETCOREAPP || NET45_OR_GREATER .Bind>() .To((TT[] arr) => new global::System.Collections.Generic.SortedSet(arr)) -#endif +#endif +#if NET9_0_OR_GREATER + .Bind>() + .To((global::System.Collections.Generic.ISet val) => new global::System.Collections.ObjectModel.ReadOnlySet(val)) +#endif .Bind>() .To((TT[] arr) => new global::System.Collections.Generic.Queue(arr)) .Bind>() diff --git a/tests/Pure.DI.IntegrationTests/BclInjectionTests.cs b/tests/Pure.DI.IntegrationTests/BclInjectionTests.cs index 50bba170..b4e48c34 100644 --- a/tests/Pure.DI.IntegrationTests/BclInjectionTests.cs +++ b/tests/Pure.DI.IntegrationTests/BclInjectionTests.cs @@ -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 @@ -43,7 +46,7 @@ namespace Sample { struct Point { - int X, Y; + int X, Y; public Point(int x, int y) { @@ -75,7 +78,7 @@ class Service: IService public Service(###CollectionType### deps) { Console.WriteLine("Service creating"); - } + } } static class Setup @@ -103,7 +106,7 @@ public static void Main() { var composition = new Composition(); var service = composition.Service; - } + } catch(Exception ex) { Console.WriteLine(ex.Message); @@ -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 @@ -164,7 +168,7 @@ namespace Sample { struct Point { - int X, Y; + int X, Y; public Point(int x, int y) { @@ -196,7 +200,7 @@ class Service: IService public Service(###CollectionType###> deps) { Console.WriteLine("Service creating"); - } + } } static class Setup @@ -223,7 +227,7 @@ public static void Main() { var composition = new Composition(); var service = composition.Service; - } + } catch(Exception ex) { Console.WriteLine(ex.Message); @@ -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 @@ -293,7 +299,7 @@ class Service: IService public Service(###CollectionType### deps) { Console.WriteLine("Service creating"); - } + } } static class Setup @@ -314,7 +320,7 @@ public static void Main() { var composition = new Composition(); var service = composition.Service; - } + } catch(Exception ex) { Console.WriteLine(ex.Message); @@ -350,7 +356,7 @@ namespace Sample { struct Point { - int X, Y; + int X, Y; public Point(int x, int y) { @@ -386,7 +392,7 @@ public Service(###CollectionType### deps) } Console.WriteLine("Service creating"); - } + } } static class Setup @@ -413,7 +419,7 @@ public static void Main() { var composition = new Composition(); var service = composition.Service; - } + } catch(Exception ex) { Console.WriteLine(ex.Message);