ContainerExtensions.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. namespace Caliburn.Micro.Core {
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. /// <summary>
  7. /// Extension methods for the <see cref="SimpleContainer"/>.
  8. /// </summary>
  9. public static class ContainerExtensions {
  10. /// <summary>
  11. /// Registers a singleton.
  12. /// </summary>
  13. /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
  14. /// <param name="container">The container.</param>
  15. /// <param name="key">The key.</param>
  16. /// <returns>The container.</returns>
  17. public static SimpleContainer Singleton<TImplementation>(this SimpleContainer container, string key = null) {
  18. return Singleton<TImplementation, TImplementation>(container, key);
  19. }
  20. /// <summary>
  21. /// Registers a singleton.
  22. /// </summary>
  23. /// <typeparam name="TService">The type of the service.</typeparam>
  24. /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
  25. /// <param name="container">The container.</param>
  26. /// <param name="key">The key.</param>
  27. /// <returns>The container.</returns>
  28. public static SimpleContainer Singleton<TService, TImplementation>(this SimpleContainer container, string key = null)
  29. where TImplementation : TService {
  30. container.RegisterSingleton(typeof (TService), key, typeof (TImplementation));
  31. return container;
  32. }
  33. /// <summary>
  34. /// Registers an service to be created on each request.
  35. /// </summary>
  36. /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
  37. /// <param name="container">The container.</param>
  38. /// <param name="key">The key.</param>
  39. /// <returns>The container.</returns>
  40. public static SimpleContainer PerRequest<TImplementation>(this SimpleContainer container, string key = null) {
  41. return PerRequest<TImplementation, TImplementation>(container, key);
  42. }
  43. /// <summary>
  44. /// Registers an service to be created on each request.
  45. /// </summary>
  46. /// <typeparam name="TService">The type of the service.</typeparam>
  47. /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
  48. /// <param name="container">The container.</param>
  49. /// <param name="key">The key.</param>
  50. /// <returns>The container.</returns>
  51. public static SimpleContainer PerRequest<TService, TImplementation>(this SimpleContainer container, string key = null)
  52. where TImplementation : TService {
  53. container.RegisterPerRequest(typeof (TService), key, typeof (TImplementation));
  54. return container;
  55. }
  56. /// <summary>
  57. /// Registers an instance with the container.
  58. /// </summary>
  59. /// <typeparam name="TService">The type of the service.</typeparam>
  60. /// <param name="container">The container.</param>
  61. /// <param name="instance">The instance.</param>
  62. /// <returns>The container.</returns>
  63. public static SimpleContainer Instance<TService>(this SimpleContainer container, TService instance) {
  64. container.RegisterInstance(typeof (TService), null, instance);
  65. return container;
  66. }
  67. /// <summary>
  68. /// Registers a custom service handler with the container.
  69. /// </summary>
  70. /// <typeparam name="TService">The type of the service.</typeparam>
  71. /// <param name="container">The container.</param>
  72. /// <param name="handler">The handler.</param>
  73. /// <returns>The container.</returns>
  74. public static SimpleContainer Handler<TService>(this SimpleContainer container,
  75. Func<SimpleContainer, object> handler) {
  76. container.RegisterHandler(typeof (TService), null, handler);
  77. return container;
  78. }
  79. /// <summary>
  80. /// Registers all specified types in an assembly as singleton in the container.
  81. /// </summary>
  82. /// <typeparam name="TService">The type of the service.</typeparam>
  83. /// <param name="container">The container.</param>
  84. /// <param name="assembly">The assembly.</param>
  85. /// <param name="filter">The type filter.</param>
  86. /// <returns>The container.</returns>
  87. public static SimpleContainer AllTypesOf<TService>(this SimpleContainer container, Assembly assembly,
  88. Func<Type, bool> filter = null) {
  89. if (filter == null)
  90. filter = type => true;
  91. var serviceType = typeof (TService);
  92. var types = from type in assembly.GetTypes()
  93. where serviceType.IsAssignableFrom(type)
  94. && !type.IsAbstract()
  95. && !type.IsInterface()
  96. && filter(type)
  97. select type;
  98. foreach (var type in types) {
  99. container.RegisterSingleton(typeof (TService), null, type);
  100. }
  101. return container;
  102. }
  103. /// <summary>
  104. /// Requests an instance.
  105. /// </summary>
  106. /// <typeparam name="TService">The type of the service.</typeparam>
  107. /// <param name="container">The container.</param>
  108. /// <param name="key">The key.</param>
  109. /// <returns>The instance.</returns>
  110. public static TService GetInstance<TService>(this SimpleContainer container, string key = null) {
  111. return (TService) container.GetInstance(typeof (TService), key);
  112. }
  113. /// <summary>
  114. /// Gets all instances of a particular type.
  115. /// </summary>
  116. /// <typeparam name="TService">The type to resolve.</typeparam>
  117. /// <param name="container">The container.</param>
  118. /// <returns>The resolved instances.</returns>
  119. public static IEnumerable<TService> GetAllInstances<TService>(this SimpleContainer container) {
  120. return container.GetAllInstances(typeof (TService)).Cast<TService>();
  121. }
  122. /// <summary>
  123. /// Determines if a handler for the service/key has previously been registered.
  124. /// </summary>
  125. /// <typeparam name="TService">The service type.</typeparam>
  126. /// <param name="container">The container.</param>
  127. /// <param name="key">The key.</param>
  128. /// <returns>True if a handler is registere; false otherwise.</returns>
  129. public static bool HasHandler<TService>(this SimpleContainer container, string key = null) {
  130. return container.HasHandler(typeof (TService), key);
  131. }
  132. /// <summary>
  133. /// Unregisters any handlers for the service/key that have previously been registered.
  134. /// </summary>
  135. /// <typeparam name="TService">The service type.</typeparam>
  136. /// <param name="container">The container.</param>
  137. /// <param name = "key">The key.</param>
  138. public static void UnregisterHandler<TService>(this SimpleContainer container, string key = null) {
  139. container.UnregisterHandler(typeof(TService), key);
  140. }
  141. }
  142. }