IServiceLocator.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. namespace Wayne.Lib
  3. {
  4. /// <summary>
  5. /// This interface represents the locator part of the service container. Clients are often only interested in retrieving
  6. /// services from the container and thus only interested in this contract instead of the entire service container.
  7. /// </summary>
  8. public interface IServiceLocator
  9. {
  10. /// <summary>
  11. /// Retrieves the service of type serviceType from the container.
  12. /// </summary>
  13. /// <typeparam name="TServiceContract">Type of service.</typeparam>
  14. /// <returns>Service from the service container.</returns>
  15. TServiceContract GetService<TServiceContract>();
  16. /// <summary>
  17. /// Retrieves the service of type serviceType from the container.
  18. /// </summary>
  19. /// <typeparam name="TServiceContract">Type of service.</typeparam>
  20. /// <param name="serviceId">Id of the service , supplied upon registration.</param>
  21. /// <returns>Service from the service container.</returns>
  22. TServiceContract GetService<TServiceContract>(string serviceId);
  23. /// <summary>
  24. /// Retrieves the service of type serviceType from the container.
  25. /// </summary>
  26. /// <param name="serviceType">Type of service.</param>
  27. /// <returns>Service from the service container.</returns>
  28. object GetService(Type serviceType);
  29. /// <summary>
  30. /// Retrieves the service of type serviceType from the container.
  31. /// </summary>
  32. /// <param name="serviceType">Type of service.</param>
  33. /// <param name="serviceId">Id of the service , supplied upon registration.</param>
  34. /// <returns>Service from the service container.</returns>
  35. object GetService(Type serviceType, string serviceId);
  36. /// <summary>
  37. /// Gets a registered service. If the service is not registered, it invokes the supplied
  38. /// callback to get a default instance of the service. NOTE that this instance is not automatically registered.
  39. /// </summary>
  40. /// <typeparam name="TServiceContract"></typeparam>
  41. /// <param name="func"></param>
  42. /// <returns></returns>
  43. TServiceContract GetServiceOrDefault<TServiceContract>(CreateDefaultService<TServiceContract> func);
  44. /// <summary>
  45. /// Gets a registered service. If the service is not registered, it invokes the supplied
  46. /// callback to get a default instance of the service. NOTE that this instance is not automatically registered.
  47. /// </summary>
  48. /// <typeparam name="TServiceContract"></typeparam>
  49. /// <param name="func"></param>
  50. /// <param name="serviceId">Id of the service , supplied upon registration.</param>
  51. /// <returns></returns>
  52. TServiceContract GetServiceOrDefault<TServiceContract>(CreateDefaultService<TServiceContract> func, string serviceId);
  53. /// <summary>
  54. /// Creates an instance of T. Service locator uses the first constructor it finds that it can use based on what is
  55. /// registered in the service container and the additional parameters.
  56. /// </summary>
  57. /// <typeparam name="T"></typeparam>
  58. /// <param name="additionalParameter">List of parameters that</param>
  59. /// <returns>An instance of the created object</returns>
  60. [Obsolete("Use ServiceActivator.Create<T> instead")]
  61. T CreateInstance<T>(params object[] additionalParameter) where T : class;
  62. /// <summary>
  63. /// Creates an instance of T. Service locator uses the first constructor it finds that it can use based on what is
  64. /// registered in the service container and the additional parameters.
  65. /// </summary>
  66. /// <typeparam name="T"></typeparam>
  67. /// <param name="serviceId">Id of the service , supplied upon registration.</param>
  68. /// <param name="additionalParameter">List of parameters that</param>
  69. /// <returns>An instance of the created object</returns>
  70. [Obsolete("Use ServiceActivator.Create<T> instead")]
  71. T CreateInstance<T>(string serviceId, params object[] additionalParameter) where T : class;
  72. /// <summary>
  73. /// Creates an instance of the specified type. Service locator uses the first constructor it finds that it can use
  74. /// based on what is regeistered in the service container and the additional parameters.
  75. /// </summary>
  76. /// <param name="typeToInstantiate"></param>
  77. /// <param name="additionalParameter"></param>
  78. /// <returns></returns>
  79. object CreateInstance(Type typeToInstantiate, params object[] additionalParameter);
  80. /// <summary>
  81. /// Creates an instance of the specified type. Service locator uses the first constructor it finds that it can use
  82. /// based on what is regeistered in the service container and the additional parameters.
  83. /// </summary>
  84. /// <param name="typeToInstantiate"></param>
  85. /// <param name="serviceId">Id of the service , supplied upon registration.</param>
  86. /// <param name="additionalParameter"></param>
  87. /// <returns></returns>
  88. object CreateInstance(Type typeToInstantiate, string serviceId, params object[] additionalParameter);
  89. /// <summary>
  90. /// Tries to locate the service T. If it does not succeed, it returns null.
  91. /// </summary>
  92. /// <typeparam name="T"></typeparam>
  93. /// <returns></returns>
  94. T TryGetService<T>();
  95. /// <summary>
  96. /// Tries to locate the service T. If it does not succeed, it returns null.
  97. /// </summary>
  98. /// <typeparam name="T"></typeparam>
  99. /// <param name="serviceId">Id of the service , supplied upon registration.</param>
  100. /// <returns></returns>
  101. T TryGetService<T>(string serviceId);
  102. /// <summary>
  103. /// Tries to locate the specified service. If it does not succeed it returns null.
  104. /// </summary>
  105. /// <param name="serviceType"></param>
  106. /// <returns></returns>
  107. object TryGetService(Type serviceType);
  108. /// <summary>
  109. /// Tries to locate the specified service. If it does not succeed it returns null.
  110. /// </summary>
  111. /// <param name="serviceType"></param>
  112. /// <param name="serviceId">Id of the service , supplied upon registration.</param>
  113. /// <returns></returns>
  114. object TryGetService(Type serviceType, string serviceId);
  115. }
  116. /// <summary>
  117. /// Delegate for creating a default service with the IServiceLocator.GetServiceOrDefault method.
  118. /// </summary>
  119. /// <typeparam name="T"></typeparam>
  120. /// <returns></returns>
  121. public delegate T CreateDefaultService<T>();
  122. }