Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject and F#?

Tags:

f#

ninject

Has anyone used Ninject in F# code? any hint is appreciated.

like image 279
amirmonshi Avatar asked Feb 26 '11 15:02

amirmonshi


1 Answers

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.

like image 175
Mauricio Scheffer Avatar answered Oct 22 '22 20:10

Mauricio Scheffer