在每个注册的基础上注册依赖项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# 中创建的属性之间没有明显的区别。