开发者

Ninject and F#?

Has anyone used Ninject in F# code? any hint is 开发者_如何学Goappreciated.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