using System.Globalization;
using Abp.Configuration;
using Abp.Domain.Uow;
using Abp.Localization;
using Abp.Localization.Sources;
using Castle.Core.Logging;
namespace Abp.Threading.BackgroundWorkers
{
///
/// Base class that can be used to implement .
///
public abstract class BackgroundWorkerBase : RunnableBase, IBackgroundWorker
{
///
/// Reference to the setting manager.
///
public ISettingManager SettingManager { protected get; set; }
///
/// Reference to .
///
public IUnitOfWorkManager UnitOfWorkManager
{
get
{
if (_unitOfWorkManager == null)
{
throw new AbpException("Must set UnitOfWorkManager before use it.");
}
return _unitOfWorkManager;
}
set { _unitOfWorkManager = value; }
}
private IUnitOfWorkManager _unitOfWorkManager;
///
/// Gets current unit of work.
///
protected IActiveUnitOfWork CurrentUnitOfWork { get { return UnitOfWorkManager.Current; } }
///
/// Reference to the localization manager.
///
public ILocalizationManager LocalizationManager { protected get; set; }
///
/// Gets/sets name of the localization source that is used in this application service.
/// It must be set in order to use and methods.
///
protected string LocalizationSourceName { get; set; }
///
/// Gets localization source.
/// It's valid if is set.
///
protected ILocalizationSource LocalizationSource
{
get
{
if (LocalizationSourceName == null)
{
throw new AbpException("Must set LocalizationSourceName before, in order to get LocalizationSource");
}
if (_localizationSource == null || _localizationSource.Name != LocalizationSourceName)
{
_localizationSource = LocalizationManager.GetSource(LocalizationSourceName);
}
return _localizationSource;
}
}
private ILocalizationSource _localizationSource;
///
/// Reference to the logger to write logs.
///
public ILogger Logger { protected get; set; }
///
/// Constructor.
///
protected BackgroundWorkerBase()
{
Logger = NullLogger.Instance;
LocalizationManager = NullLocalizationManager.Instance;
}
public override void Start()
{
base.Start();
Logger.Debug("Start background worker: " + ToString());
}
public override void Stop()
{
base.Stop();
Logger.Debug("Stop background worker: " + ToString());
}
public override void WaitToStop()
{
base.WaitToStop();
Logger.Debug("WaitToStop background worker: " + ToString());
}
///
/// Gets localized string for given key name and current language.
///
/// Key name
/// Localized string
protected virtual string L(string name)
{
return LocalizationSource.GetString(name);
}
///
/// Gets localized string for given key name and current language with formatting strings.
///
/// Key name
/// Format arguments
/// Localized string
protected string L(string name, params object[] args)
{
return LocalizationSource.GetString(name, args);
}
///
/// Gets localized string for given key name and specified culture information.
///
/// Key name
/// culture information
/// Localized string
protected virtual string L(string name, CultureInfo culture)
{
return LocalizationSource.GetString(name, culture);
}
///
/// Gets localized string for given key name and current language with formatting strings.
///
/// Key name
/// culture information
/// Format arguments
/// Localized string
protected string L(string name, CultureInfo culture, params object[] args)
{
return LocalizationSource.GetString(name, culture, args);
}
public override string ToString()
{
return GetType().FullName;
}
}
}