From 34ae0f1d9ba938d62a707fe5b05da7f2dec0a6c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20LB?= Date: Mon, 3 Jun 2024 20:24:00 +0200 Subject: [PATCH] ShouldRegisterServiceDescriptor -> FAIL --- .../Assemblies/AssemblyServiceTests.cs | 3 +- ...ceTests.Logic.RegisterServiceDescriptor.cs | 54 ++++++++ .../ServiceCollectionServiceTests.cs | 121 ++++++++++++++++++ ...ndencyInjectionBroker.ServiceCollection.cs | 4 +- ...pendencyInjectionBroker.ServiceProvider.cs | 4 +- .../DependencyInjectionBroker.cs | 4 +- ...ndencyInjectionBroker.ServiceCollection.cs | 4 +- ...pendencyInjectionBroker.ServiceProvider.cs | 2 +- .../IServiceCollectionService.cs | 17 +++ .../ServiceCollectionService.cs | 28 ++++ 10 files changed, 231 insertions(+), 10 deletions(-) create mode 100644 STX.SPAL.Core.Tests.Unit/Services/Foundations/ServicesCollections/ServiceCollectionServiceTests.Logic.RegisterServiceDescriptor.cs create mode 100644 STX.SPAL.Core.Tests.Unit/Services/Foundations/ServicesCollections/ServiceCollectionServiceTests.cs create mode 100644 STX.SPAL.Core/Models/Services/Foundations/ServicesCollections/IServiceCollectionService.cs create mode 100644 STX.SPAL.Core/Models/Services/Foundations/ServicesCollections/ServiceCollectionService.cs diff --git a/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.cs b/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.cs index 858aa30..6a06102 100644 --- a/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.cs +++ b/STX.SPAL.Core.Tests.Unit/Services/Foundations/Assemblies/AssemblyServiceTests.cs @@ -52,8 +52,9 @@ private static string[] CreateRandomPathArray() private static Assembly CreateRandomAssembly() { string randomAssemblyName = GetRandomString(); + var assemblyName = - new AssemblyName("randomAssemblyName"); + new AssemblyName(randomAssemblyName); AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( diff --git a/STX.SPAL.Core.Tests.Unit/Services/Foundations/ServicesCollections/ServiceCollectionServiceTests.Logic.RegisterServiceDescriptor.cs b/STX.SPAL.Core.Tests.Unit/Services/Foundations/ServicesCollections/ServiceCollectionServiceTests.Logic.RegisterServiceDescriptor.cs new file mode 100644 index 0000000..dce99ef --- /dev/null +++ b/STX.SPAL.Core.Tests.Unit/Services/Foundations/ServicesCollections/ServiceCollectionServiceTests.Logic.RegisterServiceDescriptor.cs @@ -0,0 +1,54 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using FluentAssertions; +using Microsoft.Extensions.DependencyInjection; +using Moq; + +namespace STX.SPAL.Core.Tests.Unit.Services.Foundations.ServicesCollections +{ + public partial class ServiceCollectionServiceTests + { + [Fact] + private void ShouldRegisterServiceDescriptor() + { + // given + dynamic randomProperties = CreateRandomProperties(); + dynamic inputProperties = randomProperties; + + ServiceDescriptor randomServiceDescriptor = randomProperties.ServiceDescriptor; + ServiceDescriptor inputServiceDescriptor = randomServiceDescriptor; + + IServiceCollection randomServiceCollection = randomProperties.ServiceCollection; + IServiceCollection expectedServiceCollection = randomServiceCollection; + IServiceCollection returnedServiceCollection = randomServiceCollection; + + this.dependencyInjectionBroker + .Setup(broker => + broker.RegisterServiceDescriptor( + It.Is(actualServiceDescriptor => + actualServiceDescriptor == inputServiceDescriptor))) + .Returns(returnedServiceCollection); + + // when + IServiceCollection actualServiceCollection = + this.serviceCollectionService.RegisterServiceDescriptor( + inputProperties.SpalInterfaceType, + inputProperties.ImplementationType, + inputProperties.ServiceLifeTime); + + //then + actualServiceCollection.Should().BeSameAs(expectedServiceCollection); + + this.dependencyInjectionBroker.Verify( + broker => + broker.RegisterServiceDescriptor( + It.Is(actualServiceDescriptor => + actualServiceDescriptor == inputServiceDescriptor)), + Times.Once); + + this.dependencyInjectionBroker.VerifyNoOtherCalls(); + } + } +} diff --git a/STX.SPAL.Core.Tests.Unit/Services/Foundations/ServicesCollections/ServiceCollectionServiceTests.cs b/STX.SPAL.Core.Tests.Unit/Services/Foundations/ServicesCollections/ServiceCollectionServiceTests.cs new file mode 100644 index 0000000..4fa097f --- /dev/null +++ b/STX.SPAL.Core.Tests.Unit/Services/Foundations/ServicesCollections/ServiceCollectionServiceTests.cs @@ -0,0 +1,121 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System; +using System.Reflection; +using System.Reflection.Emit; +using Microsoft.Extensions.DependencyInjection; +using Moq; +using STX.SPAL.Abstractions; +using STX.SPAL.Core.Brokers.DependenciesInjection; +using STX.SPAL.Core.Models.Services.Foundations.ServicesCollections; +using Tynamix.ObjectFiller; + +namespace STX.SPAL.Core.Tests.Unit.Services.Foundations.ServicesCollections +{ + public partial class ServiceCollectionServiceTests + { + private readonly Mock dependencyInjectionBroker; + private readonly IServiceCollectionService serviceCollectionService; + + public ServiceCollectionServiceTests() + { + this.dependencyInjectionBroker = new Mock(); + + this.serviceCollectionService = + new ServiceCollectionService(dependencyInjectionBroker.Object); + } + + private static string GetRandomString() => + new MnemonicString().GetValue(); + + private static AssemblyBuilder CreateRandomAssembly() + { + string randomAssemblyName = GetRandomString(); + + var assemblyName = + new AssemblyName(randomAssemblyName); + + AssemblyBuilder assemblyBuilder = + AssemblyBuilder.DefineDynamicAssembly( + assemblyName, + AssemblyBuilderAccess.Run); + + return assemblyBuilder; + } + + private static Type CreateRandomSpalInterfaceType() + { + AssemblyBuilder spalAssembly = CreateRandomAssembly(); + string assemblyName = spalAssembly.GetName().Name; + ModuleBuilder moduleBuilder = spalAssembly.DefineDynamicModule(assemblyName); + + TypeBuilder typeBuilder = moduleBuilder.DefineType( + name: GetRandomString(), + attr: TypeAttributes.Public, + parent: null, + interfaces: new Type[] + { + typeof(ISPALBase) + }); + + return typeBuilder; + } + + private static Type CreateRandomImplementationType() + { + AssemblyBuilder spalAssembly = CreateRandomAssembly(); + string assemblyName = spalAssembly.GetName().Name; + ModuleBuilder moduleBuilder = spalAssembly.DefineDynamicModule(assemblyName); + + TypeBuilder typeBuilder = moduleBuilder.DefineType( + name: GetRandomString(), + attr: TypeAttributes.Public, + parent: null, + interfaces: new Type[] + { + typeof(ISPALBase) + }); + + return typeBuilder; + } + + private static ServiceLifetime CreateRandomServiceLifeTime() + { + int randomNumber = new IntRange(min: 0, max: 2).GetValue(); + + return randomNumber switch + { + 0 => ServiceLifetime.Singleton, + 1 => ServiceLifetime.Scoped, + _ => ServiceLifetime.Transient + }; + } + + private static dynamic CreateRandomProperties() + { + Type randomSpalInterfaceType = CreateRandomSpalInterfaceType(); + string randomSpalId = GetRandomString(); + Type randomImplementationType = CreateRandomImplementationType(); + ServiceLifetime randomServiceLifeTime = CreateRandomServiceLifeTime(); + + return new + { + SpalInterfaceType = randomSpalInterfaceType, + SpalId = randomSpalId, + ImplementationType = randomImplementationType, + ServiceLifeTime = randomServiceLifeTime, + + ServiceDescriptor = + new ServiceDescriptor( + randomSpalInterfaceType, + randomSpalId, + randomImplementationType, + randomServiceLifeTime), + + ServiceCollection = new ServiceCollection() + }; + } + } +} diff --git a/STX.SPAL.Core/Brokers/DependenciesInjection/DependencyInjectionBroker.ServiceCollection.cs b/STX.SPAL.Core/Brokers/DependenciesInjection/DependencyInjectionBroker.ServiceCollection.cs index 590f83f..44bb154 100644 --- a/STX.SPAL.Core/Brokers/DependenciesInjection/DependencyInjectionBroker.ServiceCollection.cs +++ b/STX.SPAL.Core/Brokers/DependenciesInjection/DependencyInjectionBroker.ServiceCollection.cs @@ -9,7 +9,7 @@ namespace STX.SPAL.Core.Brokers.DependenciesInjection { internal partial class DependencyInjectionBroker { - public void RegisterServiceDescriptor(ServiceDescriptor serviceDescriptor) => - serviceColllection.Add(serviceDescriptor); + public IServiceCollection RegisterServiceDescriptor(ServiceDescriptor serviceDescriptor) => + serviceCollection.Add(serviceDescriptor); } } diff --git a/STX.SPAL.Core/Brokers/DependenciesInjection/DependencyInjectionBroker.ServiceProvider.cs b/STX.SPAL.Core/Brokers/DependenciesInjection/DependencyInjectionBroker.ServiceProvider.cs index c529e14..f19c5b8 100644 --- a/STX.SPAL.Core/Brokers/DependenciesInjection/DependencyInjectionBroker.ServiceProvider.cs +++ b/STX.SPAL.Core/Brokers/DependenciesInjection/DependencyInjectionBroker.ServiceProvider.cs @@ -10,12 +10,12 @@ namespace STX.SPAL.Core.Brokers.DependenciesInjection internal partial class DependencyInjectionBroker { public IServiceProvider BuildServiceProvider() => - serviceColllection.BuildServiceProvider(); + serviceCollection.BuildServiceProvider(); public T ResolveImplementation(ServiceProvider serviceProvider) => serviceProvider.GetRequiredService(); - T ResolveImplementation(ServiceProvider serviceProvider, string spalId) => + public T ResolveImplementation(ServiceProvider serviceProvider, string spalId) => serviceProvider.GetRequiredKeyedService(spalId); } } diff --git a/STX.SPAL.Core/Brokers/DependenciesInjection/DependencyInjectionBroker.cs b/STX.SPAL.Core/Brokers/DependenciesInjection/DependencyInjectionBroker.cs index 3c712f5..bc32e6a 100644 --- a/STX.SPAL.Core/Brokers/DependenciesInjection/DependencyInjectionBroker.cs +++ b/STX.SPAL.Core/Brokers/DependenciesInjection/DependencyInjectionBroker.cs @@ -8,11 +8,11 @@ namespace STX.SPAL.Core.Brokers.DependenciesInjection { internal partial class DependencyInjectionBroker : IDependencyInjectionBroker { - private readonly ServiceCollection serviceColllection; + private readonly ServiceCollection serviceCollection; public DependencyInjectionBroker() { - serviceColllection = new ServiceCollection(); + serviceCollection = new ServiceCollection(); } } } diff --git a/STX.SPAL.Core/Brokers/DependenciesInjection/IDependencyInjectionBroker.ServiceCollection.cs b/STX.SPAL.Core/Brokers/DependenciesInjection/IDependencyInjectionBroker.ServiceCollection.cs index 47cc90d..8a7667c 100644 --- a/STX.SPAL.Core/Brokers/DependenciesInjection/IDependencyInjectionBroker.ServiceCollection.cs +++ b/STX.SPAL.Core/Brokers/DependenciesInjection/IDependencyInjectionBroker.ServiceCollection.cs @@ -6,8 +6,8 @@ namespace STX.SPAL.Core.Brokers.DependenciesInjection { - internal partial interface IDependencyInjection + internal partial interface IDependencyInjectionBroker { - void RegisterServiceDescriptor(ServiceDescriptor serviceDescriptor); + IServiceCollection RegisterServiceDescriptor(ServiceDescriptor serviceDescriptor); } } diff --git a/STX.SPAL.Core/Brokers/DependenciesInjection/IDependencyInjectionBroker.ServiceProvider.cs b/STX.SPAL.Core/Brokers/DependenciesInjection/IDependencyInjectionBroker.ServiceProvider.cs index d60d1d1..dc3c654 100644 --- a/STX.SPAL.Core/Brokers/DependenciesInjection/IDependencyInjectionBroker.ServiceProvider.cs +++ b/STX.SPAL.Core/Brokers/DependenciesInjection/IDependencyInjectionBroker.ServiceProvider.cs @@ -7,7 +7,7 @@ namespace STX.SPAL.Core.Brokers.DependenciesInjection { - internal partial interface IDependencyInjection + internal partial interface IDependencyInjectionBroker { IServiceProvider BuildServiceProvider(); T ResolveImplementation(ServiceProvider serviceProvider); diff --git a/STX.SPAL.Core/Models/Services/Foundations/ServicesCollections/IServiceCollectionService.cs b/STX.SPAL.Core/Models/Services/Foundations/ServicesCollections/IServiceCollectionService.cs new file mode 100644 index 0000000..6451ff5 --- /dev/null +++ b/STX.SPAL.Core/Models/Services/Foundations/ServicesCollections/IServiceCollectionService.cs @@ -0,0 +1,17 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System; +using Microsoft.Extensions.DependencyInjection; + +namespace STX.SPAL.Core.Models.Services.Foundations.ServicesCollections +{ + internal partial interface IServiceCollectionService + { + IServiceCollection RegisterServiceDescriptor( + Type spalInterfaceType, + Type implementationType, + ServiceLifetime serviceLifetime); + } +} diff --git a/STX.SPAL.Core/Models/Services/Foundations/ServicesCollections/ServiceCollectionService.cs b/STX.SPAL.Core/Models/Services/Foundations/ServicesCollections/ServiceCollectionService.cs new file mode 100644 index 0000000..44898c4 --- /dev/null +++ b/STX.SPAL.Core/Models/Services/Foundations/ServicesCollections/ServiceCollectionService.cs @@ -0,0 +1,28 @@ +// ---------------------------------------------------------------------------------- +// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers +// ---------------------------------------------------------------------------------- + +using System; +using Microsoft.Extensions.DependencyInjection; +using STX.SPAL.Core.Brokers.DependenciesInjection; + +namespace STX.SPAL.Core.Models.Services.Foundations.ServicesCollections +{ + internal partial class ServiceCollectionService : IServiceCollectionService + { + private readonly IDependencyInjectionBroker dependencyInjectionBroker; + + public ServiceCollectionService(IDependencyInjectionBroker dependencyInjectionBroker) + { + this.dependencyInjectionBroker = dependencyInjectionBroker; + } + + public IServiceCollection RegisterServiceDescriptor( + Type spalInterfaceType, + Type implementationType, + ServiceLifetime serviceLifetime) + { + throw new System.NotImplementedException(); + } + } +}