Has anyone used Ninject in F# code? any hint is appreciated.
I don't think there's anything particular about using Ninject in F#. The Ninject example of Samurai / IWeapon / Sword looks like this:
open Ninject
type IWeapon =
abstract Hit: string -> unit
type Sword() =
interface IWeapon with
member x.Hit s = printfn "Slash %s" s
type Samurai(weapon: IWeapon) =
member x.Attack target =
weapon.Hit target
[<EntryPoint>]
let main args =
use kernel = new StandardKernel()
kernel.Bind<IWeapon>().To<Sword>() |> ignore
kernel.Bind<Samurai>().ToSelf() |> ignore
let samurai = kernel.Get<Samurai>()
samurai.Attack "enemy"
0
F# features like implicit constructors and type inference make the code quite concise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With