PageRenderTime 42ms CodeModel.GetById 16ms 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. //===================================================================================
  17. using System.ComponentModel.Composition.Hosting;
  18. namespace MVVM.Client.Infrastructure.StateManagement
  19. {
  20. /// <summary>
  21. /// Provides access to state management objects in the container and handles setting state while returning previous values.
  22. /// </summary>
  23. public class StateHandler
  24. {
  25. private readonly CompositionContainer container;
  26. public StateHandler(CompositionContainer container)
  27. {
  28. this.container = container;
  29. }
  30. public T SetState<T>(T context)
  31. {
  32. return SetCurrentState<T>(context);
  33. }
  34. /// <summary>
  35. /// Sets <paramref name="context"/> as the current context for type <typeparamref name="T"/>.
  36. /// </summary>
  37. private T SetCurrentState<T>(T context)
  38. {
  39. var currentState = GetInstance<ICurrentState<T>>();
  40. var previousValue = currentState.Value;
  41. currentState.Value = context;
  42. return previousValue;
  43. }
  44. private T GetInstance<T>()
  45. {
  46. return this.container.GetExportedValue<T>();
  47. }
  48. }
  49. }