<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using SFA.DAS.UnitOfWork.Context;

namespace SFA.DAS.CommitmentsV2.Models
{
    public abstract class Entity
    {
        protected void Publish<T>(Func<T> action) where T : class
        {
            UnitOfWorkContext2.AddEvent(action);
        }
    }

    public class UnitOfWorkContext2 : IUnitOfWorkContext
    {
        private static readonly AsyncLocal<ConcurrentQueue<Func<object>>> Events = new AsyncLocal<ConcurrentQueue<Func<object>>>();

        private readonly ConcurrentDictionary<string, object> _data = new ConcurrentDictionary<string, object>();

        public UnitOfWorkContext2()
        {
            Events.Value = new ConcurrentQueue<Func<object>>();
        }

        public static void AddEvent<T>(T message) where T : class
        {
            Events.Value.Enqueue(() => message);
        }

        public static void AddEvent<T>(Func<T> messageFactory) where T : class
        {
            Events.Value.Enqueue(messageFactory);
        }

        void IUnitOfWorkContext.AddEvent<T>(T message)
        {
            AddEvent(message);
        }

        void IUnitOfWorkContext.AddEvent<T>(Func<T> action)
        {
            AddEvent(action);
        }

        public T Find<T>() where T : class
        {
            return _data.TryGetValue(typeof(T).FullName, out var value) ? (T)value : null;
        }

        public T Get<T>() where T : class
        {
            var key = typeof(T).FullName;

            if (_data.TryGetValue(key, out var value))
            {
                return (T)value;
            }

            throw new KeyNotFoundException($"The key '{key}' was not present in the unit of work context");
        }

        public IEnumerable<object> GetEvents()
        {
            return Events.Value.Select(e => e());
        }

        public void Set<T>(T value) where T : class
        {
            _data[typeof(T).FullName] = value;
        }
    }


}<commit_msg>Removing debug code<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using SFA.DAS.UnitOfWork.Context;

namespace SFA.DAS.CommitmentsV2.Models
{
    public abstract class Entity
    {
        protected void Publish<T>(Func<T> action) where T : class
        {
            UnitOfWorkContext.AddEvent(action);
        }
    }
}
{'fraction_non_alphanumeric': 0.10124049619847938, 'fraction_numerical': 0.0020008003201280513, 'mean_word_length': 2.1969309462915603, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, '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': '2492821', 'n_tokens_mistral': 706, 'n_tokens_neox': 697, 'n_words': 163}