-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First go at defining pluggable modules
- Loading branch information
1 parent
b4e32fc
commit a4b9563
Showing
8 changed files
with
253 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Reflection; | ||
using Infrastructure.WebApi.Common; | ||
|
||
namespace ApiHost1; | ||
|
||
public class Module : ISubDomainModule | ||
{ | ||
public Action<WebApplication> MinimalApiRegistrationFunction | ||
{ | ||
get { return app => app.RegisterRoutes(); } | ||
} | ||
|
||
public Action<ConfigurationManager, IServiceCollection> RegisterServicesFunction | ||
{ | ||
get { return (configuration, services) => { }; } | ||
} | ||
|
||
public Assembly ApiAssembly => typeof(Program).Assembly; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Reflection; | ||
using Infrastructure.WebApi.Common; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace CarsApi; | ||
|
||
public class Module : ISubDomainModule | ||
{ | ||
public Action<WebApplication> MinimalApiRegistrationFunction | ||
{ | ||
get { return app => app.RegisterRoutes(); } | ||
} | ||
|
||
public Action<ConfigurationManager, IServiceCollection> RegisterServicesFunction | ||
{ | ||
get { return (configuration, services) => { }; } | ||
} | ||
|
||
public Assembly ApiAssembly => typeof(CarsApi).Assembly; | ||
} |
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 @@ | ||
global using Xunit; |
17 changes: 17 additions & 0 deletions
17
src/Infrastructure.WebApi.Common.UnitTests/Infrastructure.WebApi.Common.UnitTests.csproj
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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Common.UnitTests\Common.UnitTests.csproj" /> | ||
<ProjectReference Include="..\Infrastructure.WebApi.Common\Infrastructure.WebApi.Common.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
114 changes: 114 additions & 0 deletions
114
src/Infrastructure.WebApi.Common.UnitTests/SubDomainModulesSpec.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,114 @@ | ||
using System.Reflection; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Infrastructure.WebApi.Common.UnitTests; | ||
|
||
[Trait("Category", "Unit")] | ||
public class SubDomainModulesSpec | ||
{ | ||
private readonly SubDomainModules _modules; | ||
|
||
public SubDomainModulesSpec() | ||
{ | ||
_modules = new SubDomainModules(); | ||
} | ||
|
||
[Fact] | ||
public void WhenRegisterAndNullModule_ThenThrows() | ||
{ | ||
_modules | ||
.Invoking(x => x.Register(null!)) | ||
.Should().Throw<ArgumentNullException>(); | ||
} | ||
|
||
[Fact] | ||
public void WhenRegisterAndNullApiAssembly_ThenThrows() | ||
{ | ||
_modules | ||
.Invoking(x => x.Register(new TestModule { ApiAssembly = null! })) | ||
.Should().Throw<ArgumentNullException>(); | ||
} | ||
|
||
[Fact] | ||
public void WhenRegisterAndNullMinimalApiRegistrationFunction_ThenThrows() | ||
{ | ||
_modules | ||
.Invoking(x => x.Register(new TestModule | ||
{ ApiAssembly = typeof(SubDomainModulesSpec).Assembly, RegisterServicesFunction = (_, _) => { } })) | ||
.Should().Throw<ArgumentNullException>(); | ||
} | ||
|
||
[Fact] | ||
public void WhenRegisterAndNullRegisterServicesFunction_ThenRegisters() | ||
{ | ||
_modules.Register(new TestModule | ||
{ | ||
ApiAssembly = typeof(SubDomainModulesSpec).Assembly, MinimalApiRegistrationFunction = _ => { }, | ||
RegisterServicesFunction = null! | ||
}); | ||
|
||
_modules.HandlerAssemblies.Should().ContainSingle(); | ||
} | ||
|
||
[Fact] | ||
public void WhenRegisterServicesAndNoModules_ThenAppliedToAllModules() | ||
{ | ||
var configuration = new ConfigurationManager(); | ||
var services = new ServiceCollection(); | ||
|
||
_modules.RegisterServices(configuration, services); | ||
} | ||
|
||
[Fact] | ||
public void WhenRegisterServices_ThenAppliedToAllModules() | ||
{ | ||
var configuration = new ConfigurationManager(); | ||
var services = new ServiceCollection(); | ||
var wasCalled = false; | ||
_modules.Register(new TestModule | ||
{ | ||
ApiAssembly = typeof(SubDomainModulesSpec).Assembly, | ||
MinimalApiRegistrationFunction = _ => { }, | ||
RegisterServicesFunction = (_, _) => { wasCalled = true; } | ||
}); | ||
|
||
_modules.RegisterServices(configuration, services); | ||
|
||
wasCalled.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void WhenConfigureHostAndNoModules_ThenAppliedToAllModules() | ||
{ | ||
var app = WebApplication.Create(); | ||
|
||
_modules.ConfigureHost(app); | ||
} | ||
|
||
[Fact] | ||
public void WhenConfigureHost_ThenAppliedToAllModules() | ||
{ | ||
var app = WebApplication.Create(); | ||
var wasCalled = false; | ||
_modules.Register(new TestModule | ||
{ | ||
ApiAssembly = typeof(SubDomainModulesSpec).Assembly, | ||
MinimalApiRegistrationFunction = _ => { wasCalled = true; }, | ||
RegisterServicesFunction = (_, _) => { } | ||
}); | ||
|
||
_modules.ConfigureHost(app); | ||
|
||
wasCalled.Should().BeTrue(); | ||
} | ||
} | ||
|
||
public class TestModule : ISubDomainModule | ||
{ | ||
public Assembly? ApiAssembly { get; init; } | ||
public Action<WebApplication>? MinimalApiRegistrationFunction { get; init; } | ||
public Action<ConfigurationManager, IServiceCollection>? RegisterServicesFunction { get; init; } | ||
} |
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,55 @@ | ||
using System.Reflection; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Infrastructure.WebApi.Common; | ||
|
||
public class SubDomainModules | ||
{ | ||
private readonly List<Assembly> _handlerAssemblies; | ||
private readonly List<Action<WebApplication>> _minimalApiRegistrationFunctions; | ||
private readonly List<Action<ConfigurationManager, IServiceCollection>> _serviceCollectionFunctions; | ||
|
||
public SubDomainModules() | ||
{ | ||
_handlerAssemblies = new List<Assembly>(); | ||
_minimalApiRegistrationFunctions = new List<Action<WebApplication>>(); | ||
_serviceCollectionFunctions = new List<Action<ConfigurationManager, IServiceCollection>>(); | ||
} | ||
|
||
public IReadOnlyList<Assembly> HandlerAssemblies => _handlerAssemblies; | ||
|
||
public void Register(ISubDomainModule module) | ||
{ | ||
ArgumentNullException.ThrowIfNull(module); | ||
ArgumentNullException.ThrowIfNull(module.ApiAssembly, nameof(module.ApiAssembly)); | ||
ArgumentNullException.ThrowIfNull(module.MinimalApiRegistrationFunction, | ||
nameof(module.MinimalApiRegistrationFunction)); | ||
|
||
_handlerAssemblies.Add(module.ApiAssembly); | ||
_minimalApiRegistrationFunctions.Add(module.MinimalApiRegistrationFunction); | ||
if (module.RegisterServicesFunction is not null) | ||
{ | ||
_serviceCollectionFunctions.Add(module.RegisterServicesFunction); | ||
} | ||
} | ||
|
||
public void RegisterServices(ConfigurationManager configuration, IServiceCollection serviceCollection) | ||
{ | ||
_serviceCollectionFunctions.ForEach(func => func(configuration, serviceCollection)); | ||
} | ||
|
||
public void ConfigureHost(WebApplication app) | ||
{ | ||
_minimalApiRegistrationFunctions.ForEach(func => func(app)); | ||
} | ||
} | ||
|
||
public interface ISubDomainModule | ||
{ | ||
public Assembly ApiAssembly { get; } | ||
|
||
public Action<WebApplication> MinimalApiRegistrationFunction { get; } | ||
public Action<ConfigurationManager, IServiceCollection>? RegisterServicesFunction { get; } | ||
} |
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