<commit_before>﻿using System;
using CommandQueryExample.Common.Extensions;

namespace CommandQueryExample.Common
{
    public static class DataContextFactory
    {
        // TODO: HAS 02/09/2015 Handle multiple data context classes.
        static Func<IDataContext> _createDataContext;

        static IDataContext _currentContext;

        static readonly object _lock = new object();

        public static IDataContext CurrentContext
        {
            get
            {
                lock (_lock)
                {
                    if (_currentContext.IsNull()) throw new NullReferenceException("CurrentContext is null.");
                    if (_currentContext.IsDisposed) throw new ObjectDisposedException("CurrentContext has been disposed.");
                    return _currentContext;
                }
            }
            private set
            {
                lock (_lock)
                {
                    if (_currentContext.IsNotNull() && !_currentContext.IsDisposed)
                        throw new InvalidOperationException("CurrentContext has not been disposed.");
                    _currentContext = value;
                }
            }
        }

        public static Func<IDataContext> CreateDataContext
        {
            get
            {
                lock (_lock)
                {
                    if (_createDataContext.IsNull())
                        throw new NullReferenceException("DataContextFactory.CreateDataContext is not properly configured.");
                    if (_currentContext.IsNotNull() && !_currentContext.IsDisposed)
                        throw new InvalidOperationException("CurrentContext has not been disposed.");
                    return _createDataContext;
                }
            }
            set
            {
                _createDataContext = () =>
                {
                    CurrentContext = value();
                    return CurrentContext;
                };
            }
        }
    }
}<commit_msg>Convert DataContextFactory current context reference to a Lazy<T><commit_after>﻿using System;
using CommandQueryExample.Common.Extensions;

namespace CommandQueryExample.Common
{
    public static class DataContextFactory
    {
        // TODO: HAS 02/09/2015 Handle multiple data context classes.
        static Func<IDataContext> _createDataContext;

        static Lazy<IDataContext> _currentContext;

        static readonly object _lock = new object();

        public static IDataContext CurrentContext
        {
            get
            {
                lock (_lock)
                {
                    if (_currentContext.IsNull()) throw new NullReferenceException("CurrentContext is null.");
                    if (_currentContext.Value.IsNull()) throw new NullReferenceException("CurrentContext is null.");
                    if (_currentContext.Value.IsDisposed) throw new ObjectDisposedException("CurrentContext has been disposed.");
                    return _currentContext.Value;
                }
            }
            private set
            {
                lock (_lock)
                {
                    if (_currentContext.IsNotNull() &&_currentContext.Value.IsNotNull() && !_currentContext.Value.IsDisposed)
                        throw new InvalidOperationException("CurrentContext has not been disposed.");
                    _currentContext = new Lazy<IDataContext>(() => value);
                }
            }
        }

        public static Func<IDataContext> CreateDataContext
        {
            get
            {
                lock (_lock)
                {
                    if (_createDataContext.IsNull())
                        throw new NullReferenceException("DataContextFactory.CreateDataContext is not properly configured.");
                    if (_currentContext.IsNotNull() && !_currentContext.Value.IsDisposed)
                        throw new InvalidOperationException("CurrentContext has not been disposed.");
                    return _createDataContext;
                }
            }
            set
            {
                _createDataContext = () =>
                {
                    CurrentContext = value();
                    return CurrentContext;
                };
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.06896551724137931, 'fraction_numerical': 0.0037028465632955336, 'mean_word_length': 1.4390519187358917, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 13, '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': '250516', 'n_tokens_mistral': 954, 'n_tokens_neox': 878, 'n_words': 225}