RimDev.FeatureFlags icon indicating copy to clipboard operation
RimDev.FeatureFlags copied to clipboard

How to get the set connection string to the static options?

Open sturlath opened this issue 5 years ago • 3 comments

There are probably lots of ways to do this but how do you recommend doing this?

Like the code below?

     public class Startup
    {
        private static readonly FeatureFlagOptions options 
            = new FeatureFlagOptions().UseCachedSqlFeatureProvider(Configuration.GetConnectionString("connectionString"));

	public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

sturlath avatar Apr 18 '19 10:04 sturlath

Looks good to me!

kendaleiv avatar Apr 18 '19 12:04 kendaleiv

It looks good but the things is like this I get the following build error

A field initializer cannot reference the non-static field, method, or property 'Startup.Configuration'

when trying to use Configuration.GetConnectionString("connectionString") like I do in the example above.

But after some trial and error I managed to get this to work with with both sql and in-memory.

UseCachedSqlFeatureProvider

private readonly FeatureFlagOptions options;

public Startup(IConfiguration configuration, IHostingEnvironment env)
{
    Configuration = configuration;
    options = new FeatureFlagOptions()
    {
       FeatureFlagAssemblies = new[] {
        typeof(MyServiceLayerThatUsesthis).Assembly
       ,typeof(MyFeatureClassInMyCoreLib).Assembly 
    }}.UseCachedSqlFeatureProvider(Configuration.GetConnectionString("DefaultConnection"));
}

And staticly with UseInMemoryFeatureProvider

like this (with no need for a connection string)

public static readonly FeatureFlagOptions options = new FeatureFlagOptions
{
    FeatureFlagAssemblies = new[] {
        typeof(MyServiceLayerThatUsesthis).Assembly
       ,typeof(MyFeatureClassInMyCoreLib).Assembly 
    }}.UseInMemoryFeatureProvider();

And then using options like before.

Explanation of code

MyServiceLayerThatUsesthis == I use this in my servicelayer togling not in a Controller. MyFeatureClassInMyCoreLib == Since I need this both in my Web and servicelayer and my servicelayer should not know about web I keep this feature class in my core lib.

UseCachedSqlFeatureProvider

I would add some doc that lets us know that UseCachedSqlFeatureProvider creates a table RimDevAspNetCoreFeatureFlags where it stores the flags.

Question

But could you quickly tell me the difference between the two?

Thanks

But all in all I really like this. Thanks for creating it!

sturlath avatar Apr 18 '19 16:04 sturlath

UseInMemoryFeatureProvider shouldn't be used except during local development since the settings don't survive when the application is shutdown. That said, UseCachedSqlFeatureProvider is the only viable option currently for actual use.

Updating sample code to use IConfiguration is a good idea.

kendaleiv avatar Apr 23 '19 13:04 kendaleiv