Neb
Neb

Reputation: 354

Ninject singleton init

Im using Ninject 3.0.1.10 and I created a Ninject Module:

Public Class NinjectCustomModule
    Inherits NinjectModule

    Public Overrides Sub Load()
        'init
    End Sub

End Class

Now let's suppose I got an instance of Class A in my program and I pass it to the constructor of the module:

Public Class NinjectCustomModule
    Inherits NinjectModule

    Private _Obj As ClassA

    Public Sub New(ByVal Obj As ClassA)
        _Obj = Obj
    End Sub

    Public Overrides Sub Load()
        'init
    End Sub


End Class

I know how to initialize a Singleton of Class A:

Bind(Of ClassA).ToSelf().InSingletonScope()

How to bind a Singleton of Class A wich refer to the instance that I already have (_Obj in my example)?

Also, what if ClassA is an interface?

Upvotes: 3

Views: 250

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 190897

Try

Bind(Of ClassA).ToConstant(instanceOfClassA)

Upvotes: 3

Related Questions