Skip to content

Client Getting Started

baileyjs78 edited this page Aug 16, 2019 · 6 revisions

Configuration

Once the client has been installed in your application, the following mandatory keys have to be added in the application configuration file:

  • The application name
  • The environment name
  • The API endpoint used in order to retrieve the Feature Toggles (Moggles server)

The Application Name needs to exactly match the value setup in the server, and will be used to load all the Feature Toggles for that application.

The Environment Name needs to exactly match the value setup in the server, and will be used to load the Feature Toggle state for that environment. If you have multiple environments you should transform your configuration files to specify a different environment value for each deployed environment.

The API Endpoint will be constructed as YourServerName and /api/FeatureToggles/getApplicationFeatureToggles The endpoint can work with http or https

Additional configuration, such as timeouts, are also available.

.NET Framework

web.config

  <appSettings>
    <!--REQUIRED KEYS-->
    <add key="Moggles.ApplicationName" value="MogglesExampleApp" />
    <add key="Moggles.Environment" value="DEV" />
    <add key="Moggles.Url" value="http://myFeatureToggleSource.com/api/FeatureToggles/getApplicationFeatureToggles" />
  </appSettings>

.NET Core

appsettings.json

"Moggles": {
    //REQUIRED KEYS
    "ApplicationName": "MogglesExampleApp",
    "Environment": "DEV",
    "Url": "http://myFeatureToggleSource.com/api/FeatureToggles/getApplicationFeatureToggles"
  } 

StartUp

After the values are added in the configuration file, the startup method of MogglesClient has to be called at application start. The method will initialize the client and it will cache the feature toggles.

.NET Framework

Global.asax

    using MogglesClient.PublicInterface;  
    
    public void Application_Start()
    {
      Moggles.ConfigureAndStartClient();
    }

.NET Core

The IConfiguration object will have to be passed to the method.

Startup.cs

   using MogglesClient.PublicInterface;  
   
   public void ConfigureServices(IServiceCollection services)
   {
     Moggles.ConfigureAndStartClient(Configuration);
   }

Creating Feature Toggles

Once the code is installed, configured and set for startup, the only thing left to do is to add a Feature Toggle.

Each Feature Toggle needs to have a corresponding class that inherits from MogglesFeatureToggle. The feature toggle class name has to be the exactly the same as the feature toggle added in Moggles server. (ie.. if you setup your server with "MyFirstToggle", then your class name will be "MyFirstToggle")

No additional code or attributes are necessary. The namespace that you choose (TestApp.FeatureToggles) will be used when you need to reference your Feature Toggle.

    using MogglesClient.PublicInterface;

    namespace TestApp.FeatureToggles
    {
        public class MyFirstToggle: MogglesFeatureToggle
        {

        }
    }

Finally, locate the section of code that you want to toggle, and add an IF statement, wrapping new code or a different method call. You will need to add a using statement for the MogglesClient and your FeatureToggle namespace you just created.

   using MogglesClient.PublicInterface;
   using MoggleTestApp.FeatureToggles;

   if (Is<MyFirstToggle>.Enabled)
   {
      NewCode();
   } else {
      OriginalCode();
   }

For additional Feature Toggles, you only need to repeat this last section.

Pushing Values to Views

If you need to use your Feature Toggle inside a view, you can update the IndexModel as Follows:

    public class IndexModel : PageModel
    {
        public bool myFirstToggle { get; set; }
        public void OnGet()
        {
            myFirstToggle = Is<MyFirstToggle>.Enabled;
        }

    }

Then in your view

    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        @if (Model.myFirstToggle) { 
            <p>This is my welcome message</p>
        }
    </div>