MVC4 / AutoFac依赖注入的视图模型 [英] MVC4/AutoFac dependency injection in ViewModel

查看:351
本文介绍了MVC4 / AutoFac依赖注入的视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有DI使用我们的应用程序MVC4的构造模式实现,它已经完美的一对夫妇几年了工作。

I have DI implemented using the constructor pattern in our MVC4 application, it's been working perfectly for a couple of years now.

最近,我们有一个承包商进来做一些工作。他需要访问我们的服务层视图模型和硬codeD在视图模型,坏,坏,坏的。

Recently, we had a contractor come in the do some work. He needed to access our services layer in a view model and hard coded the services (and their repositories) in the constructor of the viewModel, bad, bad, bad.

他做的基本上是搞乱我们的我们的工作我们击打了实现和维护的松耦合。

What he did was basically mess up our loose coupling we worked our buts off to implement and maintain.

时间,现在$ P $从重新写他的code此时pvents我们。

Timing right now prevents us from re-writing his code at this time.

我们的的ViewModels是在单独的程序/项目比MVC应用程序一样,都是我们的服务层和我们的代码库层。

Our ViewModels are in a separate assembly/project than the MVC application, as are our services layer and our repository layer.

到目前为止好。

如何使用依赖注入来解决视图模型里面我的服务。

How can i used dependency injection to resolve my services inside a view model.

我再说一遍,内部的视图模式,而不是INSIDE控制器!如果使用属性注入模型或构造注入模型并不重要,我。

I say again, INSIDE A VIEW MODEL, NOT INSIDE THE CONTROLLER!! Doesn't matter to me if it uses the property injection model or constructor injection model.

在此先感谢的任何援助。

Thanks in advance for any assistance.

推荐答案

这只是普通的硬盘。如你所知,其实你说:在最简单的层次国际奥委会和DI使用类似Autofac是关于构造函数注入或财产注入。这种注入开始在树的顶端,和碰巧一路下跌,通过构造函数本身依赖链(C需要的电压,因此IV注入; V需要ř所以IR注入等)。

This is just plain hard. As you know, in fact you stated: at the simplest level IoC and DI using something like Autofac is about constructor injection or property injection. This injection starts at the top of the tree, and happens all the way down the dependency chain through the constructors themselves (C requires V, so IV is injected; V requires R so IR is injected etc.).

构造和财产注射是基本相同的事情。所不同的通常只是关于构建函数参数膨胀(我说,并强调,一般的)。我还以为你不希望使用服务定位器,因为你已经把只用注射的辛勤工作。

Constructor and Property injections are basically the same thing. The difference is usually just about parameter bloat in ctors (I say, and emphasise, usually). I also assume you don't want to use a service locator, due to all the hard work you have put in to only use injection.

最简单的一部分,因此注册VM类型的Autofac,让你注入VM构造函数或属性。那么最难的是虚拟机实例在创建虚拟机的类(控制器或其他方式),在不Autofac引用硬编码,并使得你保持你的可测性的一种方式。

The easy part is therefore registering the VM types with Autofac to allow you to inject into the VM ctor or properties. The hard part is then instantiating VMs in the classes (controllers or otherwise) where you create the VMs, without hardcoding in Autofac references, and in such a way that you maintain your testability.

不过,我因此不能看你怎么可以做脱身的部分的微小变化,以您的控制器的构造函数(特别是创建的ViewModels如果这些都不是控制器类):他们需要或者模板视图模型通过构造函数(eugh)为您注射,然后垫了实际价值,或者他们需要一个工厂或发电机注入,让它们来创建它们依次注入的ViewModels。还有其他的事情,你必须做的就是这个工作:它只是凌乱

However I therefore can't see how you can get away from doing some minor change to your Controller's constructors (specifically the classes that create the ViewModels if these are not the Controllers): they need either a template ViewModel injected through the constructor (eugh) for you to then pad out with actual values, or they need a "factory" or "generator" injected to allow them to create viewmodels which are in turn injected. There are other things you would have to do to get this to work: it is just messy.

您可以使用A需要创建的B实例关系类型,即注入 Func键< TViewModel> 入您的ViewModels的实例化器相关的方法来允许你创建时,你必须(也允许您创建在许多虚拟机一个的情况下名单,LT; TViewModel> )。这将注入服务到VM构造函数没有你知道关于注射。

Could you use the "A needs to create instance of B" relationship type, i.e. inject a Func<TViewModel> into the relevant methods in your instantiators of viewmodels to allow you to create when you have to (also allowing you to create many VMs in the case of a List<TViewModel>). This will inject services into the VM ctor without you knowing about the injection.

动态实例化含义在这里将涉及最少的改变你的实际控制人的构造函数,并会保留相同的寿命范围,有您的控制器使用的服务。

The Dynamic Instantiation "Meaning" here would involve minimal changes to the constructors of your actual controllers, and would retain the same lifetime scopes as the services your controller uses.

还要注意的是Autofac可以让你使用 ExtensibleActionInvoker IActionInvoker 注入参数到控制器操作方法,因此,如果您与任何的这个讨厌的控制器构造函数膨胀结束了,你可以注入 Func键&LT; TViewModel方式&gt; 进入行动,而不是,作为最后的手段。

Note also that Autofac lets you inject parameters into Controller Action methods using the ExtensibleActionInvoker as an IActionInvoker so if you end up with nasty Controller ctor bloat with any of this, you could inject the Func<TViewModel> into the Action instead, as a last resort.

这是非此即彼不改变你的控制器接口(包括构造函数),而是求助于的服务定位器使用Autofac 来创建你需要实例化的ViewModels的方法中注入的依赖关系的ViewModels。

It's either this or don't change your controller interface (including the constructor), but resort to a Service Locator using Autofac to create ViewModels with the dependencies injected within the methods where you need to instantiate ViewModels.

这一切都不回答了另一个问题:怎么样传递从客户端回邮行动作为一个参数一个视图模型。这可能是与Autofac注册您的视图模型类型会照顾了你,但我从来没有需要去尝试并测试它。如果没有,你将不得不使用您的模板VM,VM或函数功能,与依赖注入,然后转移到模板复制实际的虚拟机属性。

None of this answers the other problem: what about a ViewModel that is passed back from the client into the Post Action as a parameter. It is possible that registering your ViewModel types with Autofac will take care of that for you, but I have never had the need to try it and test it. If it doesn't you will have to use your template VM, or VM Func, with dependencies injected, and then copy the actual VM properties over to the template.

这篇关于MVC4 / AutoFac依赖注入的视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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