Mau*_*fer 14
我不认为在F#中使用Ninject有什么特别之处.Samurai/IWeapon/Sword的Ninject示例如下所示:
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
Run Code Online (Sandbox Code Playgroud)
像隐式构造函数和类型推断这样的F#特性使代码非常简洁.
| 归档时间: |
|
| 查看次数: |
651 次 |
| 最近记录: |