<commit_before>using System;
using System.Collections.Generic;
using StructureMap;

namespace ShortBus
{
    public class Bus : IBus
    {
        readonly IContainer _container;

        public Bus(IContainer container)
        {
            _container = container;
        }

        public virtual Response<TResponseData> Request<TResponseData>(IQuery<TResponseData> query)
        {
            var response = new Response<TResponseData>();

            var handler = GetHandler(query);

            AssertHandlerNotNull(query, handler);

            try
            {
                response.Data = GetResponseData(query, handler);
            }
            catch (Exception e)
            {
                response.Exception = e;
            }

            return response;
        }

        public virtual Response Send<TMessage>(TMessage message)
        {
            var allInstances = _container.GetAllInstances<ICommandHandler<TMessage>>();

            var response = new Response();
            List<Exception> exceptions = null;
            foreach (var handler in allInstances)
                try
                {
                    handler.Handle(message);
                }
                catch (Exception e)
                {
                    (exceptions ?? (exceptions = new List<Exception>())).Add(e);
                }
            if (exceptions != null) 
                response.Exception = new AggregateException(exceptions);
            return response;
        }

        static void AssertHandlerNotNull<TResponseData>(IQuery<TResponseData> query, object handler)
        {
            if (handler == null)
                throw new InvalidOperationException(string.Format("handler not found for message of type {0}",
                                                                  query.GetType().Name));
        }

        static TResponseData GetResponseData<TResponseData>(IQuery<TResponseData> query, object handler)
        {
            var helperType = typeof (Helper<,>).MakeGenericType(query.GetType(), typeof (TResponseData));
            var helper = ((IHelper) Activator.CreateInstance(helperType));
            var responseData = (TResponseData) helper.ExecuteHandler(handler, query);
            return responseData;
        }

        object GetHandler<TResponseData>(IQuery<TResponseData> query)
        {
            var handlerType = typeof (IQueryHandler<,>).MakeGenericType(query.GetType(), typeof (TResponseData));
            var handler = _container.TryGetInstance(handlerType);
            return handler;
        }

        class Helper<TRequest, TResponse> : IHelper where TRequest : IQuery<TResponse>
        {
            public object ExecuteHandler(object handler, object query)
            {
                return ((IQueryHandler<TRequest, TResponse>) handler).Handle((TRequest) query);
            }
        }

        interface IHelper
        {
            object ExecuteHandler(object handler, object query);
        }
    }
}<commit_msg>using expressions instead of activator.create instance<commit_after>using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using StructureMap;

namespace ShortBus
{
    public class Bus : IBus
    {
        readonly IContainer _container;

        public Bus(IContainer container)
        {
            _container = container;
        }

        public virtual Response<TResponseData> Request<TResponseData>(IQuery<TResponseData> query)
        {
            var response = new Response<TResponseData>();

            var handler = GetHandler(query);

            AssertHandlerNotNull(query, handler);

            try
            {
                response.Data = GetResponseData(query, handler);
            }
            catch (Exception e)
            {
                response.Exception = e;
            }

            return response;
        }

        public virtual Response Send<TMessage>(TMessage message)
        {
            var allInstances = _container.GetAllInstances<ICommandHandler<TMessage>>();

            var response = new Response();
            List<Exception> exceptions = null;
            foreach (var handler in allInstances)
                try
                {
                    handler.Handle(message);
                }
                catch (Exception e)
                {
                    (exceptions ?? (exceptions = new List<Exception>())).Add(e);
                }
            if (exceptions != null)
                response.Exception = new AggregateException(exceptions);
            return response;
        }

        static void AssertHandlerNotNull<TResponseData>(IQuery<TResponseData> query, object handler)
        {
            if (handler == null)
                throw new InvalidOperationException(string.Format("handler not found for message of type {0}",
                                                                  query.GetType().Name));
        }

        TResponseData GetResponseData<TResponseData>(IQuery<TResponseData> query, object handler)
        {
            var helper = InstantiateHelper(query);
            var responseData = (TResponseData) helper.ExecuteHandler(handler, query);
            return responseData;
        }

        IHelper InstantiateHelper<TResponseData>(IQuery<TResponseData> query)
        {
            var helperType = typeof (Helper<,>).MakeGenericType(query.GetType(), typeof (TResponseData));
            var ctor = helperType.GetConstructors()[0];
            var newExpression = Expression.New(ctor);
            var makeHandler = (MakeHandler) Expression.Lambda(typeof (MakeHandler), newExpression).Compile();
            return ((IHelper) makeHandler());
        }

        object GetHandler<TResponseData>(IQuery<TResponseData> query)
        {
            var handlerType = typeof (IQueryHandler<,>).MakeGenericType(query.GetType(), typeof (TResponseData));
            var handler = _container.TryGetInstance(handlerType);
            return handler;
        }

        class Helper<TRequest, TResponse> : IHelper where TRequest : IQuery<TResponse>
        {
            public object ExecuteHandler(object handler, object query)
            {
                return ((IQueryHandler<TRequest, TResponse>) handler).Handle((TRequest) query);
            }
        }

        interface IHelper
        {
            object ExecuteHandler(object handler, object query);
        }

        delegate object MakeHandler();
    }
}
{'fraction_non_alphanumeric': 0.08494208494208494, 'fraction_numerical': 0.0004633204633204633, 'mean_word_length': 1.816876903001305, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2044178', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1473, 'n_words': 354}