PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/V1/trunk/Source/CAL/Composite.UnityExtensions/UnityContainerHelper.cs

#
C# | 78 lines | 34 code | 4 blank | 40 comment | 2 complexity | 7b391169848b95fe143d158efc9b09ad MD5 | raw file
 1//===============================================================================
 2// Microsoft patterns & practices
 3// Composite Application Guidance for Windows Presentation Foundation
 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
18using System;
19using Microsoft.Practices.Unity;
20
21namespace Microsoft.Practices.Composite.UnityExtensions
22{
23    /// <summary>
24    /// Extensions methods to extend and facilitate the usage of <see cref="IUnityContainer"/>.
25    /// </summary>
26    public static class UnityContainerHelper
27    {
28        /// <summary>
29        /// Returns whether a specified type has a type mapping registered in the container.
30        /// </summary>
31        /// <param name="container">The <see cref="IUnityContainer"/> to check for the type mapping.</param>
32        /// <param name="type">The type to check if there is a type mapping for.</param>
33        /// <returns><see langword="true"/> if there is a type mapping registered for <paramref name="type"/>.</returns>
34        /// <remarks>In order to use this extension method, you first need to add the
35        /// <see cref="IUnityContainer"/> extension to the <see cref="UnityBootstrapperExtension"/>.
36        /// </remarks>
37        public static bool IsTypeRegistered(this IUnityContainer container, Type type)
38        {
39            return UnityBootstrapperExtension.IsTypeRegistered(container, type);
40        }
41
42        /// <summary>
43        /// Utility method to try to resolve a service from the container avoiding an exception if the container cannot build the type.
44        /// </summary>
45        /// <param name="container">The cointainer that will be used to resolve the type.</param>
46        /// <typeparam name="T">The type to resolve.</typeparam>
47        /// <returns>The instance of <typeparamref name="T"/> built up by the container.</returns>
48        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
49        public static T TryResolve<T>(this IUnityContainer container)
50        {
51            object result = TryResolve(container, typeof(T));
52            if (result != null)
53            {
54                return (T)result;
55            }
56            return default(T);
57        }
58
59        /// <summary>
60        /// Utility method to try to resolve a service from the container avoiding an exception if the container cannot build the type.
61        /// </summary>
62        /// <param name="container">The cointainer that will be used to resolve the type.</param>
63        /// <param name="typeToResolve">The type to resolve.</param>
64        /// <returns>The instance of <paramref name="typeToResolve"/> built up by the container.</returns>
65        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
66        public static object TryResolve(this IUnityContainer container, Type typeToResolve)
67        {
68            try
69            {
70                return container.Resolve(typeToResolve);
71            }
72            catch
73            {
74                return null;
75            }
76        }
77    }
78}