ASP.NET MVC Unity - 在模型层注入
我看到了大量有关如何使用 ControllerBuilder.Current.SetControllerFactory 注入服务的材料,但是如果我想解析模型中的服务该怎么办?我是否必须从控制器层获取它们并将它们传递出去?
I see tons of material on how to inject services using the ControllerBuilder.Current.SetControllerFactory
but what if I wanted to resolve my services in the model? Would I have to get them from the Controller layer and pass them up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理想情况下,您不应将服务注入模型,因为这需要您向容器注册模型。
如果您需要在模型实例中使用服务,请将服务作为方法参数传入,然后可以将服务注入到控制器中。
在不了解更多情况的情况下,很难给出更清晰的建议,但以下概述可能会有所帮助:
Ideally you should not be injecting services into the model as this would require you to register your model with the container.
If you need to use a service within a model instance, pass the service in as a method argument and then you can inject the service into the controller.
Without knowing more about the scenario it is hard to give clearer advice, however the following outline may help: