Giter Club home page Giter Club logo

featurebits's Introduction

FeatureBits - A .NET (and multi-platform) feature toggling system

Build status NuGet NuGet

FeatureBits is a feature toggling system to support Continuous Delivery on multiple platforms.

FeatureBits is now available in NuGet!

install-package featurebits.core

For more information about how feature toggling works, please see Feature Toggles (aka Feature Flags)

How to use

Note: A complete sample application of the steps below can be found at feature-bits-sample.

  1. Create a .NET Core 2.X Web Application (Web API) application.
  2. In the Package Manager Console, Install-Package featurebits.core -IncludePrerelease
  3. Edit your .csproj, add the following <ItemGroup> section:
  <ItemGroup>
    <DotNetCliToolReference Include="dotnet-fbit" Version="0.4.1" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="*" />
  </ItemGroup>

The SecretManager tool is not absolutely necessary, but will keep you from having to continually enter your SQL or Azure Table connection strings when using the command-line interface.

Next:

  1. Create your SQL database or Azure Table to store the FeatureBit definitions. For SQL, you can use (or modify for your particular environment) FeatureBitDefinitions.SQL, found in this repository.
  2. Right-click your web project in Visual Studio 2017 or greater and select "Manage User Secrets"
  3. Add the following into your User Secrets file:
{
  "fbit:-s":  "[your SQL Connection string goes here]",
  "ConnectionStrings": {
    "FeatureBitsDbContext": "[your SQL Connection string goes here]"
  }

}

If you plan to use Azure Table storage, use "fbit:-a"

{
  "fbit:-a":  "[your Azure Table Storage Connection string goes here]"
}

The sample application assumes a local SQL Server from this point.

  1. Follow the instructions in Safe storage of app secrets in development in ASP.NET Core to set up your web applciation to consume User Secrets from the web application's configuration.
  2. Navigate a console window to the web application's folder and execute the following command, which creates an initial FeatureBit definition that is always on: dotnet fbit add -n DummyOn -o true
  3. Add another FeatureBit definition, which always evaluates to off, as follows: dotnet fbit add -n DummyOff -o false
  4. Now generate an enum for your new feature bit definitions with: dotnet fbit generate -n "SampleWeb"
  5. Edit your Startup.cs file for dependency injection of your SQL connection string and the FeatureBitEvaluator:
public void ConfigureServices(IServiceCollection services)
{
    string featureBitsConnectionString = Configuration.GetConnectionString("FeatureBitsDbContext");
    services.AddDbContext<FeatureBitsEfDbContext>(options => options.UseSqlServer(featureBitsConnectionString));
    services.AddTransient<IFeatureBitsRepo, FeatureBitsEfRepo>((serviceProvider) =>
    {
        DbContextOptionsBuilder<FeatureBitsEfDbContext> options = new DbContextOptionsBuilder<FeatureBitsEfDbContext>();
        options.UseSqlServer(featureBitsConnectionString);
        var context = new FeatureBitsEfDbContext(options.Options);
        return new FeatureBitsEfRepo(context);
    });
    services.AddTransient<IFeatureBitEvaluator, FeatureBitEvaluator>();

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

Now add a simple controller that can handle requests for evaluating FeatureBits to your web application:

using System.Linq;
using FeatureBits.Core;
using Microsoft.AspNetCore.Mvc;

namespace SampleWeb.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FeatureBitsController : ControllerBase
    {

        private readonly IFeatureBitEvaluator _evaluator;

        public FeatureBitsController(IFeatureBitEvaluator evaluator)
        {
            _evaluator = evaluator;
        }

        [HttpGet("/{id}")]
        public IActionResult GetById([FromRoute] int id)
        {
            var definition = _evaluator.Definitions.SingleOrDefault(d => d.Id == id);

            if (definition != null)
            {
                bool isEnabled = _evaluator.IsEnabled((Features) id, 0);
                return new JsonResult(isEnabled);
            }

            return NotFound();
        }
    }
}

In ValuesController.cs, replace the first two methods with the following code:

private readonly IFeatureBitEvaluator _evaluator;

public ValuesController(IFeatureBitEvaluator evaluator)
{
    _evaluator = evaluator;
}

// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
    if (_evaluator.IsEnabled(Features.DummyOn))
    {
        return new string[] { "value1", "value2" };
    }
    else
    {
        return new string[] { };
    }
}

// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
    if (_evaluator.IsEnabled(Features.DummyOff))
    {
        return "value";
    }
    else
    {
        return "";
    }
}

