| | | 1 | | using NexMediator.Abstractions.Interfaces; |
| | | 2 | | using NexMediator.Core.Internal; |
| | | 3 | | |
| | | 4 | | namespace NexMediator.Core; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Default implementation of INexMediator. |
| | | 8 | | /// Routes requests, notifications, and streams through a cached pipeline delegate. |
| | | 9 | | /// </summary> |
| | | 10 | | public class DefaultNexMediator : INexMediator |
| | | 11 | | { |
| | | 12 | | private readonly IServiceProvider _provider; |
| | | 13 | | private readonly NexMediatorOptions _options; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Initializes a new DefaultNexMediator instance. |
| | | 17 | | /// Validates pipeline options at startup and throws if invalid. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="provider"> |
| | | 20 | | /// Service provider for resolving handlers, behaviors, and scoped services. |
| | | 21 | | /// </param> |
| | | 22 | | /// <param name="options"> |
| | | 23 | | /// Configuration options for the mediator pipeline. |
| | | 24 | | /// </param> |
| | 19 | 25 | | public DefaultNexMediator(IServiceProvider provider, NexMediatorOptions options) |
| | 19 | 26 | | { |
| | 19 | 27 | | _provider = provider ?? throw new ArgumentNullException(nameof(provider)); |
| | 18 | 28 | | _options = options ?? throw new ArgumentNullException(nameof(options)); |
| | | 29 | | |
| | 17 | 30 | | var warnings = _options.Validate(); |
| | 17 | 31 | | if (warnings.Count > 0) |
| | 0 | 32 | | throw new InvalidOperationException($"Pipeline configuration validation failed: {string.Join("; ", warnings) |
| | 17 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Sends a request through the compiled, cached pipeline and returns its response. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <typeparam name="TResponse">Type of the response.</typeparam> |
| | | 39 | | /// <param name="request">The request to send.</param> |
| | | 40 | | /// <param name="cancellationToken">Token to cancel the operation.</param> |
| | | 41 | | /// <returns>The response from the handler.</returns> |
| | | 42 | | public Task<TResponse> Send<TResponse>(INexRequest<TResponse> request, CancellationToken cancellationToken = default |
| | 9 | 43 | | { |
| | 9 | 44 | | if (request == null) |
| | 1 | 45 | | throw new ArgumentNullException(nameof(request)); |
| | | 46 | | |
| | 8 | 47 | | return NexRequestExecutor.Dispatch(request, _provider, cancellationToken); |
| | 8 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Publishes a notification through the pipeline without caching. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <typeparam name="TNotification">Type of the notification.</typeparam> |
| | | 54 | | /// <param name="notification">The notification to publish.</param> |
| | | 55 | | /// <param name="cancellationToken">Token to cancel the operation.</param> |
| | | 56 | | /// <returns>A task representing the publish operation.</returns> |
| | | 57 | | public Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default) |
| | | 58 | | where TNotification : INexNotification |
| | 3 | 59 | | { |
| | 3 | 60 | | if (notification == null) |
| | 1 | 61 | | throw new ArgumentNullException(nameof(notification)); |
| | | 62 | | |
| | 2 | 63 | | return NexNotificationExecutor.Publish(notification, _provider, cancellationToken); |
| | 2 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Streams responses for a stream request through the pipeline without caching. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <typeparam name="TResponse">Type of each streamed response.</typeparam> |
| | | 70 | | /// <param name="request">The stream request.</param> |
| | | 71 | | /// <param name="cancellationToken">Token to cancel the operation.</param> |
| | | 72 | | /// <returns>An async enumerable of responses.</returns> |
| | | 73 | | public IAsyncEnumerable<TResponse> Stream<TResponse>(INexStreamRequest<TResponse> request, CancellationToken cancell |
| | 4 | 74 | | { |
| | 4 | 75 | | if (request == null) |
| | 1 | 76 | | throw new ArgumentNullException(nameof(request)); |
| | | 77 | | |
| | 3 | 78 | | return NexStreamExecutor.Execute(request, _provider, cancellationToken); |
| | 2 | 79 | | } |
| | | 80 | | } |