Summary of ASP. net mvc ValueProvider, mvcvalueprovider

Source: Internet
Author: User

Summary of ASP. net mvc ValueProvider, mvcvalueprovider

In Model binding, there are many data sources for Model. In MVC, a set of ValueProvider components are defined to deal with the diversity of Model data sources. The entire ValueProvider structure is similar to the dictionary (Dictrionary) the Key-Value structure. The Value is obtained using the given Key. In a complete set of components, there are interfaces, abstract ValueProvider and its subclass implementing interfaces, and ValueProvider's abstract factory ValueProviderFactory and its subclass, there are a set of all ValueProviderFactory, and so on. The class diagram is as follows, but the class diagram in instructor Jiang's book seems to have an incorrect mark.

Interface Section

IValueProvider: Declare the GetValue method and the ContainsPrefix method. The former obtains the corresponding Value based on the key. This key may have a prefix, and the latter determines whether a given prefix is available.

IEnumerableValueProvider: Inherits IValueProvider. The GetKeysFromPrefix method is provided for data of the Collection type. The Key with the specified prefix in the container is returned. This process needs to be verified by default.

IUnvalidatedValueProvider: Inherits IValueProvider. Another GetValue method is declared. This method does not need to verify the data.

ValueProvider

ValueProviderResult: ValueProvider (class that implements IValueProvider) method GetValue return Value type, where the AttemptedValue attribute is represented as a string of Value; RawValue is the original Value of Value, which is of the Object type; the convertor method is to convert objects of the specified Type and return objects of the corresponding Type. A overload method is to add the CultrualInfo Type.

NameValueCollectionValueProvider: Implements the IValueProvider, IEnumerableValueProvider, and IUnValidatedValueProvider interfaces. In this class, the key and value are stored in a similar dictionary set. A key in this set can have multiple values, so it is similar to a dictionary set. Both Key and value are of the string type.

The prefix can be used in two scenarios:

1 indicates the prefix of a complex type and is expressed by a fully qualified name. For example, if the Address attribute in the Contact class in the book is of an Address type, add an "Address" prefix to each attribute.

2. When multiple models of the same type are bound, prefixes are added to distinguish the same attributes of different instances for differentiation. There are three prefixes: 1) in the form of "Object Name."; 2) in the form of "[index]."; 3) in the form of "[Object Name ]."

When GetKeysFromPrefix is executed, square brackets ("[", "]") and points (".") have special meanings and are placeholders.

FormValueProvider: Inherits NameValueCollectionValueProvider, which is used to provide data from the submitted form.

QueryStringValueProvider: Inherits NameValueCollectionValueProvider, which is used to provide data from the URL (specifically QueryString.

DictionaryValueProvider <TValue>: Implements the IEnumerableValueProvider and IValueProvider interfaces. Keys and values are stored in real dictionary sets, keys are of the string type, and values are of the generic type.

RouteDataValueProvider: Inherits DictionaryValueProvider <object> and provides values from route parameters.

HttpFileCollectionProvider: Inherit from DictionaryValueProvider <HttpPostedFileBase []>. The Model contains the HttpPostFileBase data provider, which is usually used by the Action for uploading files. I personally estimate that a key is opened every time a request is sent.

ChildActionValueProvider: Inherits DictionaryValueProvider <object> and provides values when processing subactions (A subaction is called in a View to generate HTML of a part, which is similar to an Action that generates a Partial View, both RouteDataValueProviderd and RouteDataValueProviderd are object-type values, and the Routedata value (actually RouteValueDictionary type) is also extracted from the Routedata of ControllerContext as the data container Dictionary of ChildActionValueProvider, the difference between ChildActionValueProvider and RouteDataValueProvider lies in the GetValue method. RouteDataValueProvider matches the RouteValueDictionary key-value pair, while ChildActionValueProv Ider is a dictionary set in which a GUID value is taken as the value corresponding to the key. This GUID is a static attribute of ChildActionValueProvider and the value obtained with this key, it is a dictionary set. Each pair of key/value in this dictionary is stored in the ChildActionValueProvider's key-value Pair container. It is equivalent to using the GUID as the key value as a copy. I don't quite understand why I want to do this.

ValueProviderCollection: Implements the IValueProvider, IEnumerableValueProvider, and IUnValidatedValueProvider interfaces. It is a collection of ivalueproviders. The methods that implement each interface in it are the methods that traverse and call each element in the set. For details, refer to the book.

Factory

ValueProviderFactory: Is an abstract class. Various valueproviders are created based on ControllerContext. The specific creation work is implemented by its subclass. The factory method mode is used here.

ValueProviderFactoryCollection: The set of ValueProviderFactory defines the GetValueProvider method. After ControllerContext is passed in, the methods of the same name in each ValueProviderFactory will be executed. Whoever creates the method will return the type of factory, which will involve priority, it is actually the order of each element in the set.

ValueProviderFactories: The struct is used. The ValueProviderFactories is actually filled in for this Collection set. The order of those factories is ChildActionValueProviderFactory, FormValueProviderFactory, JsonValueProviderFactory, struct, QueryStringValueProviderFactory, and struct.

For this ValueProvider component, the factory method mode is used in the overall structure. The advantages of the factory method do not have to be mentioned. From the background, the Model comes from various sources, rules that provide specific values can be expanded freely without the core of the component. Therefore, the creator of the corresponding rule object cannot operate on the core of the component, for the component core, you only need to provide sufficient information. In various scenarios, you can use your own method to create the Model value. Here, you need to provide sufficient information, controllerContext contains the request information and route information.


What is the use of aspnet mvc model [DefaultValue?

This is a feature attribute. It just marks it. Some components or code will be used, and there is no certain saying about wrong writing.

In fact, this is used in many places. For example, it will be used as the default value during deserialization, and it will be prompted for verification when the metadata information is verified in mvc.

This must be reflected in a specific environment. You can also read this value and perform your own operations.

How to Implement aspnet mvc?

The problem is widespread in space. It is recommended that you learn mvc first. If you learn about it, you can look at mvc-related projects or the source code of mvc. Good luck! Think more, practice diligently, do not simply look at and do not practice!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.