Open Closed

Database driven Features List #10485


0
james.marley created

Hello,

I am trying to setup a dynamic list of features based on rows in a database table. I know I can add in features through code as documented but I haven't found any examples where I can query a table and populate a list of features. My attempts at reading items from a repository have resulted in issues around sync and async on application start up.

I wanted to check that what I'm trying to do is possible/correct and if there is any guidance availabe on how to do this.

Thanks


1 Answer(s)
  • 0
    musa.demir created
    Support Team

    Hello @james.marley

    You can follow that document to see how to define your features: https://aspnetboilerplate.com/Pages/Documents/Feature-Management#defining-features.

    To get datas from database you can simply inject your feature store and list items. For example:

    public class AppFeatureProvider : FeatureProvider
    {
    	private readonly IRepository<MyFeature> _myFeatureRepository;
    	private readonly IUnitOfWorkManager _unitOfWorkManager;
    
    	public AppFeatureProvider(IRepository<MyFeature> myFeatureRepository, IUnitOfWorkManager unitOfWorkManager)
    	{
    		_myFeatureRepository = myFeatureRepository;
    		_unitOfWorkManager = unitOfWorkManager;
    	}
    
    	public override void SetFeatures(IFeatureDefinitionContext context)
    	{
    		using (_unitOfWorkManager.Begin())
    		{
    			var myFeatures = _myFeatureRepository.GetAll().ToList();
    			
    			foreach (var myFeature in myFeatures){
    				var chatFeature = context.Create(
    					myFeature.Name,
    					defaultValue: myFeature.defaultValue,
    					displayName: myFeature.displayName,
    					inputType: GetInputTypeOfFeature(myFeature)
    				);			
    			}
    		}
    	}
    	
    	private IInputType GetInputTypeOfFeature(MyFeature feature){
    		//get input type name from db and cast it to IInputType you want
    		if(feature.InputType=="MySingleLineStringInputTypeKey"){
    			return new SingleLineStringInputType(new NumericValueValidator(0, int.MaxValue));
    		}
    		//...
    		return new CheckboxInputType();
    	}
    }