-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(EntityFrameworkCore): add extensions for entity configurations f…
…rom assemblies - Added a new file `BuilderExtensions.cs` which includes two extension methods: `WithEntityConfigurationsFromAssembliesExtension` and `ApplyConfigurationsFromAssembliesExtension`. - These methods allow registering and applying `IEntityTypeConfiguration<TEntity>` implementations from provided assemblies. - Created another new file `EntityConfigurationsFromAssembliesExtension.cs` that defines a custom DbContextOptionsExtension to load entity configurations from assemblies. - This is useful when the DbContext is in a separate assembly from the entity configurations and cannot reference the entity configurations assemblies directly. - Refactored method `AddOutboxBehavior` in `OutboxExtensions.cs`, simplifying its implementation.
- Loading branch information
1 parent
abf1e2a
commit c334452
Showing
16 changed files
with
202 additions
and
67 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
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
18 changes: 18 additions & 0 deletions
18
playground/Playground.Microservice.Api.Host/Testing/MassTransitOutboxRelay.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,18 @@ | ||
using ES.FX.TransactionalOutbox.EntityFrameworkCore.Delivery; | ||
using MassTransit; | ||
|
||
namespace Playground.Microservice.Api.Host.Testing; | ||
|
||
public class MassTransitOutboxRelay(IBusControl busControl, IPublishEndpoint publishEndpoint) : IOutboxMessageHandler | ||
{ | ||
public async ValueTask<bool> IsReadyAsync() => | ||
await busControl.WaitForHealthStatus(BusHealthStatus.Healthy, TimeSpan.FromSeconds(5)).ConfigureAwait(false) == | ||
BusHealthStatus.Healthy; | ||
|
||
public async ValueTask<bool> HandleAsync(OutboxMessageHandlerContext context, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
await publishEndpoint.Publish(context.Message, cancellationToken); | ||
return true; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
playground/Playground.Microservice.Api.Host/Testing/MediatorConsumer.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,26 @@ | ||
using MassTransit; | ||
using MediatR; | ||
|
||
namespace Playground.Microservice.Api.Host.Testing; | ||
|
||
public class MediatorConsumer<TMessage>(IMediator mediator) : IConsumer<TMessage> where TMessage : class | ||
{ | ||
public async Task Consume(ConsumeContext<TMessage> context) | ||
{ | ||
await mediator.Publish(context.Message); | ||
} | ||
} | ||
|
||
public class MediatorConsumerDefinition<TMessage> : | ||
ConsumerDefinition<MediatorConsumer<TMessage>> where TMessage : class | ||
{ | ||
public MediatorConsumerDefinition() | ||
{ | ||
// override the default endpoint name, for whatever reason | ||
EndpointName = "ha-submit-order"; | ||
|
||
// limit the number of messages consumed concurrently | ||
// this applies to the consumer only, not the endpoint | ||
ConcurrentMessageLimit = 4; | ||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
playground/Playground.Microservice.Api.Host/Testing/OutboxMessageHandler.cs
This file was deleted.
Oops, something went wrong.
7 changes: 5 additions & 2 deletions
7
playground/Playground.Microservice.Api.Host/Testing/OutboxTestMessage.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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
using ES.FX.TransactionalOutbox.EntityFrameworkCore.Messages; | ||
using MassTransit; | ||
using MediatR; | ||
|
||
namespace Playground.Microservice.Api.Host.Testing; | ||
|
||
[OutboxMessageType("SomeTestMessage")] | ||
public record OutboxTestMessage(string SomeProp); | ||
[OutboxMessageType("OutboxTextMessage.v1")] | ||
[EntityName("OutboxTextMessage.v1")] | ||
public record OutboxTestMessage(string SomeProp) : INotification; |
12 changes: 12 additions & 0 deletions
12
playground/Playground.Microservice.Api.Host/Testing/OutboxTestMessageHandler.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,12 @@ | ||
using MediatR; | ||
|
||
namespace Playground.Microservice.Api.Host.Testing; | ||
|
||
public class OutboxTestMessageHandler() : INotificationHandler<OutboxTestMessage> | ||
{ | ||
public async Task Handle(OutboxTestMessage request, CancellationToken cancellationToken) | ||
{ | ||
await Task.CompletedTask; | ||
|
||
} | ||
} |
3 changes: 0 additions & 3 deletions
3
playground/Playground.Microservice.Api.Host/Testing/TestComplexRequest.cs
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
playground/Playground.Microservice.Api.Host/Testing/TestComplexRequestValidator.cs
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
playground/Playground.Microservice.Api.Host/Testing/TestRequest.cs
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
playground/Playground.Microservice.Api.Host/Testing/TestValidator.cs
This file was deleted.
Oops, something went wrong.
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
41 changes: 41 additions & 0 deletions
41
src/ES.FX.Microsoft.EntityFrameworkCore/Extensions/BuilderExtensions.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,41 @@ | ||
using System.Reflection; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
|
||
namespace ES.FX.Microsoft.EntityFrameworkCore.Extensions; | ||
|
||
public static class BuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Registers an <see cref="EntityConfigurationsFromAssembliesExtension" /> to the | ||
/// <see cref="DbContextOptionsBuilder" />. | ||
/// Provides a list of assemblies to scan for <see cref="IEntityTypeConfiguration{TEntity}" /> implementations. | ||
/// Requires the model builder to be configured with /> | ||
/// </summary> | ||
/// <param name="builder">The <see cref="DbContextOptionsBuilder" /></param> | ||
/// <param name="assemblies">List of <see cref="Assembly" /> to scan</param> | ||
public static void WithEntityConfigurationsFromAssembliesExtension(this DbContextOptionsBuilder builder, | ||
params Assembly[] assemblies) | ||
{ | ||
((IDbContextOptionsBuilderInfrastructure)builder).AddOrUpdateExtension( | ||
new EntityConfigurationsFromAssembliesExtension | ||
{ Assemblies = assemblies }); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Applies the <see cref="IEntityTypeConfiguration{TEntity}" /> implementations from the assemblies registered in the | ||
/// <see cref="EntityConfigurationsFromAssembliesExtension" /> to the <see cref="ModelBuilder" />. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="ModelBuilder" /></param> | ||
/// <param name="options"> | ||
/// The <see cref="DbContextOptions" /> to load the | ||
/// <see cref="EntityConfigurationsFromAssembliesExtension" />> from | ||
/// </param> | ||
public static void ApplyConfigurationsFromAssembliesExtension(this ModelBuilder builder, DbContextOptions options) | ||
{ | ||
var extension = options.FindExtension<EntityConfigurationsFromAssembliesExtension>(); | ||
if (extension is null) return; | ||
foreach (var assembly in extension.Assemblies) builder.ApplyConfigurationsFromAssembly(assembly); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...X.Microsoft.EntityFrameworkCore/Extensions/EntityConfigurationsFromAssembliesExtension.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,43 @@ | ||
using System.Reflection; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace ES.FX.Microsoft.EntityFrameworkCore.Extensions; | ||
|
||
/// <summary> | ||
/// Custom extension to load <see cref="IEntityTypeConfiguration{TEntity}" /> implementations from assemblies. | ||
/// This is useful when the DbContext is in a separate assembly from the entity configurations and cannot reference the | ||
/// entity configurations assemblies directly. | ||
/// </summary> | ||
public class EntityConfigurationsFromAssembliesExtension : IDbContextOptionsExtension | ||
{ | ||
public EntityConfigurationsFromAssembliesExtension() => Info = new ExtensionInfo(this); | ||
public required Assembly[] Assemblies { get; init; } | ||
|
||
public void ApplyServices(IServiceCollection services) | ||
{ | ||
//No services required | ||
} | ||
|
||
public void Validate(IDbContextOptions options) | ||
{ | ||
// No-op. No validation required | ||
} | ||
|
||
public DbContextOptionsExtensionInfo Info { get; } | ||
|
||
|
||
public sealed class ExtensionInfo(IDbContextOptionsExtension extension) | ||
: DbContextOptionsExtensionInfo(extension) | ||
{ | ||
public override bool IsDatabaseProvider => false; | ||
public override string LogFragment => string.Empty; | ||
public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other) => true; | ||
public override int GetServiceProviderHashCode() => 0; | ||
|
||
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo) | ||
{ | ||
} | ||
} | ||
} |
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