FeatureChecker.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Threading.Tasks;
  2. using Abp.Dependency;
  3. using Abp.Runtime.Session;
  4. namespace Abp.Application.Features
  5. {
  6. /// <summary>
  7. /// Default implementation for <see cref="IFeatureChecker"/>.
  8. /// </summary>
  9. public class FeatureChecker : IFeatureChecker, ITransientDependency
  10. {
  11. /// <summary>
  12. /// Reference to the current session.
  13. /// </summary>
  14. public IAbpSession AbpSession { get; set; }
  15. /// <summary>
  16. /// Reference to the store used to get feature values.
  17. /// </summary>
  18. public IFeatureValueStore FeatureValueStore { get; set; }
  19. private readonly IFeatureManager _featureManager;
  20. /// <summary>
  21. /// Creates a new <see cref="FeatureChecker"/> object.
  22. /// </summary>
  23. public FeatureChecker(IFeatureManager featureManager)
  24. {
  25. _featureManager = featureManager;
  26. FeatureValueStore = NullFeatureValueStore.Instance;
  27. AbpSession = NullAbpSession.Instance;
  28. }
  29. /// <inheritdoc/>
  30. public Task<string> GetValueAsync(string name)
  31. {
  32. if (!AbpSession.TenantId.HasValue)
  33. {
  34. throw new AbpException("FeatureChecker can not get a feature value by name. TenantId is not set in the IAbpSession!");
  35. }
  36. return GetValueAsync(AbpSession.TenantId.Value, name);
  37. }
  38. /// <inheritdoc/>
  39. public async Task<string> GetValueAsync(int tenantId, string name)
  40. {
  41. var feature = _featureManager.Get(name);
  42. var value = await FeatureValueStore.GetValueOrNullAsync(tenantId, feature);
  43. if (value == null)
  44. {
  45. return feature.DefaultValue;
  46. }
  47. return value;
  48. }
  49. }
  50. }