Feature.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Immutable;
  4. using System.Linq;
  5. using Abp.Collections.Extensions;
  6. using Abp.Localization;
  7. using Abp.UI.Inputs;
  8. namespace Abp.Application.Features
  9. {
  10. /// <summary>
  11. /// Defines a feature of the application. A <see cref="Feature"/> can be used in a multi-tenant application
  12. /// to enable or disable some application features depending on editions and tenants.
  13. /// </summary>
  14. public class Feature
  15. {
  16. /// <summary>
  17. /// Gets/sets the arbitrary objects related to this object.
  18. /// Gets null if a given key does not exist.
  19. /// This is a shortcut for the <see cref="Attributes"/> dictionary.
  20. /// </summary>
  21. /// <param name="key">Key</param>
  22. public object this[string key]
  23. {
  24. get => Attributes.GetOrDefault(key);
  25. set => Attributes[key] = value;
  26. }
  27. /// <summary>
  28. /// Arbitrary objects related to this object.
  29. /// These objects must be serializable.
  30. /// </summary>
  31. public IDictionary<string, object> Attributes { get; private set; }
  32. /// <summary>
  33. /// Parent of this feature, if one exists.
  34. /// If set, this feature can be enabled only if the parent is enabled.
  35. /// </summary>
  36. public Feature Parent { get; private set; }
  37. /// <summary>
  38. /// Unique name of the feature.
  39. /// </summary>
  40. public string Name { get; private set; }
  41. /// <summary>
  42. /// Display name of this feature.
  43. /// This can be used to show features on the UI.
  44. /// </summary>
  45. public ILocalizableString DisplayName { get; set; }
  46. /// <summary>
  47. /// A brief description for this feature.
  48. /// This can be used to show this feature's description on the UI.
  49. /// </summary>
  50. public ILocalizableString Description { get; set; }
  51. /// <summary>
  52. /// Input type.
  53. /// This can be used to prepare an input for changing this feature's value.
  54. /// Default: <see cref="CheckboxInputType"/>.
  55. /// </summary>
  56. public IInputType InputType { get; set; }
  57. /// <summary>
  58. /// Default value of this feature.
  59. /// This value is used if this feature's value is not defined for the current edition or tenant.
  60. /// </summary>
  61. public string DefaultValue { get; set; }
  62. /// <summary>
  63. /// Feature's scope.
  64. /// </summary>
  65. public FeatureScopes Scope { get; set; }
  66. /// <summary>
  67. /// List of child features.
  68. /// </summary>
  69. public IReadOnlyList<Feature> Children => _children.ToImmutableList();
  70. private readonly List<Feature> _children;
  71. /// <summary>
  72. /// Creates a new feature.
  73. /// </summary>
  74. /// <param name="name">Unique name of the feature</param>
  75. /// <param name="defaultValue">Default value</param>
  76. /// <param name="displayName">Display name of the feature</param>
  77. /// <param name="description">A brief description for the feature</param>
  78. /// <param name="scope">Feature scope</param>
  79. /// <param name="inputType">Input type</param>
  80. public Feature(string name, string defaultValue, ILocalizableString displayName = null, ILocalizableString description = null, FeatureScopes scope = FeatureScopes.All, IInputType inputType = null)
  81. {
  82. Name = name ?? throw new ArgumentNullException("name");
  83. DisplayName = displayName;
  84. Description = description;
  85. Scope = scope;
  86. DefaultValue = defaultValue;
  87. InputType = inputType ?? new CheckboxInputType();
  88. _children = new List<Feature>();
  89. Attributes = new Dictionary<string, object>();
  90. }
  91. /// <summary>
  92. /// Adds a child feature.
  93. /// </summary>
  94. /// <returns>Returns a newly created child feature</returns>
  95. public Feature CreateChildFeature(string name, string defaultValue, ILocalizableString displayName = null, ILocalizableString description = null, FeatureScopes scope = FeatureScopes.All, IInputType inputType = null)
  96. {
  97. var feature = new Feature(name, defaultValue, displayName, description, scope, inputType) { Parent = this };
  98. _children.Add(feature);
  99. return feature;
  100. }
  101. public void RemoveChildFeature(string name)
  102. {
  103. var featureToRemove = _children.FirstOrDefault(f => f.Name == name);
  104. _children.Remove(featureToRemove);
  105. }
  106. public override string ToString()
  107. {
  108. return string.Format("[Feature: {0}]", Name);
  109. }
  110. }
  111. }