ASP.NET MVC2 ModelMetadataProviders: What's the difference between overriding CreateMetadata() and GetMetadataForProperty()? -
i'm statring explore framework's extension points, starting metadataproviders. i've implemented populating modelmetadata.isrequired property using requiredattribute
succesfully, can't seem find difference between overriding createmetadata()
or getmetadataforproperty()
, since both options seem work.
in general, examples i've seen override createmetadata()
.
- what pro , cons of using either options?
- are there scenarios 1 of these preferred options?
as extra: there resources (blogs, books) learn extension point?
the getmetadataforproperty()
declared on class modelmetadataprovider
.
associatedmetadataprovider
derives modelmetadataprovider
. createmetadata()
declared on associatedmetadataprovider
. dataannotationsmetadataprovider
overridden in link provide derived associatedmetadataprovider
.
the mvc framework makes calls modelmetadataprovider
's getmetadataforproperty()
method.
the reason overriding createmetadata()
working because associatedmodelmetadataprovider
's default implementation of getmetadataforproperty()
makes call createmetadata()
. looks this:
public override modelmetadata getmetadataforproperty(func<object> modelaccessor, type containertype, string propertyname) { if (containertype == null) { throw new argumentnullexception("containertype"); } if (string.isnullorempty(propertyname)) { throw new argumentexception(mvcresources.common_nullorempty, "propertyname"); } propertydescriptor propertydescriptor = this.gettypedescriptor(containertype).getproperties().find(propertyname, true); if (propertydescriptor == null) { throw new argumentexception(string.format(cultureinfo.currentculture, mvcresources.common_propertynotfound, new object[] { containertype.fullname, propertyname })); } return this.getmetadataforproperty(modelaccessor, containertype, propertydescriptor);
}
protected virtual modelmetadata getmetadataforproperty(func<object> modelaccessor, type containertype, propertydescriptor propertydescriptor) { ienumerable<attribute> attributes = this.filterattributes(containertype, propertydescriptor, propertydescriptor.attributes.cast<attribute>()); return this.createmetadata(attributes, containertype, modelaccessor, propertydescriptor.propertytype, propertydescriptor.name); }
if subclassing associatedmetadataprovider
in link provided, preferred extensibility point createmetadata
method, because associatedmetadataprovider.getmetadataforproperty()
method pre-validates contract of createmetadata()
method. way, know if there error in createmetadata()
method, know source of error in method , not in arguments passed it.
also, here source of filterattributes()
method, in case wondering:
protected virtual ienumerable<attribute> filterattributes(type containertype, propertydescriptor propertydescriptor, ienumerable<attribute> attributes) { if (!typeof(viewpage).isassignablefrom(containertype) && !typeof(viewusercontrol).isassignablefrom(containertype)) { return attributes; } return attributes.where<attribute>(delegate (attribute a) { return !(a readonlyattribute); }); }
Comments
Post a Comment