Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# MVC: Performance and Advantages of MVC Html Helpers vs. Direct HTML in views

Tags:

c#

asp.net-mvc

I'd like to know what kind of performance impact Html helpers have on C# ASP.NET MVC views, especially when setting attribute parameters, and what kind of advantages they have overall (why use them?)

With Html Helpers:

<%= Html.TextBox("firstName", Model.FirstName, 
    new { @disabled = "disabled", @class = "myCssClass" }) %>

Direct Html:

<input type="text" class="myCssClass" name="firstName" 
     disabled="disabled" text="<%= Model.FirstName %>"/>

I have quite a few pages that contain between 5 and 15 of such inputs. On top of that Html Helpers allow you to render the form (think Html.BeginForm()) etc. so you potentially end up with 20 or even more Html Helper calls. I think some of them use reflection too, e.g. when you set attributes like the disabled one above.

Isn't there a huge performance impact to do this? Why on earth is it considered better practice to use those helpers? Please somebody give me a good reason :) I'd like to use them but I really fear the performance impact they have.

Are there any real advantages to using Html helpers?

like image 980
Alex Avatar asked Jun 06 '09 04:06

Alex


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C and C++ meaning?

C is a function driven language because C is a procedural programming language. C++ is an object driven language because it is an object oriented programming. Function and operator overloading is not supported in C. Function and operator overloading is supported by C++. C is a function-driven language.


2 Answers

The overhead of doing reflection is something that people really like to worry about. Outside of synthetic benchmarks, however, it becomes a pretty boring topic!

In the context of a real production application (where you are doing CRUD operations against a databases or consuming a webservice for example), the overhead of using html helpers is going to be insignificant compared to the overhead of doing that kind of context switch.

Really not something to worry about especially considering the benefits html helpers provide such as automatically restoring form values from ViewData/Model, and validation support.

Bottom line: use html helpers when possible. You can always use straight html if you encounter a rare limitation that you need to work around.

like image 147
James H Avatar answered Sep 20 '22 19:09

James H


Are there any real advantages to using Html helpers?

The biggest advantage I see in using HtmlHelpers is to provide an abstraction layer for your markup. If in the future you wanted to change the structure of your markup you only need to change the output generated from the helpers, as opposed to going through all your views and making manual changes.

It also promotes consistency amongst teams of developers. Those developers aren't required to know the exact details of the markup structure and css classes your UI is based on.

As an example, I'm currently developing a new UI framework for the company I work for based on Bootstrap. I have created a set of HtmlHelpers that generate the appropriate markup and css classes for the various Bootstrap components. This is all done in a Fluent API which is nice for developers to use, without any in-depth knowledge required of Bootstrap, plus with the added benefit of having Intellisense available.

Telerik's Kendo UI framework is based on the same concept. Take a look at some of their code samples.

As for reflection and performance, I really wouldn't worry considering the number of calls likely to be involved in a few HtmlHelper methods. See this post for reasons why.

like image 31
Brett Postin Avatar answered Sep 24 '22 19:09

Brett Postin