<commit_before>﻿using System;
using System.Diagnostics;
using System.Threading;

namespace Lokad.Cqrs.Core.Directory
{
    public sealed class MethodContextManager<TContext> : IMethodContextManager
        where TContext : class
    {
        readonly ThreadLocal<TContext> _context = new ThreadLocal<TContext>();

        readonly Func<ImmutableEnvelope, ImmutableMessage, TContext> _factory;

        public MethodContextManager(Func<ImmutableEnvelope, ImmutableMessage, TContext> factory)
        {
            _factory = factory;
        }

        public void SetContext(ImmutableEnvelope envelope, ImmutableMessage message)
        {
            _context.Value = _factory(envelope, message);
            if (Debugger.IsAttached)
            {
                Thread.CurrentThread.Name = string.Format("Consume: {0}", message.MappedType.Name);
            }
        }

        public void ClearContext()
        {
            _context.Value = null;
            if (Debugger.IsAttached)
            {
                Thread.CurrentThread.Name = "Consume: <Wait>";
            }
        }

        public TContext Get()
        {
            if (_context.Value == null)
            {
                throw new InvalidOperationException("Context is not set outside of message invocation");
            }<commit_msg>Removing stupid code - threads can't be renamed.<commit_after>﻿#region (c) 2010-2011 Lokad - CQRS for Windows Azure - New BSD License 

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

#endregion

using System;
using System.Threading;

namespace Lokad.Cqrs.Core.Directory
{
    public sealed class MethodContextManager<TContext> : IMethodContextManager
        where TContext : class
    {
        readonly ThreadLocal<TContext> _context = new ThreadLocal<TContext>();

        readonly Func<ImmutableEnvelope, ImmutableMessage, TContext> _factory;

        public MethodContextManager(Func<ImmutableEnvelope, ImmutableMessage, TContext> factory)
        {
            _factory = factory;
        }

        public void SetContext(ImmutableEnvelope envelope, ImmutableMessage message)
        {
            _context.Value = _factory(envelope, message);
        }

        public void ClearContext()
        {
            _context.Value = null;
        }

        public TContext Get()
        {
            if (_context.Value == null)
            {
                throw new InvalidOperationException("Context is not set outside of message invocation");
            }
{'fraction_non_alphanumeric': 0.07898832684824902, 'fraction_numerical': 0.006614785992217899, 'mean_word_length': 2.2421185372005046, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '3020757', 'n_tokens_mistral': 674, 'n_tokens_neox': 642, 'n_words': 173}