PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/V1/spikes/AGCompositeApplicationLibrary/AGComposite.UnityExtensions/UnityContainerAdapter.cs

#
C# | 78 lines | 31 code | 7 blank | 40 comment | 0 complexity | 5ace8fc9a96f2d4d4d883ce209a43353 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
18using System;
19using Microsoft.Practices.Unity;
20
21namespace Microsoft.Practices.Composite.UnityExtensions
22{
23    /// <summary>
24    /// Defines a <seealso cref="IUnityContainer"/> adapter for
25    /// the <see cref="IContainerFacade"/> interface
26    /// to be used by the Composite Application Library.
27    /// </summary>
28    public class UnityContainerAdapter : IContainerFacade
29    {
30        private readonly IUnityContainer _unityContainer;
31
32        /// <summary>
33        /// Initializes a new instance of <see cref="UnityContainerAdapter"/>.
34        /// </summary>
35        /// <param name="unityContainer">The <seealso cref="IUnityContainer"/> that will be used
36        /// by the <see cref="Resolve"/> and <see cref="TryResolve"/> methods.</param>
37        public UnityContainerAdapter(IUnityContainer unityContainer)
38        {
39            _unityContainer = unityContainer;
40        }
41
42        /// <summary>
43        /// Resolve an instance of the requested type from the container.
44        /// </summary>
45        /// <param name="type">The type of object to get from the container.</param>
46        /// <returns>An instance of <paramref name="type"/>.</returns>
47        /// <exception cref="ResolutionFailedException"><paramref name="type"/> cannot be resolved by the container.</exception>
48        public object Resolve(Type type)
49        {
50            return _unityContainer.Resolve(type);
51        }
52
53        /// <summary>
54        /// Tries to resolve an instance of the requested type from the container.
55        /// </summary>
56        /// <param name="type">The type of object to get from the container.</param>
57        /// <returns>
58        /// An instance of <paramref name="type"/>. 
59        /// If the type cannot be resolved it will return a <see langword="null"/> value.
60        /// </returns>
61        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
62        public object TryResolve(Type type)
63        {
64            object resolved;
65
66            try
67            {
68                resolved = Resolve(type);
69            }
70            catch
71            {
72                resolved = null;
73            }
74
75            return resolved;
76        }
77    }
78}