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