Data Binding in ASP. NET: Which is faster?

Source: Internet
Author: User

ASP. net uses the dynamic compilation technology to dynamically change *. the aspx file is *. CS, and then call compilerservices to compile it into assemblies (you can go to your % SystemRoot % \ Microsoft. net \ framework \ v1.x. XXXX \ temporary ASP. net files ). Therefore, understanding ASP. NET compilation is one of the keys to optimizing the efficiency of ASP. NET. Taking the Common Data Binding syntax as an example, we can find that its conversion rules are:

. Aspx:Data-binding expression %>

->

. CS:System. Convert. tostring (Data-binding expression);

Where data-binding expression isCopy from the originalIn this way, when you write a data binding expression, you will be confused. Container, dataitem, and databinder are common in data binding expressions. eval is like this: databinder is system. A static class in the Web, which provides the eval method to simplify writing data binding expressions. However, it uses reflection and other overhead methods to achieve ease of use, therefore, its performance is not the best. The container is not a static object or method at all. It is processed by the ASP. NET page compiler in the Data Binding event.ProgramLocal variables declared internally are data container types of controls that can be bound to data (for example, the data binding container inside repeater is called repeateritem ), basically, these container classes have the dataitem attribute, so you can write container. dataitem, which returns the data item from the data source you are bound. If your data source is datatable, the Data Type of this data item is actually datarowview.

Now you can think about which of the following statements is the most efficient (take repeater + datatable data source as an example ):

    1. <@ % Databinder. eval (container. dataitem, "columnname") %>
    2. <@ % Databinder. eval (container. dataitem, "columnname", null) %>
    3. <@ % Databinder. eval (container, "dataitem. columnname", null) %>
    4. <@ % (Datarowview) container. dataitem) ["columnname"] %>
    5. <@ % (Datarowview) container. dataitem). Row ["columnname"] %>

Note: The system. Data namespace needs to be introduced in the last two usage methods ...... The answer will be announced one day later. You are welcome to come to a conclusion by yourself!

Announcement + brief analysis:

At first glance, 1-3 uses the databinder. Eval Method for Data Binding calculation, while 4-5 uses strong type to directly obtain the data binding value. According to my previous reasoning, many people think that 4-5 is faster than 1-3. In fact, the 4th usage is also a common online "optimization" for databinder. eval ".

In fact, according to our test, the efficiency of the 4th writing methodsIn some common cases (that is, when the input field name and the field name inside the data table are case-insensitive)Even less than the most common 1st writing methods. However, the principle is correct, that is, to avoid using reflectionOr similar mechanism(Such as the propertydescriptor mechanism in system. componentmodel). However, the efficiency of Indexer in datarowview isWhen the number of fields is large and hashtable causes addressing conflictsIt is not as efficient as using the indexer of its row attribute (datarow type. The reason is that the indexer of datarowview implements the view function, which is not required for most applications in this scenario, and its overhead is even larger than that of databinder. Eval!(The content of this section is too arbitrary. I have tried several more experiments to find the correct reasons)Therefore, the fifth method can achieve better performance, rather than directly using the datarowview indexer when necessary.

Now we will go back to the 1-3 discussion. First of all, pay attention to the two overloads of the eval method:

Object databinder. eval (object container, string expression)
String databinder. eval (object container, string expression, string format)

Notes ASP. net in the generated. the CS file uses system. convert. tostring to convert the eval result to string, so the explicitly provided value is null or string. the format parameter of empty enables eval to call the first method to obtain the bound object, and then directly call the tostring () method of the object to return it to convert. tostring method. For this method, the compiler hasDuring CompilationConnect it to the convert. tostring (string) overload, and this method directly returns the input string. What if we use the first method directly? Although the second method calls the first method first, the compiler selects convert for it because its return value is of the object type. tostring (object) overload. In this overload method, some additional judgment will be made to convert the object to the string type, these extra judgments clearly bring about additional overhead-though not a major contradiction.

As for the 3rd writing methods, an indirect layer is introduced in the expression parameter, so one reflection is required to parse the expression. Therefore, the efficiency is very low.

I will sell another token here. Can we speculate whether the 5th methods can be further optimized? (I mean in the most common ASP. NET development scenarios): p

Through the above analysis, we can draw the following conclusion:

    1. Databinder. Eval is the most common and easy-to-use data binding expression. However, because its implementation mechanism uses reflection, you need to pay attention to its performance loss. Generally, when the application development enters a stable period, these expressions can be optimized accordingly.
    2. However, optimization is not literally visible. The 4th so-called optimizations are everywhere. HoweverIn some casesIt brings about overhead of other links and lower execution efficiency.
    3. Note that method Overloading is a compilation mechanism that explicitly tells the compiler about the method overloading that needs to be used. Generally, better performance can be achieved when the same result is obtained.
    4. Although the performance is important, the function price is higher-in general project development, we should first focus on the implementation of the function, and then test the performance bottleneck with targeted optimization.

I hope my experiences will inspire you. :)

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.