<commit_before>﻿using System.Linq;
using System.Reflection;
using Scritchy.Infrastructure.Exceptions;
using System;
using System.Collections.Generic;
using Scritchy.Domain;

namespace Scritchy.Infrastructure.Implementations
{

    public class CommandBus : ICommandBus
    {
        public IEventStore eventstore;
        public HandlerRegistry handlerregistry = new HandlerRegistry();
        public IHandlerInstanceResolver resolver;

        static readonly Helpers.Synchronizer<string> ArLocker = new Helpers.Synchronizer<string>();

        public CommandBus(IEventStore eventstore, HandlerRegistry handlerregistry, IHandlerInstanceResolver resolver)
        {
            this.eventstore = eventstore;
            this.handlerregistry = handlerregistry;
            this.resolver = resolver;
        }

        public void RunCommand(object Command)
        {
            var key = handlerregistry.RegisteredHandlers.Where(x => x.MessageType == Command.GetType()).FirstOrDefault();
            if (key == null)
                throw new InvalidOperationException("No handler found for the commands of type " + Command.GetType().Name);
            var id = Command.GetType().GetProperty(key.InstanceType.Name + "Id").GetValue(Command, null) as string;
            Guid index = Guid.NewGuid();
            AR ar = null;
            try
            {
                var handler = handlerregistry[key];
                using (var mylock = ArLocker.Lock(id))
                {
                    lock (mylock)
                    {
                        ar = resolver.LoadARSnapshot(key.InstanceType, id);
                        handler(ar, Command);
                    }
                }
            }
            catch (TargetInvocationException ex)
            {
                throw new FailedCommandException(ex.InnerException, Command);
            }
            if (!eventstore.SaveEvents(ar.Changes.GetPublishedEvents()))
                throw new SaveEventsException { Events = ar.Changes.GetPublishedEvents() };
        }

    }
}<commit_msg>+ Added the possibility to have multiple AR handlers for a command<commit_after>﻿using System.Linq;
using System.Reflection;
using Scritchy.Infrastructure.Exceptions;
using System;
using System.Collections.Generic;
using Scritchy.Domain;

namespace Scritchy.Infrastructure.Implementations
{

    public class CommandBus : ICommandBus
    {
        public IEventStore eventstore;
        public HandlerRegistry handlerregistry = new HandlerRegistry();
        public IHandlerInstanceResolver resolver;

        static readonly Helpers.Synchronizer<string> ArLocker = new Helpers.Synchronizer<string>();

        public CommandBus(IEventStore eventstore, HandlerRegistry handlerregistry, IHandlerInstanceResolver resolver)
        {
            this.eventstore = eventstore;
            this.handlerregistry = handlerregistry;
            this.resolver = resolver;
        }

        public void RunCommand(object Command)
        {
            var keys = handlerregistry.RegisteredHandlers.Where(x => x.MessageType == Command.GetType());
            if (!keys.Any() )
                throw new InvalidOperationException("No handler found for the commands of type " + Command.GetType().Name);
            foreach (var key in keys)
            {
                var id = Command.GetType().GetProperty(key.InstanceType.Name + "Id").GetValue(Command, null) as string;
                Guid index = Guid.NewGuid();
                AR ar = null;
                try
                {
                    var handler = handlerregistry[key];
                    using (var mylock = ArLocker.Lock(id))
                    {
                        lock (mylock)
                        {
                            ar = resolver.LoadARSnapshot(key.InstanceType, id);
                            handler(ar, Command);
                        }
                    }
                }
                catch (TargetInvocationException ex)
                {
                    throw new FailedCommandException(ex.InnerException, Command);
                }
                if (!eventstore.SaveEvents(ar.Changes.GetPublishedEvents()))
                    throw new SaveEventsException { Events = ar.Changes.GetPublishedEvents() };
            }
        }

    }
}
{'fraction_non_alphanumeric': 0.07981438515081206, 'fraction_numerical': 0.0, 'mean_word_length': 1.8625498007968126, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '6884123', 'n_tokens_mistral': 1052, 'n_tokens_neox': 992, 'n_words': 241}