FeatureBit Definitions

  1. On/Off (column OnOff) - if (and only if) no other columns are populated, then the OnOff column determines whether the feature is on or off.
  2. ExcludedEnvironments - Comma separated list of environments for which the feature should be turned off. Attempts to read the ASPNETCORE_ENVIRONMENT environment variable to determine whether the bit should be on or off. IncludedEnvironments (when supplied) supercedes ExcludedEnvironments.
  3. IncludedEnvironments - Comma separated list of environments for which the feature should be turned on. Attempts to read the ASPNETCORE_ENVIRONMENT environment variable to determine whether the bit should be on or off.
  4. MinimumAllowedPermissionLevel - If the user's permission level (as uniquely determine by your application) is greater than or equal to a certain integer value, then the feature bit is "on".
  5. ExactAllowedPermissionLevel - Same as the last one, but the user's permission level must exactly match (equals).

You can also take a look at the file FeatureBitEvaluatorTests.cs to see how the different kinds of feature bit definitions are used.

Other feature bit definitions are in the works.

fbit .NET Core Console Application

If you include a <DotNetCliToolReference> to dotnet-fbit as mentioned above, you'll be able to take advantage of the FeatureBit CLI rather than manipulating the SQL DB or Azure Table directly. Help is enabled for the CLI and any of its 'verbs'. For example, dotnet fbit --help

Features

  • FeatureBits can be used in .NET Core, and via your own Web API, through TypeScript/Angular. Currently supports SQL Server, Azure SQL Database, and Azure Data Tables on the back end.

Contributing

See CONTRIBUTING.md and review the Microsoft Open Source Code of Conduct.

Description

Feature Toggling is important to support Continuous Integration/Continuous Deployment. Features that may be experimental or incomplete can be hidden behind a FeatureBit for some, most, or all users. FeatureBits currently supports both .NET Core and TypeScript/Angular.

Reporting Security Issues

Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) at [email protected]. You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Further information, including the MSRC PGP key, can be found in the Security TechCenter.

featurebits's People

Contributors

dseelinger avatar jackwagons avatar microsoftopensource avatar msftgits avatar petkahl avatar sitwalkstand avatar styxxy avatar themitchk avatar vanderby avatar ynauls avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

featurebits's Issues

Nuget publish is deploying an extra package for fbit-cli

the following packages are being deployed to nuget.org. dotnet-fbit.0.3.0.25.nupkg should not be deployed.

  • (BUG) src\FeatureBits.Console\bin\Release\dotnet-fbit.0.3.0.25.nupkg
  • src\FeatureBits.Console\bin\Release\dotnet-fbit.0.3.0.nupkg
  • src\FeatureBits.Core\bin\Release\FeatureBits.Core.0.3.0.nupkg
  • src\FeatureBits.Data\bin\Release\FeatureBits.Data.0.3.0.nupkg

ExcludedEnvironments value needs to be parsed into individual items for comparison

During evaluation, this feature bit is doing a "Contains()" to search for a string value. Since ExcludedEnvironments is a comma delimited string, it should be split and each item compared to the string.

private static bool EvaluateEnvironmentBasedFeatureState(IFeatureBitDefinition bitDef) { bool featureState; var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT").ToUpperInvariant(); featureState = !bitDef.ExcludedEnvironments.ToUpperInvariant().Contains(env); return featureState; }

KeyNotFoundException if identity seed does not exactly match feature enums

I cloned the repo and inserted a test feature directly using SQL which incremented the identity seed. I then found your suggestion to use the 'dotnet fbit add' command. When I added the features using that command, my FeatureBitDefinitions table's identity seed no longer matched the enum values in the sample app. Since line 53 of IsEnabled in FeatureBitEvaluator is using the Id to lookup the feature it throws a KeyNotFoundException when bitDef is null.

Expected Outcome: The way features are defined in code should be decoupled from the way SQL handles identities or even from the persistence implementation itself.

Possible solutions:

  • Change the Id column in FeatureBitDefinitions to be a primary key integer with a unique constraint but not an auto-incrementing identity, that way the application can supply the identity as it defines new enums.

  • Change the IsEnabled implementation to look the feature up by name instead of by Id. This would require changing the SQL script to also create a unique constraint on the name column and could break existing implementations if they have duplicate names.

  • Change the application architecture to make feature resolution / persistence extendable and not as tightly coupled to the database implementation.

  • Enable identity insert in the FeatureBits.Console dotnet fbit command

fbit-cli: add support for "list" command

fbit list should display feature bit definitions
By default "fbit list" shows the ID and Name of the feature bits

  • Accepts a required connection string properties
  • Accepts a -Long argument, which shows all feature bit fields.

Create User-based Feature Bit

Schema already supports User-based feature bit, but there is no way to create one with fbit add, and there is no evaluation of user-based feature bits.

Microsoft.FeatureManagment package

There is any relation with the development of the package Microsoft.FeatureManagement, nuget, github, documentation, that seems to come from the Azure team.
Both packages have related goals, they work both on netcore.
It would be better, IMHO, tohave a unified approach to feature flags.

Upgrade to .NET Core 2.1

Is there a plan to upgrade these projects to .NET Core 2.1?

I've cloned the repo locally and would be interested in tackling that task, but I don't have the ability to create a branch.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.