您可以使用IOC容器将依赖项注入到自定义WebViewPage的构造函数中吗? [英] Can you inject dependencies into a constructor of a custom WebViewPage, using an IOC container?

查看:64
本文介绍了您可以使用IOC容器将依赖项注入到自定义WebViewPage的构造函数中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC 3中,我了解您可以创建自定义WebViewPages.您可以使用构造函数注入通过IOC容器注入依赖项吗?

In MVC 3, I understand you can create custom WebViewPages. Can you inject dependencies, using constructor injection, via an IOC container?

推荐答案

Brad Wilson

There is an expample for view injection in a blog post by Brad Wilson http://bradwilson.typepad.com/blog/2010/07/service-location-pt3-views.html

其他人的观点认为,视图允许构造函数注入不完全正确.是IDependencyResolver允许创建具有构造函数参数的视图.但是,除非您实现自己的视图引擎,否则这对您完全没有帮助.现有的视图引擎(如razor)将要求您具有无参数的构造函数.这意味着您只能使用它们对视图进行属性注入.

The statements of the others that views allow constructor injection not entirely correct. Yes IDependencyResolver enables creating views that have constructor arguments. But unless you are implementing your own view engine this won't help you at all. Existing view engines like razor will require that you have a parameterless constructor. This means you can do only property injection on views with them.

但是正如其他人所说,您无论如何都不应该查看注入.您的视图应该是愚蠢的,只需将视图模型呈现为HTML.任何需要依赖项的操作都应在控制器或服务中完成.

But as the others said you shouldn't do view injection anyway. Your view should be dumb and just render the view model to HTML. Anything requiring a dependency should be done in the controller or a service.

这篇关于您可以使用IOC容器将依赖项注入到自定义WebViewPage的构造函数中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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