PageRenderTime 13ms CodeModel.GetById 10ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/MVVM RI/MVVM.Client/Infrastructure/StateManagement/StateHandler.cs

#
C# | 55 lines | 27 code | 6 blank | 22 comment | 0 complexity | df9340bf08149b17575b1a3b12c9468b MD5 | raw file
 1//===================================================================================
 2// Microsoft patterns & practices
 3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
 4//===================================================================================
 5// Copyright (c) Microsoft Corporation.  All rights reserved.
 6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
 7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
 8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 9// FITNESS FOR A PARTICULAR PURPOSE.
10//===================================================================================
11// The example companies, organizations, products, domain names,
12// e-mail addresses, logos, people, places, and events depicted
13// herein are fictitious.  No association with any real company,
14// organization, product, domain name, email address, logo, person,
15// places, or events is intended or should be inferred.
16//===================================================================================
17using System.ComponentModel.Composition.Hosting;
18
19namespace MVVM.Client.Infrastructure.StateManagement
20{
21    /// <summary>
22    /// Provides access to state management objects in the container and handles setting state while returning previous values.
23    /// </summary>
24    public class StateHandler
25    {
26        private readonly CompositionContainer container;
27
28        public StateHandler(CompositionContainer container)
29        {
30            this.container = container;
31        }
32
33        public T SetState<T>(T context)
34        {
35            return SetCurrentState<T>(context);
36        }
37
38        /// <summary>
39        /// Sets <paramref name="context"/> as the current context for type <typeparamref name="T"/>.
40        /// </summary>
41        private T SetCurrentState<T>(T context)
42        {
43            var currentState = GetInstance<ICurrentState<T>>();
44            var previousValue = currentState.Value;
45            currentState.Value = context;
46
47            return previousValue;
48        }
49
50        private T GetInstance<T>()
51        {
52            return this.container.GetExportedValue<T>();
53        }
54    }
55}