PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/V2.2/trunk/CAL/Desktop/Composite.Presentation/Regions/RegionAdapterMappings.cs

#
C# | 85 lines | 42 code | 7 blank | 36 comment | 8 complexity | 7bf801d063f9f83d05c3ddfeedb1b454 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;
  18. using System.Collections.Generic;
  19. using System.Globalization;
  20. using Microsoft.Practices.Composite.Regions;
  21. using Microsoft.Practices.Composite.Presentation.Properties;
  22. namespace Microsoft.Practices.Composite.Presentation.Regions
  23. {
  24. /// <summary>
  25. /// This class maps <see cref="Type"/> with <see cref="IRegionAdapter"/>.
  26. /// </summary>
  27. public class RegionAdapterMappings
  28. {
  29. private readonly Dictionary<Type, IRegionAdapter> mappings = new Dictionary<Type, IRegionAdapter>();
  30. /// <summary>
  31. /// Registers the mapping between a type and an adapter.
  32. /// </summary>
  33. /// <param name="controlType">The type of the control.</param>
  34. /// <param name="adapter">The adapter to use with the <paramref name="controlType"/> type.</param>
  35. /// <exception cref="ArgumentNullException">When any of <paramref name="controlType"/> or <paramref name="adapter"/> are <see langword="null" />.</exception>
  36. /// <exception cref="InvalidOperationException">If a mapping for <paramref name="controlType"/> already exists.</exception>
  37. public void RegisterMapping(Type controlType, IRegionAdapter adapter)
  38. {
  39. if (controlType == null)
  40. {
  41. throw new ArgumentNullException("controlType");
  42. }
  43. if (adapter == null)
  44. {
  45. throw new ArgumentNullException("adapter");
  46. }
  47. if (mappings.ContainsKey(controlType))
  48. {
  49. throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
  50. Resources.MappingExistsException, controlType.Name));
  51. }
  52. mappings.Add(controlType, adapter);
  53. }
  54. /// <summary>
  55. /// Returns the adapter associated with the type provided.
  56. /// </summary>
  57. /// <param name="controlType">The type to obtain the <seealso cref="IRegionAdapter"/> mapped.</param>
  58. /// <returns>The <seealso cref="IRegionAdapter"/> mapped to the <paramref name="controlType"/>.</returns>
  59. /// <remarks>This class will look for a registered type for <paramref name="controlType"/> and if there is not any,
  60. /// it will look for a registered type for any of its ancestors in the class hierarchy.
  61. /// If there is no registered type for <paramref name="controlType"/> or any of its ancestors,
  62. /// an exception will be thrown.</remarks>
  63. /// <exception cref="KeyNotFoundException">When there is no registered type for <paramref name="controlType"/> or any of its ancestors.</exception>
  64. public IRegionAdapter GetMapping(Type controlType)
  65. {
  66. Type currentType = controlType;
  67. while (currentType != null)
  68. {
  69. if (mappings.ContainsKey(currentType))
  70. {
  71. return mappings[currentType];
  72. }
  73. currentType = currentType.BaseType;
  74. }
  75. throw new KeyNotFoundException("controlType");
  76. }
  77. }
  78. }