简单的构造函数后MVC参数后调用初始化绑定 [英] Call Initialize after simple ctor and after MVC parameters are bound

查看:132
本文介绍了简单的构造函数后MVC参数后调用初始化绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用autofac和MVC一起,让我自动解决视图模型。这种运作良好,虽然我有一个问题,我需要被解决后,调用一个方法Initializable对每个视图模式。

I am using autofac and MVC together to allow me to auto resolve view models. This works well although I have an issue where I need to call an Initializable method for every view model after it is resolved.

我简单地钩住OnActivated方法autofac内解决了这个问题。

I solved this issue by simply hooking into the OnActivated method within autofac.

这里唯一的问题是,对MVC已结合其参数到视图模型中的属性之前初始化方法被调用,因此造成局部初始化

The only issue here is that the Initialize method is called before the MVC has bound its parameters to the properties within the view model, therefore resulting in partial initialization.

我可以通过添加调用(从autofac删除)在我的我的自定义模型绑定器的OnModelUpdated覆盖初始化解决这个问题,虽然现在如果我创建MVC之外的视图模型绑定也不会叫初始化。

I can solve this by adding the call (removing from autofac) to Initialize in my OnModelUpdated override of my custom ModelBinder, although now if I create a view model outside the MVC binding it won't call initialize.

我能看到的唯一的解决办法是把它在这两个地方,Autofac(OnActivated),并在我的模型绑定器(OnModelUpdated),这将是一个巨大的性能损失肯定,任何想法?

The only solution I can see is to have it in both places, Autofac (OnActivated) and in my modelbinder (OnModelUpdated), this will be a tremendous performance hit surely, any ideas?

推荐答案

我没有足够的代表处点发表评论,所以我问在这里,我想知道,如果你需要呼吁得到INITIALISE和/或POST如果两个为什么?通常情况下我有一个GET因为INITIALISE很可能意味着我们pre-填充某些属性。而邮政向你走来,你很可能会执行一些操作的信息。

I don't have enough rep points to comment, so I'm asking here, I was wondering if you need to call the initialise on GET and/or POST and if both why? Typically I have an initialise for GET because it likely means we're pre-populating some properties. Whereas the POST has information coming to you which you'll likely perform some action on.

两种解决方案我会考虑:

Two solutions I'd consider:

1)让事情简单一些,并呼吁从操作方法INITIALISE方法。如上所述我这样做的GET请求,而不是职位,但没有理由你不能两者都做,如果这是你的需要。

1) Keep things simple and call the initialise method from the action method. As mentioned above I do this for GET requests, not for POSTs, but no reason why you can't do both if that's your need.

2)由于您使用Autofac,你可以使用自定义操作调用和Autofac注册。这将使您的DI +模式,同时结合让你现有的事件想法应该工作。 code可以在我的问题中找到<一个href=\"http://stackoverflow.com/questions/28260285/autofac-action-injection-with-asp-net-mvc-model-binding/28379311\">here.

2) Since you're using Autofac, you could use a custom action invoker and register it with Autofac. That would give you your DI + model binding at the same time so your existing event idea should work. Code can be found in my question here.

这篇关于简单的构造函数后MVC参数后调用初始化绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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