Bill
Bill

Reputation: 2362

How to manually instantiate objects using Ninject for MVC 3

How is it possible to use Ninject inside ASP.NET MVC 3 to instantiate objects manually? Something as

"NinjectObject".Resolve<IMyService>();

Thank you & regards

Upvotes: 3

Views: 4155

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039248

It is better to inject dependencies instead of resolving them. Service Locator is an anti-pattern. You could for example use the following:

IMyService myService = DependencyResolver.Current.GetService<IMyService>();

But please do not use it. That's an anti-pattern.

Dependency injection is the preferred way. You should have the constructor of the class that needs this dependency take an IMyService instead of having the class fetch this dependency.

Upvotes: 14

Related Questions