Castle Windsor - DependsOn在F#工作?

时间:2013-10-22 08:05:15

标签: f# castle-windsor

使用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 

我在C#和F#中Dependency.OnValue创建的属性之间没有明显区别。

1 个答案:

答案 0 :(得分:1)

正如Mauricio Scheffer所指出的,问题是Dependency.OnValue<T>返回一个属性,而F#不会自动使用属性上定义的隐式转换来调用DependsOn(Dependency)。相反,它会调用DependsOn(obj),这意味着用于匿名类型。

更改代码以便像这样创建依赖项可以解决问题:

let dependency = Property.op_Implicit(Dependency.OnValue<IChild>(Child ()))