Beispiel #1
0
 public MediatorTest(
     ServiceProvider serviceProvider,
     IPublishStrategyProvider defaultPublishStrategy = null
     )
     : base(serviceProvider, defaultPublishStrategy)
 {
 }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Mediator"/> class.
        /// </summary>
        /// <param name="serviceProvider">A mechanism for retrieving a service object.</param>
        /// <param name="defaultPublishStrategy">
        /// The default way <see cref="IMediator"/> uses the list of
        /// <see cref="NotificationHandler{TNotification}"/>. If not specified the value will be
        /// <see cref="PublishStrategyProvider.ParallelWhenAll" />.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Thrown when <paramref name="serviceProvider"/> is null.
        /// </exception>
        public Mediator(
            ServiceProvider serviceProvider,
            IPublishStrategyProvider defaultPublishStrategy = null)
        {
            ServiceProvider = Guard.ThrowIfNull(serviceProvider, nameof(serviceProvider));

            DefaultPublishStrategy = defaultPublishStrategy ?? PublishStrategyProvider.ParallelWhenAll;
        }
Beispiel #3
0
        /// <summary>
        /// Asynchronously sends a <typeparamref name="TNotification"/> to multiple handlers.
        /// </summary>
        /// <typeparam name="TNotification">The type of a notification.</typeparam>
        /// <param name="notification">The notification sent to <see cref="IMediator"/>.</param>
        /// <param name="publishStrategy">
        /// The way <see cref="IMediator"/> uses the list of
        /// <see cref="NotificationHandler{TNotification}"/>.
        /// </param>
        /// <param name="cancellationToken">
        /// A cancellation token that should be used to cancel the work.
        /// </param>
        /// <returns>A task that represents the publish operation.</returns>
        public Task PublishAsync <TNotification>(
            TNotification notification,
            IPublishStrategyProvider publishStrategy,
            CancellationToken cancellationToken = default
            )
            where TNotification : INotification
        {
            Guard.ThrowIfNull(notification, nameof(notification));
            Guard.ThrowIfNull(publishStrategy, nameof(publishStrategy));

            cancellationToken.ThrowIfCancellationRequested();

            var handler = (NotificationHandlerWrapper <TNotification>)NotificationHandlers.GetOrAdd(
                typeof(TNotification),
                type => new NotificationHandlerWrapper <TNotification>()
                );

            return(handler.HandleAsync(
                       notification,
                       publishStrategy.Resolve <TNotification>(),
                       ServiceProvider,
                       cancellationToken
                       ));
        }