AbpServiceBase.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Globalization;
  2. using Abp.Configuration;
  3. using Abp.Domain.Uow;
  4. using Abp.Localization;
  5. using Abp.Localization.Sources;
  6. using Abp.ObjectMapping;
  7. using Castle.Core.Logging;
  8. namespace Abp
  9. {
  10. /// <summary>
  11. /// This class can be used as a base class for services.
  12. /// It has some useful objects property-injected and has some basic methods
  13. /// most of services may need to.
  14. /// </summary>
  15. public abstract class AbpServiceBase
  16. {
  17. /// <summary>
  18. /// Reference to the setting manager.
  19. /// </summary>
  20. public ISettingManager SettingManager { get; set; }
  21. /// <summary>
  22. /// Reference to <see cref="IUnitOfWorkManager"/>.
  23. /// </summary>
  24. public IUnitOfWorkManager UnitOfWorkManager
  25. {
  26. get
  27. {
  28. if (_unitOfWorkManager == null)
  29. {
  30. throw new AbpException("Must set UnitOfWorkManager before use it.");
  31. }
  32. return _unitOfWorkManager;
  33. }
  34. set { _unitOfWorkManager = value; }
  35. }
  36. private IUnitOfWorkManager _unitOfWorkManager;
  37. /// <summary>
  38. /// Gets current unit of work.
  39. /// </summary>
  40. protected IActiveUnitOfWork CurrentUnitOfWork { get { return UnitOfWorkManager.Current; } }
  41. /// <summary>
  42. /// Reference to the localization manager.
  43. /// </summary>
  44. public ILocalizationManager LocalizationManager { get; set; }
  45. /// <summary>
  46. /// Gets/sets name of the localization source that is used in this application service.
  47. /// It must be set in order to use <see cref="L(string)"/> and <see cref="L(string,CultureInfo)"/> methods.
  48. /// </summary>
  49. protected string LocalizationSourceName { get; set; }
  50. /// <summary>
  51. /// Gets localization source.
  52. /// It's valid if <see cref="LocalizationSourceName"/> is set.
  53. /// </summary>
  54. protected ILocalizationSource LocalizationSource
  55. {
  56. get
  57. {
  58. if (LocalizationSourceName == null)
  59. {
  60. throw new AbpException("Must set LocalizationSourceName before, in order to get LocalizationSource");
  61. }
  62. if (_localizationSource == null || _localizationSource.Name != LocalizationSourceName)
  63. {
  64. _localizationSource = LocalizationManager.GetSource(LocalizationSourceName);
  65. }
  66. return _localizationSource;
  67. }
  68. }
  69. private ILocalizationSource _localizationSource;
  70. /// <summary>
  71. /// Reference to the logger to write logs.
  72. /// </summary>
  73. public ILogger Logger { protected get; set; }
  74. /// <summary>
  75. /// Reference to the object to object mapper.
  76. /// </summary>
  77. public IObjectMapper ObjectMapper { get; set; }
  78. /// <summary>
  79. /// Constructor.
  80. /// </summary>
  81. protected AbpServiceBase()
  82. {
  83. Logger = NullLogger.Instance;
  84. ObjectMapper = NullObjectMapper.Instance;
  85. LocalizationManager = NullLocalizationManager.Instance;
  86. }
  87. /// <summary>
  88. /// Gets localized string for given key name and current language.
  89. /// </summary>
  90. /// <param name="name">Key name</param>
  91. /// <returns>Localized string</returns>
  92. protected virtual string L(string name)
  93. {
  94. return LocalizationSource.GetString(name);
  95. }
  96. /// <summary>
  97. /// Gets localized string for given key name and current language with formatting strings.
  98. /// </summary>
  99. /// <param name="name">Key name</param>
  100. /// <param name="args">Format arguments</param>
  101. /// <returns>Localized string</returns>
  102. protected string L(string name, params object[] args)
  103. {
  104. return LocalizationSource.GetString(name, args);
  105. }
  106. /// <summary>
  107. /// Gets localized string for given key name and specified culture information.
  108. /// </summary>
  109. /// <param name="name">Key name</param>
  110. /// <param name="culture">culture information</param>
  111. /// <returns>Localized string</returns>
  112. protected virtual string L(string name, CultureInfo culture)
  113. {
  114. return LocalizationSource.GetString(name, culture);
  115. }
  116. /// <summary>
  117. /// Gets localized string for given key name and current language with formatting strings.
  118. /// </summary>
  119. /// <param name="name">Key name</param>
  120. /// <param name="culture">culture information</param>
  121. /// <param name="args">Format arguments</param>
  122. /// <returns>Localized string</returns>
  123. protected string L(string name, CultureInfo culture, params object[] args)
  124. {
  125. return LocalizationSource.GetString(name, culture, args);
  126. }
  127. }
  128. }