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