简体   繁体   中英

F# Ninject Constructor Injection

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 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) =

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM