问题
How is it possible to use Ninject inside ASP.NET MVC 3 to instantiate objects manually? Something as
"NinjectObject".Resolve<IMyService>();
Thank you & regards
回答1:
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.
来源:https://stackoverflow.com/questions/10765424/how-to-manually-instantiate-objects-using-ninject-for-mvc-3