BackgroundWorkerBase.cs 5.2 KB

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