Beispiel #1
0
    public void WhenCurrentDataIsSet_ThenCanRetrieveIt()
    {
        var data = new Foo {
            Id = 5
        };

        using (ThreadData.SetData(data))
        {
            var saved = ThreadData.GetData <Foo>();

            Assert.Same(data, saved);
        }
    }
Beispiel #2
0
    public void WhenCurrentDisposableIsDisposed_ThenDataIsRemoved()
    {
        var data = new Foo {
            Id = 5
        };

        using (ThreadData.SetData(data))
        {
        }

        var saved = ThreadData.GetData <Foo>();

        Assert.Null(saved);
    }
Beispiel #3
0
    public void WhenMultipleThreadsSetData_ThenEachAccessesDifferentData()
    {
        Action action = () =>
        {
            var data = ThreadData.GetData <Foo>();
            Assert.Null(data);

            data = new Foo {
                Id = Thread.CurrentThread.ManagedThreadId
            };
            ThreadData.SetData(data);

            var threadData = ThreadData.GetData <Foo>();

            Assert.NotNull(threadData);
            Assert.Same(threadData, data);
        };

        var tasks = Enumerable.Range(0, 20).Select(x => Task.Factory.StartNew(action)).ToArray();

        Task.WaitAll(tasks);
    }
        public static Stack <DataStorage> GetStack(string databaseName, bool createIfEmpty)
        {
            // Sitecore.Common.Switcher uses Sitecore.Context.Items which is either ThreadLocal or HttpContext.Current
            // based on whether HttpContext is available
            var store = ThreadData.GetData(ThreadData.Keys.Items) as IDictionary;

            if (store == null)
            {
                store = new Hashtable();
                ThreadData.SetData(ThreadData.Keys.Items, store);
            }

            var stack = store[ItemsKey + databaseName] as Stack <DataStorage>;

            if (stack == null && createIfEmpty)
            {
                stack = new Stack <DataStorage>();
                store[ItemsKey + databaseName] = stack;
            }

            return(stack);
        }