Skip to content

FeatureOne.SQL

Ninja Sha!4h edited this page May 24, 2023 · 4 revisions

Feature toggles with SQL Backend.

FeatureOne.SQL package provides out of box SQL storage provider.

SQL support can easily be installed as a separate nuget package.

$ dotnet add package FeatureOne.SQL --version {latest}

Supports Db Providers MSSQL: System.Data.SqlClient, ODBC: System.Data.Odbc, OLEDB: System.Data.OleDb, SQLite: System.Data.SQLite, MySQL: MySql.Data.MySqlClient & PostgreSQL: Npgsql.

For any other SQL provider, You need to add provider factory to DbProviderFactories.RegisterFactory("ProviderName", ProviderFactory) and pass the provider specific connection settings in SQLConfiguration.

Database Setup

Requires creating a feature table with columns for feature name, toggle definition and feature archival.

SQL SCRIPT below.

CREATE TABLE TFeatures (
    Id              INT NOT NULL IDENTITY PRIMARY KEY,
    Name            VARCHAR(255) NOT NULL,
    Toggle          NVARCHAR(4000) NOT NULL,
    Archived        BIT CONSTRAINT DF_TFeatures_Archived DEFAULT (0)
);

Example Table Record

Feature toggles need to be scripted to backend database in JSON format.

Please see example entries below.

| Name |Toggle | Archived | |||| | dashboard_widget |{ "conditions":[{ "type":"Simple", "isEnabled": true }] } | false | |pen_test_dashboard| { "operator":"any", "conditions":[{ "type":"simple", "isEnabled":false}, { "type":"Regex", "claim":"email","expression":"^[a-zA-Z0-9_.+-][email protected]" }]} | false|

Bootstrap initialization

See below bootstrap initialization for FeatureOne with SQL backend.

SQL Configuration - Set connection string and other settings.

    var sqlConfiguration = new SQLConfiguration
    {
        // provider specific connection settings.
        ConnectionSettings = new ConnectionSettings
        {
            Providername = DbProviderName.MSSql, 
            ConnectionString ="Data Source=Powerstation; Initial Catalog=Features; Integrated Security=SSPI;"            
        },

        // Table and column name overrides.
        FeatureTable = new FeatureTable
        {
            TableName = "[Features].[dbo].[TFeatures]",  
            NameColumn = "[Name]",
            ToggleColumn = "[Toggle]",
            ArchivedColumn = "[Archived]"
        },

        // Enable cache with absolute expiry in Minutes.
        CacheSettings = new CacheSettings 
        {
            EnableCache = true,  
            Expiry = new CacheExpiry
            {
                InMinutes = 60,
                Type = CacheExpiryType.Absolute
            }
        }
    }

i. With SQL configuration.

   var storageProvider = new SQlStorageProvider(sqlConfiguration);
   Features.Initialize(() => new Features(new FeatureStore(storageProvider)));

ii. With Custom logger implementation, default is no logger.

    var logger = new CustomLoggerImpl();
    var storageProvider = new SQlStorageProvider(sqlConfiguration, logger);

    Features.Initialize(() => new Features(new FeatureStore(storageProvider, logger), logger));

iii. With other overloads - Custom cache and Toggle Condition deserializer.

    var toggleConditionDeserializer = CustomConditionDeserializerImpl(); // Implements IConditionDeserializer 
    var featureCache = CustomFeatureCache(); // Implements ICache

    var storageProvider = new SQlStorageProvider(sqlConfiguration, featureCache, toggleConditionDeserializer);

    Features.Initialize(() => new Features(new FeatureStore(storageProvider, logger), logger));