Git Product home page Git Product logo

growthbook-c-sharp's Introduction

growthbook banner with csharp logo

growthbook-c-sharp

Powerful feature flagging and A/B testing for C# apps using GrowthBook

nuget badge for growthbook

Table of contents

Usage examples

This library is based on the GrowthBook SDK specs and should be compatible with the usage examples in the GrowthBook docs.

Down below you'll find two basic examples on how the package works.

Basic

  1. Install GrowthBook

    dotnet add package growthbook-c-sharp
  2. Declare features

    var staticFeatures = new Dictionary<string, Feature>
        {
            {"firstFeature", new Feature{ DefaultValue = true}},
            {"secondFeature", new Feature{ DefaultValue = false}}
        };
  3. Create a context and add the features

    var context = new Context
    {
        Enabled = true,
        Url = "",
        Features = staticFeatures
    };
  4. Create the GrowthBook object with the context

    using GrowthBook;
    //...
    
    var GrowthBook = new GrowthBook.GrowthBook(context);
  5. Check whether a feature is enabled

    GrowthBook.IsOn("firstFeature") // true
    GrowthBook.IsOn("secondFeature") // false

Loading feature definitions from an API

Because feature definitions are typically loaded from API calls or cache, Json.NET objects are used to represent arbitrary document types such as Attributes, Conditions, and Feature values.

To load your features from the GrowthBook API use the following example:

  1. Create a result model for the API response

    public class FeaturesResult
    {
        public HttpStatusCode Status { get; set; }
        public IDictionary<string, Feature>? Features { get; set; }
        public DateTimeOffset? DateUpdated { get; set; }
    }
  2. call the endpoint and deserialize result

     var url = "YOUR_GROWTHBOOK_URL/api/features/YOUR_API_KEY";
    var response = await client.GetAsync(url);
    
    if (response.IsSuccessStatusCode)
    {
        var content = await response.Content.ReadAsStringAsync();
        var featuresResult = JsonConvert.DeserializeObject<FeaturesResult>(content);
    }
  3. Construct a context and initialize GrowthBook

    var GrowthBook = new GrowthBook.GrowthBook(
        new Context {
            Enabled = true,
            Url = "",
            Features = featuresResult.Features,
        }
    );

Generic getters

To make it easier to deal with Feature values, generic getter functions are provided for the following:

  • Experiment:
    • GetVariations<T>()
  • ExperimentResult:
    • GetValue<T>()
  • Feature:
    • GetDefaultValue<T>()
  • FeatureResult:
    • GetValue<T>()
  • Feature Rule:
    • GetVariations<T>()
  • GrowthBook:
    • GetFeatureValue<T>(string key, T fallback)

growthbook-c-sharp's People

Contributors

darklordzw avatar mastermindzh avatar auz avatar iangledhill avatar tamazbagdavadze avatar

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.