IocRegistrarExtensions.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. namespace Abp.Dependency
  3. {
  4. /// <summary>
  5. /// Extension methods for <see cref="IIocRegistrar"/> interface.
  6. /// </summary>
  7. public static class IocRegistrarExtensions
  8. {
  9. #region RegisterIfNot
  10. /// <summary>
  11. /// Registers a type as self registration if it's not registered before.
  12. /// </summary>
  13. /// <typeparam name="T">Type of the class</typeparam>
  14. /// <param name="iocRegistrar">Registrar</param>
  15. /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
  16. /// <returns>True, if registered for given implementation.</returns>
  17. public static bool RegisterIfNot<T>(this IIocRegistrar iocRegistrar, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
  18. where T : class
  19. {
  20. if (iocRegistrar.IsRegistered<T>())
  21. {
  22. return false;
  23. }
  24. iocRegistrar.Register<T>(lifeStyle);
  25. return true;
  26. }
  27. /// <summary>
  28. /// Registers a type as self registration if it's not registered before.
  29. /// </summary>
  30. /// <param name="iocRegistrar">Registrar</param>
  31. /// <param name="type">Type of the class</param>
  32. /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
  33. /// <returns>True, if registered for given implementation.</returns>
  34. public static bool RegisterIfNot(this IIocRegistrar iocRegistrar, Type type, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
  35. {
  36. if (iocRegistrar.IsRegistered(type))
  37. {
  38. return false;
  39. }
  40. iocRegistrar.Register(type, lifeStyle);
  41. return true;
  42. }
  43. /// <summary>
  44. /// Registers a type with it's implementation if it's not registered before.
  45. /// </summary>
  46. /// <typeparam name="TType">Registering type</typeparam>
  47. /// <typeparam name="TImpl">The type that implements <see cref="TType"/></typeparam>
  48. /// <param name="iocRegistrar">Registrar</param>
  49. /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
  50. /// <returns>True, if registered for given implementation.</returns>
  51. public static bool RegisterIfNot<TType, TImpl>(this IIocRegistrar iocRegistrar, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
  52. where TType : class
  53. where TImpl : class, TType
  54. {
  55. if (iocRegistrar.IsRegistered<TType>())
  56. {
  57. return false;
  58. }
  59. iocRegistrar.Register<TType, TImpl>(lifeStyle);
  60. return true;
  61. }
  62. /// <summary>
  63. /// Registers a type with it's implementation if it's not registered before.
  64. /// </summary>
  65. /// <param name="iocRegistrar">Registrar</param>
  66. /// <param name="type">Type of the class</param>
  67. /// <param name="impl">The type that implements <paramref name="type"/></param>
  68. /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
  69. /// <returns>True, if registered for given implementation.</returns>
  70. public static bool RegisterIfNot(this IIocRegistrar iocRegistrar, Type type, Type impl, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
  71. {
  72. if (iocRegistrar.IsRegistered(type))
  73. {
  74. return false;
  75. }
  76. iocRegistrar.Register(type, impl, lifeStyle);
  77. return true;
  78. }
  79. #endregion
  80. }
  81. }