Value providers
ASP. net mvc 2 introduced a new method to find value providers: Valueproviderfactory Class. value providers are used by the model binding system in MVC to populate the values of Model objects. MVC shortdes value providers for several common value sources (including query string, form, route data, uploaded files, and JSON postbacks); MVC futures shortdes several mode (including cookies, server values, session values, and temp data values ). in ASP. net MVC 3, we have made valueproviderfactory instances findable via the dependency resolver.
Disclause
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.
Implementing valueproviderfactory
Developers who implement this class must provide an implementation of getvalueprovider which, given a controller context, optionally returns an instance of a class which implements Ivalueprovider Interface. The developer may also choose to return NULL if there is no appropriate value provider or values.
For reference purposes, there are several implementations of valueproviderfactory In the MVC and MVC futures source code. For a simple example, see the pair of classes: Querystringvalueproviderfactory And Querystringvalueprovider.
Implementing ivalueprovider
Developers who implement this interface provide implementations of two methods: containsprefix and getvalue. the former method is used to determine if there are any values in the provider with the given prefix (so the model binding system knows when to stop recursively binding ). the latter method is used to get the value for a specific key.
There are two implementations of ivalueprovider (namevaluecollectionvalueprovider And Dictionaryvalueprovider) will accept collections of values to be used for the value provider implementation. Most of the value providers in the MVC source code actually derive from one of these two base classes.
Location: valueproviderfactory
This is a "Multiply registered" style service introduced in MVC 2. the static registration point for this service is atvalueproviderfactories. factories for non-di users.
The logic in Valueproviderfactorycollection (Which implements valueproviderfactories. factories) was updated to consult the dependency resolver, calling getservices (typeof (valueproviderfactory) and adding any services found to the list of Statically registered services. value providers all collaborate to provide values for model binding, with a "first one to provide the value wins" strategy, so registration order is important. the factories found in the dependency resolver will always run in the order they are returned from the resolver (and before the statically registered value provider factories); similarly, the value providers will be consulted in the order they are returned from the factories.
What's next?
Next up we'll talk about Model binders.