基于继承的自定义模型绑定 [英] Inheritance based custom model binding

查看:81
本文介绍了基于继承的自定义模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个动作后听在ASP.NET MVC 4一堆的事件消息有很多活动,所以我不能创建每个事件的行动。所以,最好的办法是建立与他们的基类发布模型的操作使用自定义的模型绑定来滋润的事件。

I need to create a post action to listen to bunch of event messages in ASP.NET MVC 4. There are lots of events so I cannot create an action per event. So it would be best to create an action with their base class as posted model use a custom model binder to hydrate the event.

有些事件是这样的:

public class QueryID : Identity<Guid>
{
    public QueryID(Guid id)
    {
        Identifier = id;
    }
}

public class QueryEvent : IEvent<QueryID>
{
    public QueryEvent(QueryID id)
    {
        Identity = id;
    }

    #region Implementation of IEvent<out QueryID>

    public QueryID Identity { get; private set; }

    #endregion
}

public class QueryCreated : QueryEvent
{
    public string ConnectionID { get; private set; }

    public QueryCreated(QueryID id, string connectionID)
        : base(id)
    {
        ConnectionID = connectionID;
    }
}

public class ColumnAdded : QueryEvent
{
    public string Column { get; private set; }

    public ColumnAdded(QueryID id, string column)
        : base(id)
    {
        Column = column;
    }
}

的操作方法来接收这些消息看起来是这样的:

The action method to receive these messages would look something like this:

[HttpPost]
    public ActionResult Index(IEvent<IIdentity> e)
    {
        // whatever

        return new EmptyResult();
    }

基本身份类实现了 IEvent&LT; IIdentity的方式&gt; 接口

你会模型绑定模样做到这一点?

What would the model binder look like to accomplish this?

感谢

推荐答案

仅仅模型绑定不会帮助你在这里。该方法模型绑定工作是,框架着眼于行动预计参数的类型,创建该类型使用反射的默认实例和使用水化的形式发送的数据上它的每一个公共财产。

Mere model binding would not help you here. The way model binding works is, framework looks at the type of the parameter that actions expects, creates a default instance of that type using reflection and hydrates every public property on it using the data sent in the form.

在你的行动的参数是一个接口,因此框架将不能创建一个实例。它只是不知道要创建的接口的实现。

The parameter in your action is an interface and hence framework would fail to create an instance. It would just not know which implementation of the interface to create.

在一个实例中,我使用的形式的隐藏字段被张贴以指示该接口的实现要被绑定到的操作参数。但我有一个简单的场景。一个接口的两种实现。每一个对应于一种形式。所以我可以有一个隐藏提起告诉我,我希望我的模型绑定创建类的名称。

In one instance, I have used a hidden field in the form being posted to indicate which implementation of the interface is to be bound to the action parameter. But I had a simple scenario. One interface with two implementation. Each one corresponding to a form. So I could have a hidden filed tell me the name of the class that I wanted my model binder to create.

在你的情况下,如果事件(某些客户端应用程序我估计)的鼻祖知道应该调用哪个模型,那么你可以与其他信息一起发送到您的行动信息。

In you case, if the originator of the event (some client application I reckon) knows which model should be invoked, then you can have that information sent to your action along with other information.

不幸的是,我没有在目前的code。如果你觉得这个解决方案可以帮助,我可以挖掘出code。

Unfortunately I do not have the code at the moment. If you think this solution may help, I can dig out the code.

这篇关于基于继承的自定义模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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