refactor: refactor loading invisible assemblies

This commit is contained in:
Meysam Hadeli 2025-05-11 18:19:22 +03:30
parent 774866e9ad
commit 5115e0daec
21 changed files with 113 additions and 66 deletions

View File

@ -0,0 +1,6 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Unit.Test")]
[assembly: InternalsVisibleTo("Integration.Test")]
[assembly: InternalsVisibleTo("EndToEnd.Test")]

View File

@ -17,12 +17,4 @@
<ProjectReference Include="..\..\..\..\building-blocks\BuildingBlocks.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Booking\Bookings\" />
<Folder Include="Booking\Data\" />
<Folder Include="Flight\Data\Migrations\" />
<Folder Include="Identity\Identities\" />
<Folder Include="Passenger\Passengers\" />
</ItemGroup>
</Project>

View File

@ -1,4 +1,5 @@
using System.Threading.RateLimiting;
using Booking;
using BuildingBlocks.Core;
using BuildingBlocks.Exception;
using BuildingBlocks.HealthCheck;
@ -11,7 +12,10 @@ using BuildingBlocks.PersistMessageProcessor;
using BuildingBlocks.ProblemDetails;
using BuildingBlocks.Web;
using Figgle;
using Flight;
using Identity;
using Microsoft.AspNetCore.Mvc;
using Passenger;
using Serilog;
namespace Api.Extensions;
@ -85,6 +89,19 @@ public static class SharedInfrastructureExtensions
builder.Services.AddEasyCaching(options => { options.UseInMemory(builder.Configuration, "mem"); });
builder.Services.AddProblemDetails();
builder.Services.AddScoped<IEventMapper>(sp =>
{
var mappers = new IEventMapper[] {
sp.GetRequiredService<FlightEventMapper>(),
sp.GetRequiredService<IdentityEventMapper>(),
sp.GetRequiredService<PassengerEventMapper>(),
sp.GetRequiredService<BookingEventMapper>(),
};
return new CompositeEventMapper(mappers);
});
return builder;
}

View File

@ -0,0 +1,6 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Unit.Test")]
[assembly: InternalsVisibleTo("Integration.Test")]
[assembly: InternalsVisibleTo("EndToEnd.Test")]

View File

