| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System.Threading.Tasks;
- using Abp.Dependency;
- using Abp.Runtime.Session;
- namespace Abp.Application.Features
- {
- /// <summary>
- /// Default implementation for <see cref="IFeatureChecker"/>.
- /// </summary>
- public class FeatureChecker : IFeatureChecker, ITransientDependency
- {
- /// <summary>
- /// Reference to the current session.
- /// </summary>
- public IAbpSession AbpSession { get; set; }
- /// <summary>
- /// Reference to the store used to get feature values.
- /// </summary>
- public IFeatureValueStore FeatureValueStore { get; set; }
- private readonly IFeatureManager _featureManager;
- /// <summary>
- /// Creates a new <see cref="FeatureChecker"/> object.
- /// </summary>
- public FeatureChecker(IFeatureManager featureManager)
- {
- _featureManager = featureManager;
- FeatureValueStore = NullFeatureValueStore.Instance;
- AbpSession = NullAbpSession.Instance;
- }
- /// <inheritdoc/>
- public Task<string> GetValueAsync(string name)
- {
- if (!AbpSession.TenantId.HasValue)
- {
- throw new AbpException("FeatureChecker can not get a feature value by name. TenantId is not set in the IAbpSession!");
- }
- return GetValueAsync(AbpSession.TenantId.Value, name);
- }
- /// <inheritdoc/>
- public async Task<string> GetValueAsync(int tenantId, string name)
- {
- var feature = _featureManager.Get(name);
- var value = await FeatureValueStore.GetValueOrNullAsync(tenantId, feature);
- if (value == null)
- {
- return feature.DefaultValue;
- }
- return value;
- }
- }
- }
|