如何手动使用实例化对象Ninject为MVC 3 [英] How to manually instantiate objects using Ninject for MVC 3

查看:151
本文介绍了如何手动使用实例化对象Ninject为MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这怎么可能使用Ninject ASP.NET MVC 3内手动实例化对象?有些事情是如此

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

"NinjectObject".Resolve<IMyService>();

感谢您和放大器;问候

推荐答案

这是更好地注入依赖,而不是解决这些问题的。服务定位器是一个反模式。例如,你可以使用以下命令:

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.

依赖注入是preferred方式。你应该有一个需要这种依赖性采取类的构造函数的 IMyService ,而不是具有类获取这种依赖性。

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.

这篇关于如何手动使用实例化对象Ninject为MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