문제

Is it possible to do implicit constructor injection in F# using Ninject? If so, how?

I tried to put the [<Inject>] attribute on the type definition, but I got back an error that it's invalid.

Here is what I tried:

[<Inject>]
type Blah(x : ISword) =
도움이 되었습니까?

해결책

The Inject attribute is for Property setter injection only. Constructor injection is implicit. Just create your bindings and then make a kernel.Get<Blah>() and Blah is created using constructor injection.

다른 팁

The spec allows this as follows:

class-type-defn :=

    type-name primary-constr-args_opt object-val_opt '=' class class-type-body end

where

primary-constr-args :=

    attributes_opt accessopt (simple-pat, ... , simple-pat)

As a result, you just need to change your code to

type Blah [<Inject>](x : ISword) =

Here's Constructor Injection in F#:

type Foo(bar : IBar) =
    // class members etc. here

Any library that requires you to slap an attribute on the type in order to understand that, is doing something wrong; pick another library.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top