PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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