@ -1,5 +1,4 @@
using Booking.Data;
using BuildingBlocks.Core;
using BuildingBlocks.EventStoreDB;
using BuildingBlocks.Mapster;
using BuildingBlocks.Mongo;
@ -14,7 +13,7 @@ public static class InfrastructureExtensions
{
public static WebApplicationBuilder AddBookingModules(this WebApplicationBuilder builder)
{
builder.Services.AddScoped<IEventMapper, BookingEventMapper>();
builder.Services.AddScoped<BookingEventMapper>();
builder.AddMinimalEndpoints(assemblies: typeof(BookingRoot).Assembly);
builder.Services.AddValidatorsFromAssembly(typeof(BookingRoot).Assembly);
builder.Services.AddCustomMapster(typeof(BookingRoot).Assembly);

View File

@ -0,0 +1,6 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Unit.Test")]
[assembly: InternalsVisibleTo("Integration.Test")]
[assembly: InternalsVisibleTo("EndToEnd.Test")]

View File

@ -1,4 +1,3 @@
using BuildingBlocks.Core;
using BuildingBlocks.EFCore;
using BuildingBlocks.Mapster;
using BuildingBlocks.Mongo;
@ -17,7 +16,7 @@ public static class InfrastructureExtensions
{
public static WebApplicationBuilder AddFlightModules(this WebApplicationBuilder builder)
{
builder.Services.AddScoped<IEventMapper, FlightEventMapper>();
builder.Services.AddScoped<FlightEventMapper>();
builder.AddMinimalEndpoints(assemblies: typeof(FlightRoot).Assembly);
builder.Services.AddValidatorsFromAssembly(typeof(FlightRoot).Assembly);
builder.Services.AddCustomMapster(typeof(FlightRoot).Assembly);

View File

@ -21,22 +21,6 @@
<Protobuf Include="GrpcServer\Protos\flight.proto" GrpcServices="Both" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Unit.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Integration.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>EndToEnd.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\building-blocks\BuildingBlocks.csproj" />
</ItemGroup>

View File

@ -0,0 +1,6 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Unit.Test")]
[assembly: InternalsVisibleTo("Integration.Test")]
[assembly: InternalsVisibleTo("EndToEnd.Test")]

View File

@ -1,9 +1,7 @@
using BuildingBlocks.Core;
using BuildingBlocks.EFCore;
using BuildingBlocks.Mapster;
using BuildingBlocks.Web;
using FluentValidation;
using Identity.Configurations;
using Identity.Data;
using Identity.Data.Seed;
using Microsoft.AspNetCore.Builder;
@ -17,7 +15,7 @@ public static class InfrastructureExtensions
{
public static WebApplicationBuilder AddIdentityModules(this WebApplicationBuilder builder)
{
builder.Services.AddScoped<IEventMapper, IdentityEventMapper>();
builder.Services.AddScoped<IdentityEventMapper>();
builder.AddMinimalEndpoints(assemblies: typeof(IdentityRoot).Assembly);
builder.Services.AddValidatorsFromAssembly(typeof(IdentityRoot).Assembly);
builder.Services.AddCustomMapster(typeof(IdentityRoot).Assembly);

View File

@ -0,0 +1,6 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Unit.Test")]
[assembly: InternalsVisibleTo("Integration.Test")]
[assembly: InternalsVisibleTo("EndToEnd.Test")]

View File

@ -1,4 +1,3 @@
using BuildingBlocks.Core;
using BuildingBlocks.EFCore;
using BuildingBlocks.Mapster;
using BuildingBlocks.Mongo;
@ -15,7 +14,7 @@ public static class InfrastructureExtensions
{
public static WebApplicationBuilder AddPassengerModules(this WebApplicationBuilder builder)
{
builder.Services.AddScoped<IEventMapper, PassengerEventMapper>();
builder.Services.AddScoped<PassengerEventMapper>();
builder.AddMinimalEndpoints(assemblies: typeof(PassengerRoot).Assembly);
builder.Services.AddValidatorsFromAssembly(typeof(PassengerRoot).Assembly);
builder.Services.AddCustomMapster(typeof(PassengerRoot).Assembly);

View File

@ -0,0 +1,6 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Unit.Test")]
[assembly: InternalsVisibleTo("Integration.Test")]
[assembly: InternalsVisibleTo("EndToEnd.Test")]

View File

@ -13,12 +13,6 @@
<Folder Include="GrpcClient\Protos" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Integration.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\building-blocks\BuildingBlocks.csproj" />
</ItemGroup>

View File

@ -0,0 +1,6 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Unit.Test")]
[assembly: InternalsVisibleTo("Integration.Test")]
[assembly: InternalsVisibleTo("EndToEnd.Test")]

View File

@ -21,23 +21,8 @@
<Protobuf Include="GrpcServer\Protos\flight.proto" GrpcServices="Both" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Unit.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Integration.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>EndToEnd.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\building-blocks\BuildingBlocks.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,6 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Unit.Test")]
[assembly: InternalsVisibleTo("Integration.Test")]
[assembly: InternalsVisibleTo("EndToEnd.Test")]

View File

@ -11,12 +11,6 @@
<Folder Include="Data\Migrations" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Integration.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\building-blocks\BuildingBlocks.csproj" />
</ItemGroup>

View File

@ -0,0 +1,6 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Unit.Test")]
[assembly: InternalsVisibleTo("Integration.Test")]
[assembly: InternalsVisibleTo("EndToEnd.Test")]

View File

@ -16,11 +16,6 @@
<Protobuf Include="GrpcServer\Protos\passenger.proto" GrpcServices="Both" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Integration.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\building-blocks\BuildingBlocks.csproj" />

View File

@ -0,0 +1,37 @@
using BuildingBlocks.Core.Event;
namespace BuildingBlocks.Core;
public class CompositeEventMapper : IEventMapper
{
private readonly IEnumerable<IEventMapper> _mappers;
public CompositeEventMapper(IEnumerable<IEventMapper> mappers)
{
_mappers = mappers;
}
public IIntegrationEvent? MapToIntegrationEvent(IDomainEvent @event)
{
foreach (var mapper in _mappers)
{
var integrationEvent = mapper.MapToIntegrationEvent(@event);
if (integrationEvent is not null)
return integrationEvent;
}
return null;
}
public IInternalCommand? MapToInternalCommand(IDomainEvent @event)
{
foreach (var mapper in _mappers)
{
var internalCommand = mapper.MapToInternalCommand(@event);
if (internalCommand is not null)
return internalCommand;
}
return null;
}
}