GithubHelp home page GithubHelp logo

Comments (9)

dustinmoris avatar dustinmoris commented on July 23, 2024 2

Now that you say that it is actually such an obvious change :). I am working on it (This is why I am hesitant to declare this library 1.0 too early, thanks!).

from giraffe.

dustinmoris avatar dustinmoris commented on July 23, 2024 2

Done: https://github.com/dustinmoris/Giraffe/releases/tag/v0.1.0-alpha017

Thanks as always!

from giraffe.

slang25 avatar slang25 commented on July 23, 2024 1

You could do something like this:

let someHander () =
    fun (ctx : HttpHandlerContext) ->
        async {
            let dbConfig = ctx.Services.GetService<IOptions<MyDbConfig>>()
            let dbConnectionString = dbConfig.ConnectionString
            // your code here
            return! ctx
        }

You can read about configuration and IOptions with DI here, which will explain how you get IOptions<MyDbConfig> registered.

You could use the HttpContext.RequestService but Giraffe already has an IServiceProvider on the HttpHandlerContext, I'd be interested to know who we couldn't just recommend the usage of HttpContext.RequestService. @dustinmoris ?

from giraffe.

nojaf avatar nojaf commented on July 23, 2024 1

Could the ILogger not be fetched from the HttpContext.RequestService as well?
Would it not make sense to transition from the HttpHandlerContext to the HttpContext?
If there a function getLogger, that might suffice as well

let getLogger (ctx: HttpContext) =
    ctx.RequestService<ILogger>()

let myHandler() =
    fun (ctx: HttpContext) ->
        async {
              let logger = getLogger ctx
             // do loggin
            return! ctx
        }

from giraffe.

dustinmoris avatar dustinmoris commented on July 23, 2024

Using the IServiceProvider from the HttpContext would make sense, was it always there? If yes then I must have missed it o.0

from giraffe.

slang25 avatar slang25 commented on July 23, 2024

It would be a big change, but given this is pre 1.0 that makes a lot of sense. @dustinmoris ?

from giraffe.

dustinmoris avatar dustinmoris commented on July 23, 2024

What's your opinion on helper method vs. extension method:

type HttpContext with
    member this.GetService<'T>() =
        this.RequestServices.GetService(typeof<'T>) :?> 'T

    member this.GetLogger<'T>() =
        this.GetService<ILogger<'T>>()

from giraffe.

nojaf avatar nojaf commented on July 23, 2024

Hmm, interesting. Extensions might be a better fit here.

from giraffe.

dustinmoris avatar dustinmoris commented on July 23, 2024

Ok cool, I had the same feeling, but wanted to get at least a second opinion.

from giraffe.

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.