예제 #1
0
        public void GetOrAdd_TryAddOneHundredRecords_AddsOnlyOneRecord()
        {
            var store = new ConcurrentStore <int, object>();

            Parallel.ForEach(Enumerable.Repeat(1, 100), i =>
            {
                store.GetOrAdd(i, x => new object());
            });

            store.Count.Should().Be(1);
        }
예제 #2
0
        /// <summary>
        /// Asynchronously sends a request to the single handler.
        /// </summary>
        /// <typeparam name="TResult">
        /// The type of the return value of a <see cref="IQuery{TResult}"/>.
        /// </typeparam>
        /// <param name="request">The request to <see cref="IMediator"/>.</param>
        /// <param name="cancellationToken">
        /// A cancellation token that should be used to cancel the work.
        /// </param>
        /// <returns>A task that represents the send operation.</returns>
        public Task <TResult> SendAsync <TResult>(
            IQuery <TResult> request,
            CancellationToken cancellationToken = default)
        {
            Guard.ThrowIfNull(request, nameof(request));

            cancellationToken.ThrowIfCancellationRequested();

            var handler = (RequestHandlerWrapper <TResult>)RequestHandlers.GetOrAdd(
                request.GetType(),
                type => Activator.CreateInstance(
                    typeof(RequestHandlerWrapper <,>).MakeGenericType(type, typeof(TResult))
                    )
                );

            return(handler.HandleAsync(request, ServiceProvider, cancellationToken));
        }
예제 #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
                       ));
        }