GithubHelp home page GithubHelp logo

Comments (8)

davidfowl avatar davidfowl commented on August 30, 2024

Why do you want to use IOptions in the first place? Are are you saying that you want something else for pocos other than just putting instances in the container?

from options.

jods4 avatar jods4 commented on August 30, 2024

@davidfowl I don't want to use IOptions! But I got the impression from the docs that it was the suggested way.
There are good points, though.
Once you do app.Configure<AppSettings>(Configuration.GetSection("AppSettings")), you're ready to go:

  1. The settings are taken from the configuration system (any source you have configured)
  2. They are parsed into a strongly typed class for you
  3. They are accessible pretty much anywhere with the DI.

I guess I would just like to be able to DI AppSettings directly, rather than IOptions<AppSettings>, which does not make a whole lot of sense (in this context at least).

Do you suggest another way?
Previously in ASP.NET, the "way" to do that was a designer in Properties that generated a strongly typed singleton class (usually MyNamespace.Properties.Settings).

from options.

lodejard avatar lodejard commented on August 30, 2024

You're not required to use IOptions if you want to bind an object from config directly... You might need to add dependency on "Microsoft.Framework.Configuration.Binder": "1.0.0-*". For example:

    public class Startup
    {
        private readonly IConfiguration Configuration;

        public Startup(IApplicationEnvironment env)
        {
            Configuration = new ConfigurationBuilder(env.ApplicationBasePath)
                .AddJsonFile("config.json")
                .Build();
        }

        public void ConfigureServices(IServiceCollection services)
        {
            var settings = new MyNamespace.Properties.Settings();
            Configuration.GetSection("appSettings").Bind(settings);
            services.AddInstance(settings);
        }

Is that closer to the code you're prefer to have? The settings class can be used as a service or controller dependency directly at that point.

from options.

divega avatar divega commented on August 30, 2024

FWIW, this terser syntax should already work with nightly builds:

    var settings = Configuration.Get<MyNamespace.Properties.Settings>("appSettings");

from options.

jods4 avatar jods4 commented on August 30, 2024

@lodejard yes, that would do the trick.
@divega and the new typed Gets (very good addition BTW) make it a lot cleaner!

Yet I somehow still believe the method that does those two (or three) lines of code should be in the framework. Maybe services.AddSettings<AppSettings>("appSettings").
Or just services.AddSettings<AppSettings>() and the section name might be in an attribute.

First because application settings is a fundamental thing. Every serious applications needs it. The framework should provide guidance on how to handle them, in an easily discoverable way.

Second, right now the helper method is trivial, that's true. But in the future you might want to include more features:

  • transparent encryption (ok that would probably entirely be at the Configuration level?)
  • save changes (by modifying Settings class directly, not through Configuration)
  • live reload (because of thread safety you can't modify the Settings object directly, but you could replace the one in the DI for new services, and if it implements an ISettingsChanged interface or something, you could call a notification event so that part of the code that already have the settings may get the new ones).
  • decide to merge app-wide settings and user-wide settings (that was in .NET of old times). It's not useful for ASP.NET, but if we start using .NET Core to build cross-platform console applications (or even x-plat UI) then it's needed.

Overall there is a lot of overlap between Configuration and what I call Settings, so maybe the former should be used for that latter. But currently it's a bit complicated for that.

from options.

davidfowl avatar davidfowl commented on August 30, 2024

@jods4 Seems like your initial problem is solved. If you want to have a more general discussion about configuration or request specific features, I'd suggest opening individual issues.

from options.

jods4 avatar jods4 commented on August 30, 2024

@davidfowl I opened this issue specifically to request simpler API for handling application settings.

I don't have a problem per se as everything works, so if you don't want to pursue this suggestion further feel free to close the issue.

from options.

davidfowl avatar davidfowl commented on August 30, 2024

Great!

from options.

Related Issues (20)

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.