Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject: What does it mean to bind something to itself?

Ninject has the functionality of self binding like Bind<Samurai>().ToSelf();

I read about this but I don't get the importance or how this can be useful. Any comments are appreciated.

like image 883
Cemre Mengü Avatar asked May 09 '14 07:05

Cemre Mengü


People also ask

What is ninject?

Bend software to your will Stop writing monolithic applications that make you feel like you have to move mountains to make the simplest of changes. Ninject helps you use the technique of dependency injection to break your applications into loosely-coupled, highly-cohesive components, and then glue them back together in...

How does ninject resolve the entire chain of dependencies?

The code is creating a Ninject Kernel that resolves our entire chain of dependencies. We tell Ninject to load the bindings from the executing assembly. This is the most common scenario. In this case, your Bindings class should live in one of the assemblies included in your executing project.

What is the meaning of bind?

1 a : to make secure by tying His hands were bound with rope. d : to constrain with legal authority The court's decision binds them to pay the fine. 2 a : to wrap around with something so as to enclose or cover A silk sash bound her waist. 6 : constipate Cheese tends to bind him.

How to bind someone’s name?

Amplify this type of ritual by sprinkling fidelity and passionate herbs over the names before binding inside. For binding protection rituals, choose simple supplies like photos, personal effects, ribbon and small boxes. These can be used to symbolically bind the person from doing harm.


2 Answers

If Ninject finds a object that needs to be created and it has a constructor that has a Samurai parameter it does not know how to instantiate it.

But when you use Bind<Samurai>().ToSelf(); then Ninject knows that a Samurai needs to be created to pass to the Samurai parameter.

If that binding was not there then ninject didn't know what to pass, for example there might have been a SamuraiSubClass type. But by explicitly saying that when Ninject finds a Samurai parameter that it needs to create a Samurai for that parameter then Ninject knows exactly what to do.

like image 93
SynerCoder Avatar answered Oct 11 '22 17:10

SynerCoder


I use the .WithConstructorArgument() quite a bit. Bind<Samurai>().ToSelf().WithConstructorArgument("owner", user); Just a nice way to provide an object(s) to your Samuari constructor when it is being injected.

like image 21
Eric Avatar answered Oct 11 '22 16:10

Eric