2

Is there an easy way to concatenate configuration to avoid redundancy?

  "Directories": {
    "Root": "my\\root\\folder",
    "Log": "{Root}\\Log",
    "Data": "{Root}\\Data" 
  }

Instead of writing the full path of each directory, I can pass {Root} as a variable. This way, users just need to update 1 configuration line instead of all 3.

When I call Configuration["Directories:Log"], it returns as my\\root\\folder\\Log. Same thing with Data and other possible combinations. Basically if possible, I want to use other configuration as a variable inside the configuration file (appsettings.json).

dfox
  • 81
  • 2
  • 9
  • 1
    Short answer: NO. That is however, an implementation concern that you can implement yourself in a service. – Nkosi Jun 25 '19 at 02:22

1 Answers1

4

Create a class to represent your settings and then methods that would do what you want, e.g. the below. Can do it in a .NET Core console app too doesn't have to be ASP.NET Core.

    public class YourSettings
    {
        public string Root { get; set; }
        public string Log { get; set; }
        public string Data { get; set; }

        public string LogPath => Log.Replace("{Root}", Root);
        public string DataPath => Data.Replace("{Root}", Root);
    }

Then register it:

    public IConfiguration Configuration { get; }

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

    public void ConfigureServices(IServiceCollection services)
    {
       ...
       // Configure Options using Microsoft.Extensions.Options.ConfigurationExtensions
        services.Configure<YourSettings>(Configuration.GetSection(nameof(YourSettings))); // for your specific example just pass in the string "Directories" since you don't have a section called "YourSettings" - obviously just update the class to encompass whatever settings you want it to
        services.AddSingleton(Configuration); //for DI
       ...
    }

Then use it:

    private readonly YourSettings _settings;
    public YourController(IOptions<YourSettings> settings)
    {
        _spDataRepo = spDataRepo;
        _settings = settings.Value;
        DoStuffWithSettings();
    }

    public void DoStuffWithSettings()
    {
        Debug.Print($"Hey the logs are here: {_settings.LogPath}");
    }
Mark Z.
  • 2,127
  • 16
  • 33
  • Alternatively (and more tediously and less straightforward to others), you could write an MSBuild task to do this, see the solution BELOW the answer here: https://stackoverflow.com/questions/7837644/how-to-replace-string-in-file-using-msbuild – Mark Z. Jun 25 '19 at 03:25
  • Thanks for the suggestion Mark, I'm actually looking for a more dynamic approach. I just use some basic example but the "real" configuration is more complicated. I'd like having to call Configuration["config:path"] instead of strongly typed ```_settings.LogPath```. – dfox Jun 26 '19 at 01:04
  • @dfox afraid I don't follow - config shouldn't be too dynamic - but even so if types are the concern (which is strange) then just use object instead of a specific type in the C# class that represents your config. – Mark Z. Jun 27 '19 at 15:17