'How do I map enums to a class property
I have an enum that holds feature names such as
public enum FeatureNames{
[Description("Amazing Feature")]
AmazingFeature,
[Description("Amazing New Feature")]
AmazingNewFeature
}
I have a domain entity that has properties representing each of the feature which indicates if the feature is turned on or off
public class CompanyAccount{
public bool IsAmazingFeatureEnabled{get;set;}
public bool IsAmazingNewFeatureEnabled{get;set;}
}
This is how the existing application was written and hence I don't want to make drastic changes to DB at this point. So now I am trying to write a genric method that will take in a string or enum value indicating feature and check if the property matching the same feature is enabled or not(true or false).
public bool IsEnabled(FeatureNames featureName){
if(featureName is FeatureNames.AmazingFeature){
return companyAccount.IsAmazingFeatureEnabled;
}
else if(featureName is FeatureNames.AmazingNewFeature){
return companyAccount.IsAmazingNewFeatureEnabled;
}
Is there a better way to handle this? Can we avoid the if condition?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|