<commit_before>#region (c) 2010 Lokad Open Source - New BSD License 

// Copyright (c) Lokad 2010, http://www.lokad.com
// This code is released as Open Source under the terms of the New BSD Licence

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using Autofac;
using Lokad.Cqrs.Domain;
using Lokad.Cqrs.Queue;

namespace Lokad.Cqrs.Consume
{
	public sealed class DispatchesSingleMessage : IMessageDispatcher
	{
		readonly ILifetimeScope _container;
		readonly IDictionary<Type, Type> _messageConsumers = new Dictionary<Type, Type>();
		readonly IMessageDirectory _messageDirectory;


		public DispatchesSingleMessage(ILifetimeScope container, IMessageDirectory messageDirectory)
		{
			_container = container;
			_messageDirectory = messageDirectory;
		}

		static void ThrowIfCommandHasMultipleConsumers(IEnumerable<MessageInfo> commands)
		{
			var multipleConsumers = commands
				.Where(c => c.AllConsumers.Length > 1)
				.ToArray(c => c.MessageType.FullName);

			if (multipleConsumers.Any())
			{
				throw new InvalidOperationException(
					"These messages have multiple consumers. Did you intend to declare them as events? " +
						multipleConsumers.Join(Environment.NewLine));
			}
		}

		public void Init()
		{
			ThrowIfCommandHasMultipleConsumers(_messageDirectory.Messages);
			foreach (var messageInfo in _messageDirectory.Messages)
			{
				if (!messageInfo.IsSystemMessage)
				{
					Enforce.That(messageInfo.AllConsumers.Length == 1);
				}
				if (messageInfo.AllConsumers.Length > 0)
				{
					_messageConsumers[messageInfo.MessageType] = messageInfo.AllConsumers[0];
				}
				
			}
		}

		public bool DispatchMessage(UnpackedMessage message)
		{
			Type consumerType;
			if (_messageConsumers.TryGetValue(message.ContractType, out consumerType))
			{
				using (var scope = _container.BeginLifetimeScope())
				{
					var consumer = scope.Resolve(consumerType);
					_messageDirectory.InvokeConsume(consumer, message);
				}

				return true;
			}
			return false;
		}
	}<commit_msg>Framework: fixed problem in single method dispatcher.<commit_after>#region (c) 2010 Lokad Open Source - New BSD License 

// Copyright (c) Lokad 2010, http://www.lokad.com
// This code is released as Open Source under the terms of the New BSD Licence

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using Autofac;
using Lokad.Cqrs.Domain;
using Lokad.Cqrs.Queue;

namespace Lokad.Cqrs.Consume
{
	public sealed class DispatchesSingleMessage : IMessageDispatcher
	{
		readonly ILifetimeScope _container;
		readonly IDictionary<Type, Type> _messageConsumers = new Dictionary<Type, Type>();
		readonly IMessageDirectory _messageDirectory;


		public DispatchesSingleMessage(ILifetimeScope container, IMessageDirectory messageDirectory)
		{
			_container = container;
			_messageDirectory = messageDirectory;
		}

		static void ThrowIfCommandHasMultipleConsumers(IEnumerable<MessageInfo> commands)
		{
			var multipleConsumers = commands
				.Where(c => c.AllConsumers.Length > 1)
				.ToArray(c => c.MessageType.FullName);

			if (multipleConsumers.Any())
			{
				throw new InvalidOperationException(
					"These messages have multiple consumers. Did you intend to declare them as events? " +
						multipleConsumers.Join(Environment.NewLine));
			}
		}

		public void Init()
		{
			ThrowIfCommandHasMultipleConsumers(_messageDirectory.Messages);
			foreach (var messageInfo in _messageDirectory.Messages)
			{
				if (!messageInfo.IsSystemMessage)
				{
					Enforce.That(messageInfo.AllConsumers.Length == 1);
				}
				if (messageInfo.AllConsumers.Length > 0)
				{
					_messageConsumers[messageInfo.MessageType] = messageInfo.AllConsumers[0];
				}
				
			}
		}

		public bool DispatchMessage(UnpackedMessage message)
		{
			Type consumerType;
			if (_messageConsumers.TryGetValue(message.ContractType, out consumerType))
			{
				using (var scope = _container.BeginLifetimeScope())
				{
					var consumer = scope.Resolve(consumerType);
					_messageDirectory.InvokeConsume(consumer, message.Content);
				}

				return true;
			}
			return false;
		}
	}
{'fraction_non_alphanumeric': 0.08688286544046467, 'fraction_numerical': 0.005808325266214908, 'mean_word_length': 4.05875152998776, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2368431', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1197, 'n_words': 292}