BackgroundJob.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.BackgroundJobs
  8. {
  9. /// <summary>
  10. /// Base class that can be used to implement <see cref="IBackgroundJob{TArgs}"/>.
  11. /// </summary>
  12. public abstract class BackgroundJob<TArgs> : IBackgroundJob<TArgs>
  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 BackgroundJob()
  75. {
  76. Logger = NullLogger.Instance;
  77. LocalizationManager = NullLocalizationManager.Instance;
  78. }
  79. public abstract void Execute(TArgs args);
  80. /// <summary>
  81. /// Gets localized string for given key name and current language.
  82. /// </summary>
  83. /// <param name="name">Key name</param>
  84. /// <returns>Localized string</returns>
  85. protected virtual string L(string name)
  86. {
  87. return LocalizationSource.GetString(name);
  88. }
  89. /// <summary>
  90. /// Gets localized string for given key name and current language with formatting strings.
  91. /// </summary>
  92. /// <param name="name">Key name</param>
  93. /// <param name="args">Format arguments</param>
  94. /// <returns>Localized string</returns>
  95. protected string L(string name, params object[] args)
  96. {
  97. return LocalizationSource.GetString(name, args);
  98. }
  99. /// <summary>
  100. /// Gets localized string for given key name and specified culture information.
  101. /// </summary>
  102. /// <param name="name">Key name</param>
  103. /// <param name="culture">culture information</param>
  104. /// <returns>Localized string</returns>
  105. protected virtual string L(string name, CultureInfo culture)
  106. {
  107. return LocalizationSource.GetString(name, culture);
  108. }
  109. /// <summary>
  110. /// Gets localized string for given key name and current language with formatting strings.
  111. /// </summary>
  112. /// <param name="name">Key name</param>
  113. /// <param name="culture">culture information</param>
  114. /// <param name="args">Format arguments</param>
  115. /// <returns>Localized string</returns>
  116. protected string L(string name, CultureInfo culture, params object[] args)
  117. {
  118. return LocalizationSource.GetString(name, culture, args);
  119. }
  120. }
  121. }