什么是" Func键<对象> modelAccessor"在MVC的DataAnnotationsModelMetadataProvider参数? [英] What is the "Func<object> modelAccessor" parameter for in MVC's DataAnnotationsModelMetadataProvider?

查看:115
本文介绍了什么是" Func键<对象> modelAccessor"在MVC的DataAnnotationsModelMetadataProvider参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是提供给CreateMetadata方法(你如果覆盖扩展元数据支持)的参数之一。

It's one of the parameters supplied to the CreateMetadata method (which you override if extending metadata support).

ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes,
                             Type containerType,
                             Func<object> modelAccessor, <<--THIS ONE
                             Type modelType,
                             string propertyName)

我曾以为,它允许您访问模型对象本身(例如设置基于模型值的元数据),但是当我尝试使用它投给我的模型对象我只是得到空。

I had assumed that it allowed you to access the model object itself (e.g. for setting metadata based on model values), however when I try to use it to cast to my model object I just get null.

Entity ent = (Entity)modelAccessor(); // = Null

如果我missunderstood,任何人都可以解释它的目的是什么?或者可以选择,如何正确使用它呢?

If I've missunderstood, can anyone explain what it's purpose is? Or alternatively, how to properly use it?

感谢

推荐答案

我们原本认为是对象模型,而不是FUNC modelAccessor。我们不得不在MVC 2的船周期的后期更改。

We originally had that as "object model", rather than "Func modelAccessor". We had to change it late in MVC 2's ship cycle.

是为了延缓检索模型的实际值,直到这样的点,你知道你会需要它(也就是直到你调用ModelMetadata.Model)。

The purpose is to delay retrieving the actual value of the model until such point as you know you're going to need it (that is, until you call ModelMetadata.Model).

它解决了这个问题实际上是一个相当深奥的一个模型针对的LINQ to SQL类中有一个外键引用绑定关系。问题是,如果你已经检索到的子对象,它是通过再一个外键关系psented $ P $(这通常意味着该对象的延迟加载),那么你就不再允许通过选择一个新的子对象设置外键ID属性。这是很常见的模型绑定外键ID(而不是整个外键的实体),当模型绑定,但是如果我们想要检索的外键的实体对象(填充ModelMetadata类的目的),那么绑定将不再是合法的,并且实际上抛出异常。由于ModelMetadata用于模型两个方向 - 入,通过模型绑定,和出站,通过HTML生成 - 我们需要引入间接层来保护你使用它在这两种情况下不中断的LINQ to SQL的规则的能力。

The problem it solves is actually a rather esoteric one related to model binding against a LINQ to SQL class that has a foreign key reference in it. The problem is, if you've retrieved the child object which is represented by a foreign key relationship (which usually means a delay load of that object), then you're no longer allowed to choose a new child object by setting the foreign key ID property. It's very common to model bind the foreign key ID (and not the whole foreign key entity) when model binding, but if we'd retrieved the foreign key entity object (for the purposes of populating the ModelMetadata class) then that binding would no longer be legal, and actually throw an exception. Since ModelMetadata is used for both directions of models -- inbound, via model binding, and outbound, via HTML generation -- we needed to introduce the layer of indirection to protect your ability to use it in both scenarios without disrupting LINQ to SQL's rules.

这篇关于什么是&QUOT; Func键&LT;对象&gt; modelAccessor&QUOT;在MVC的DataAnnotationsModelMetadataProvider参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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