Tuesday, October 25, 2011

Why MVC3 RAZOR ?

When I am coding, always having confusion between performance and clean code . Those two are the key success factors of the quality program. But sometimes it is hard to maintain clean organized architecture while achieving a performance.

By the way , we have to think out of the frame and avoid using building block type .NET applications development with controls , if we run towards high quality end product which matches current web standards.

Well, one of the main advantages I experience using php (interpreted) over c# (compiled) is language flexibility. Earlier when I use php I am confident enough to say, “This is possible “. If I use .NET for web applications , I used to think  several times before say that . Reason is inflexibility.
In earlier days (.NET 2.0 ) we had web forms (aspx) . Microsoft has introduced set of compatible tools / controls / plugins to integrate with them .We drag/drop and develop. GOD knows what happens inside the controls . When control gives some runtime error , developers spend hours to fix it . Sometimes it is just asking for official support . Now everything has become new . Luckily even after many years microsoft introduced MVC , integrated with jquery . 

And Razor is one of the view engine introduced later .
Earlier when u want to display a dataset .you use a gridview . Now it is fairly simple and flexible with just plain html tables .No one can hide anything from you . Everything is visible. Here is the simple sample notation.
@foreach (var dataitem in dataset){
<tr>
<td>@dataitem.property1 </td>
<td>>@dataitem.property2 </td>
</tr>
}
We can use @ symbol infront of serverside code block in html page and then compiler identifies it .
Obviously razor views are slower since it iterate the code from server side . Except that , we can also easily pass data to client and iterate through javascript  .

$.ajax ({
 url:"Employee",
type :"get",
data:{id:123},
contenttype:"application/json"

}).done(function(data){

console.log(data.Name); // May be you can write code to display in div here

}) ;

 Razor does not have viewstate and postback . Therefore it come up with ViewData dictionary and ViewBag ( Dynamic variables), to share information between controller and view.

Eg -  ViewBag.Title ="Good Article ";  // In Controller
 Can display in view using @ViewBag.Title

Simply which is Performance vs cleancode  as I mentioned in startup of the article .
So in huge applications where employees working as teams , we have to  manage the code and use patterns to organize  the code . So strongly typed razor views will helpfull to maintain proper architectural code  .Then we can use more hardware resources to achieve performance . Disk space + RAM etc .

That is a small  information about RAZOR .