SaturnFramework / Saturn

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

Provide IConfiguration as a parameter to use_config #151

Closed toburger closed 5 years ago

toburger commented 5 years ago

It would be useful when the use_config function of the application computation expression provides access to the Microsoft.Extensions.Configuration.IConfiguration object or the HttpContext itself.

I wanted to read the application settings from ASP.NET Core and put it in a Settings record, but my solution is pretty ugly.

let configureContextAccessor (services: IServiceCollection) =
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>()

let setupConfiguration () =
    let ctx = HttpContextAccessor()
    let config = ctx.HttpContext.GetService<IConfiguration>()
    { SampleSetting = config.["sampleSetting"] }

let app = application {
    ...
    service_config configureContextAccessor
    use_config setupConfiguration
    ...
}

I couldn't find a nicer way to access the IConfiguration object.

Krzysztof-Cieslak commented 5 years ago

Yes, currently accessing IConfiguration on the application level is a problem. I think I'd be open to changing use_config to take IConfiguration -> 'T as a parameter, instead of just 'T

Krzysztof-Cieslak commented 5 years ago

Fixed in https://github.com/SaturnFramework/Saturn/pull/157