SaturnFramework / Saturn

Opinionated, web development framework for F# which implements the server-side, functional MVC pattern
https://saturnframework.org
MIT License
707 stars 108 forks source link

Improve appsettings.env.json integration with FSharp.Data JsonProvider #251

Closed jkone27 closed 4 years ago

jkone27 commented 4 years ago

I personally love this, if possible use FSharp.Data JsonProvider for appsettings.env.json files for aspnetcore, that gives a big advantage of not having to type new stuff every time a setting is added, also useful in industry and quite common scenario (which already show advantages over e.g. C#)

Then registering a single AppSettings type as singleton can provide many benefits for the app, not having to adjust every time a new setting is changed

I Still haven't figured out how to use it as option, since JSonProvider provided types are erased, cannot be used for doing

services.AddConfiguration<AppSettings>(configuration) //does not work

example of some code i did (probably there is a better way, but this for me is already quite nice as results):

//somewhere
open FSharp.Data

type AppSettingsProvider = JsonProvider<"appsettings.json">

type AppSettings = AppSettingsProvider.Root

//in Startup: configuration using FSharp.Data type provider
let settingsFile = "appsettings." + Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") + ".json";
let config = AppSettingsProvider.Load(settingsFile)

//add once and never change (just add changes in appsettings.json!!!)
services.AddSingleton<AppSettings>(config) |> ignore

https://github.com/jkone27/FsharpWebApi/blob/master/FSharp/Startup.fs

Krzysztof-Cieslak commented 4 years ago

We're not planning to get a direct dependency on any type provider in Saturn. But please, feel free to continue using your method if it fits your projects - it's not something I want to push on all users of Saturn.

Thanks for the suggestion!

jkone27 commented 4 years ago

A pity, i think type providers should be a first choice for F# development, since they make it uncomparable to other languages as a development advantage. Every time data changes, developers spend time adjusting and trimming data contracts, type providers would be just a huge time saver for this, especially in enterprise environment