<commit_before>﻿using MediatR.Internal;

namespace MediatR
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Concurrent;
    using System.Linq;
    using System.Reflection;
    using System.Threading;
    using System.Threading.Tasks;

    /// <summary>
    /// Default mediator implementation relying on single- and multi instance delegates for resolving handlers.
    /// </summary>
    public class Mediator : IMediator
    {
        private readonly SingleInstanceFactory _singleInstanceFactory;
        private readonly MultiInstanceFactory _multiInstanceFactory;

        private static readonly MethodInfo CreatePipelineMethod =
            typeof(Mediator).GetTypeInfo().DeclaredMethods.Single(m => m.Name == nameof(CreatePipeline));

        private static readonly ConcurrentDictionary<Type, Delegate> _requestHandlerFactories = new ConcurrentDictionary<Type, Delegate>();

        /// <summary>
        /// Initializes a new instance of the <see cref="Mediator"/> class.
        /// </summary>
        /// <param name="singleInstanceFactory">The single instance factory.</param>
        /// <param name="multiInstanceFactory">The multi instance factory.</param>
        public Mediator(SingleInstanceFactory singleInstanceFactory, MultiInstanceFactory multiInstanceFactory)
        {
            _singleInstanceFactory = singleInstanceFactory;
            _multiInstanceFactory = multiInstanceFactory;
        }

        public Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var handler = GetHandler(request, cancellationToken);

            var pipeline = GetPipeline(request, handler);

            return pipeline;
        }

        public Task SendAsync(IRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            var handler = GetHandler(request, cancellationToken);

            var pipeline = GetPipeline(request, handler);

            return pipeline;
        }

        private RequestHandlerDelegate<Unit> GetHandler(IRequest request, CancellationToken cancellationToken)
        {
            var requestType = request.GetType();

            var handler = (Func<IRequest, CancellationToken, SingleInstanceFactory, RequestHandlerDelegate<Unit>>)
                _requestHandlerFactories.GetOrAdd(requestType, GetHandlerFactory(requestType, GetHandler));

            if (handler == null)
            {
                throw BuildException(request);
            }

            return handler(request, cancellationToken, _singleInstanceFactory);
        }

        private RequestHandlerDelegate<TResponse> GetHandler<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken)
        {
            var requestType = request.GetType();

            var handler = (Func<IRequest<TResponse>, CancellationToken, SingleInstanceFactory, RequestHandlerDelegate<TResponse>>)
                _requestHandlerFactories.GetOrAdd(requestType, GetHandlerFactory<TResponse>(requestType, GetHandler));

            if (handler == null)
            {
                throw BuildException(request);
            }

            return handler(request, cancellationToken, _singleInstanceFactory);
        }

        private object GetHandler(Type requestType)
        {<commit_msg>naming<commit_after>﻿using MediatR.Internal;

namespace MediatR
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Concurrent;
    using System.Linq;
    using System.Reflection;
    using System.Threading;
    using System.Threading.Tasks;

    /// <summary>
    /// Default mediator implementation relying on single- and multi instance delegates for resolving handlers.
    /// </summary>
    public class Mediator : IMediator
    {
        private readonly SingleInstanceFactory _singleInstanceFactory;
        private readonly MultiInstanceFactory _multiInstanceFactory;

        private static readonly MethodInfo CreatePipelineMethod =
            typeof(Mediator).GetTypeInfo().DeclaredMethods.Single(m => m.Name == nameof(CreatePipeline));

        private static readonly ConcurrentDictionary<Type, Delegate> _requestHandlerFactories = new ConcurrentDictionary<Type, Delegate>();

        /// <summary>
        /// Initializes a new instance of the <see cref="Mediator"/> class.
        /// </summary>
        /// <param name="singleInstanceFactory">The single instance factory.</param>
        /// <param name="multiInstanceFactory">The multi instance factory.</param>
        public Mediator(SingleInstanceFactory singleInstanceFactory, MultiInstanceFactory multiInstanceFactory)
        {
            _singleInstanceFactory = singleInstanceFactory;
            _multiInstanceFactory = multiInstanceFactory;
        }

        public Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var handler = GetHandler(request, cancellationToken);

            var pipeline = GetPipeline(request, handler);

            return pipeline;
        }

        public Task SendAsync(IRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            var handler = GetHandler(request, cancellationToken);

            var pipeline = GetPipeline(request, handler);

            return pipeline;
        }

        private RequestHandlerDelegate<Unit> GetHandler(IRequest request, CancellationToken cancellationToken)
        {
            var requestType = request.GetType();

            var handlerFactory = (Func<IRequest, CancellationToken, SingleInstanceFactory, RequestHandlerDelegate<Unit>>)
                _requestHandlerFactories.GetOrAdd(requestType, GetHandlerFactory(requestType, GetHandler));

            if (handlerFactory == null)
            {
                throw BuildException(request);
            }

            return handlerFactory(request, cancellationToken, _singleInstanceFactory);
        }

        private RequestHandlerDelegate<TResponse> GetHandler<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken)
        {
            var requestType = request.GetType();

            var handlerFactory = (Func<IRequest<TResponse>, CancellationToken, SingleInstanceFactory, RequestHandlerDelegate<TResponse>>)
                _requestHandlerFactories.GetOrAdd(requestType, GetHandlerFactory<TResponse>(requestType, GetHandler));

            if (handlerFactory == null)
            {
                throw BuildException(request);
            }

            return handlerFactory(request, cancellationToken, _singleInstanceFactory);
        }

        private object GetHandler(Type requestType)
        {
{'fraction_non_alphanumeric': 0.07835111825756469, 'fraction_numerical': 0.0, 'mean_word_length': 2.8720996038483304, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1515988', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1517, 'n_words': 368}