Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sturlath opened this issue Apr 18, 2019 · 3 comments
Open

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

sturlath opened this issue Apr 18, 2019 · 3 comments

Comments

@sturlath
Copy link

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; }
@kendaleiv
Copy link
Contributor

Looks good to me!

@sturlath
Copy link
Author

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!

@kendaleiv
Copy link
Contributor

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 added a commit to kendaleiv/RimDev.FeatureFlags that referenced this issue Apr 23, 2019
kendaleiv added a commit to kendaleiv/RimDev.FeatureFlags that referenced this issue Apr 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants