【转】ASP.NET MVC 3 Service Location, Part 7: Model Metadata

Model Metadata

ASP.NET MVC 2 introduced an extensible model metadata system where developers could implement a class which derived from ModelMetadataProvider to provide meta-information about the models in the system. In ASP.NET MVC 3, we have made the metadata provider findable via the dependency resolver.

Disclaimer

This blog post talks about ASP.NET MVC 3 Beta, which is a pre-release version. Specific technical details may change before the final release of MVC 3. This release is designed to elicit feedback on features with enough time to make meaningful changes before MVC 3 ships, so please comment on this blog post or contact me if you have comments.

Writing a Metadata Provider

For more information about ModelMetadata and what would be involved in writing a metadata provider, please see my blog post ASP.NET MVC 2 Templates, Part 2: ModelMetadata.

Location: ModelMetadataProvider

This is a “singly registered” style service introduced in MVC 2. The static registration point for this service is atModelMetdataProviders.Current for non-DI users.

The model metadata provider inspects models, and returns meta-information about the models, include information like display names, formatting strings for display and editing, flags on whether a value is editable or not, etc.

The logic in ModelMetadataProviders was updated to attempt to find the implementation of ModelMetadataProvider first by calling DependencyResolver.GetService(typeof(ModelMetadataProvider)). If the service does not exist in the dependency resolver, then it falls back to the static registration point. If you register a model metadata provider in both the static registration point and in the dependency resolver, MVC will throw an exception (since the service is intended to be singly registered).

The default model metadata provider continues to be DataAnnotationsModelMetadataProvider.

What’s Next?

Next up in the series is Value Providers.

posted @ 2011-01-25 17:56  四眼蒙面侠  阅读(392)  评论(0编辑  收藏  举报