使用在每次注册的基础上注册依赖项
DependsOn
似乎在F#中不起作用——我是不是错过了什么?
例如,这将不起作用(解析错误,等待依赖关系
IChild
未注册):
module Program
open System
open Castle.Windsor
open Castle.MicroKernel.Registration
type IChild =
interface end
type IParent =
interface end
type Child () =
interface IChild
type Parent (child : IChild) =
interface IParent
[<EntryPoint>]
let main _ =
let dependency = Dependency.OnValue<IChild> (Child ())
use container = new WindsorContainer ()
container.Register (
Component.For<IParent>().ImplementedBy<Parent>().DependsOn(dependency)
) |> ignore
let parent = container.Resolve<IParent> () //Exception due to missing dependency
0
然而,在容器中全局注册类型可以很好地工作,例如。
module Program
open System
open Castle.Windsor
open Castle.MicroKernel.Registration
type IChild =
interface end
type IParent =
interface end
type Child () =
interface IChild
type Parent (child : IChild) =
interface IParent
[<EntryPoint>]
let main _ =
use container = new WindsorContainer ()
container
.Register(Component.For<IChild>().ImplementedBy<Child>())
.Register(Component.For<IParent>().ImplementedBy<Parent>())
|> ignore
let parent = container.Resolve<IParent> () //Works as expected
0
我看不出由
Dependency.OnValue
在C#和F#中。