using System.Globalization;
using Abp.Configuration;
using Abp.Domain.Uow;
using Abp.Localization;
using Abp.Localization.Sources;
using Abp.ObjectMapping;
using Castle.Core.Logging;
namespace Abp
{
///
/// This class can be used as a base class for services.
/// It has some useful objects property-injected and has some basic methods
/// most of services may need to.
///
public abstract class AbpServiceBase
{
///
/// Reference to the setting manager.
///
public ISettingManager SettingManager { 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 { 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; }
///
/// Reference to the object to object mapper.
///
public IObjectMapper ObjectMapper { get; set; }
///
/// Constructor.
///
protected AbpServiceBase()
{
Logger = NullLogger.Instance;
ObjectMapper = NullObjectMapper.Instance;
LocalizationManager = NullLocalizationManager.Instance;
}
///
/// 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);
}
}
}