52

Can anyone tell me if i can register an already created instance of a class with Ninject so that it will use this instance everytime it needs injecting?

I suppose you can call it a singleton but I have the instance already created. All the documentation points to creating new instances of a class.

mipe34
  • 5,596
  • 3
  • 26
  • 38
Martin
  • 23,844
  • 55
  • 201
  • 327
  • .InSingletonScope() is what you're after. It creates a single instance and punches it into everything that needs it, because that's how it rolls. – Patrick Magee Jun 04 '13 at 11:59
  • 3
    Thanks, but InSingleScope creates the instance, i already have the instance, it has been initialized with quite a few properties.... so i need to register that specific instance... Do you have an example.? – Martin Jun 04 '13 at 12:10
  • Remember, you should be building a dependency graph, you should not be newing up any objects, the container should know about them all prior to your application running, so if you're newing up objects and then wanting to somehow reference them in some other area of your application, that sounds like some kind of anti-pattern like Service Locator, in which you would be grabbing instances from the Kernel everywhere. Unless I'm misunderstanding something, sorry if I am :) – Patrick Magee Jun 04 '13 at 12:25
  • If something is 'already instanced' that implies that something was created NOT by Ninject or in the kernel, so this is hard to understand. – Patrick Magee Jun 04 '13 at 12:27
  • Currently I also don't need access to the IKernel from anywhere as each object is being injected with its needed dependencies. The only this i have to do in Main (its a console app).. is do var process = kernel.Get(); and call .Perform ... this starts my first object instance from Kernel and then every other instance is being injected via the object graph... through there constructors. – Martin Jun 05 '13 at 09:27
  • I get that. So what's the issue you are having then, I'm confused. I thought @nemesv solves your issue. – Patrick Magee Jun 05 '13 at 09:50
  • This is a valid problem because sometimes you don't get the flexibility to create an object yourself (or to allow Ninject to create it) -- but some crazy API gives you an object, and if you (or Ninject) created a new one, it would be a broken / non-functional object. -- Additionally, sometimes the runtime creates and manages certain objects, and you can't have Ninject just newing up a new one every time you need access to it. – BrainSlugs83 Dec 07 '16 at 20:35

1 Answers1

80

You can use the ToConstant method which takes an already existing instance and registers it as singleton.

var kernel = new StandardKernel();
kernel.Bind<MyClass>().ToConstant(myClassInstance);

If you want to something more complex you can use the ToMethod (where you can use a Func to get your instance) combined with the InSingletonScope

var kernel = new StandardKernel();
kernel.Bind<MyClass>().ToMethod(context => myClassInstance).InSingletonScope();
dance2die
  • 35,807
  • 39
  • 131
  • 194
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • 1
    Nice, in which situations would someone be binding an instance that was not already created in some NinjectModule? Seems a little weird to me, unless the bindings can not be done for some reason at the beginning of the application and may require some trickery done else where. Never used .ToConstant, seems helpful for tough situations :) – Patrick Magee Jun 04 '13 at 12:30
  • Hi patrick, I have an instance of an object given to me a the start of the application by a third party component, this instance is populated with properties, if I simply newed up this object or had a ninject module new it up, even though it would be a singleton, I would not have all the correct properties. But I am open to advise. I am currently passing in my object to a constructor of a ninject module i have created and using ninject to register than instance as per nemesv. – Martin Jun 05 '13 at 09:22
  • 2
    This should solve your issue as Martin has given you a good example of how to already pass through your instanced object :) – Patrick Magee Jun 05 '13 at 09:53