November 10, 2013

The ASP.NET MVC View Engine



View Engine in MVC 4

In general, view engine is combination of markup language and programming language. View engine is responsible to create HTML from the View. This is a two step process. ASP.Net supports multiple types of view engine such as: Razor, ASPX, Spark, Brail etc. Also, MVC provides flexibility for a custom view engine. Here, we will discuss about the view engine majorly in use.

ASPX: This is like a traditional ASPX page embedded with the markup language which requires defining of opening and closing tag as syntax. Extension for ASPX is .aspx

Syntax: <%: %>

Example: <%: Html.Label("Hello world!") %>


Razor:  This is a powerful and more flexible view engine as compared to ASPX. The line of code starts with @ symbol and does not require closing tag. Extension for Razor is depends on programming language you have selected. For C#, its .cshtml, for VB.Net its .vbhtml and so on.

Syntax: @

Example: @Html.Label("Hello world!")


In both above cases, output will be: Hello world!

Your comments, likes, dislikes are very important to make it better.
All code samples are available on requests.

November 9, 2013

Journey to ASP.NET MVC



Basics of ASP.NET MVC

MVC – the Model View Controller is a framework where each component is loosely coupled with each other. MVC is not the alternate of ASP.Net web forms, but a separate framework that force to develop application in tier architecture. Whether to choose MVC framework or ASP.Net web form, it depends upon the requirement and time span. The points given below may help you to choose an appropriate technology for your requirements.



 When to use ASP.NET MVC framework?

  1. When you want to develop SOA or 3-tier based application.
  2. MVC is code driven technology, when you want full control on behavior of application, this will be the better option.
  3. Since it is code driven, therefore it requires more time to develop and is beneficial in case of big project.When you need full control on markup language, Razor will be the most suitable and flexible.
  4. MVC is loosely coupled between components; therefore managing components for a project is easy. Loosely couple mechanism also gives the flexibility of parallel programming in case of large team.
  5. Separates Business logic, UI logic & Input logic into different components, so managing a project becomes easy. This framework also defines where to put the corresponding logic.
  6. Nevertheless, it keeps track of project where large number of developers are working, so that class files should be in specific unit.
  7. When you need more flexibility for URL rewriting which vital in SEO.


When to use ASP.NET Web Form?

  1. Tightly coupled, requires less code development. Therefore, it is beneficial in case of less time span.
  2. Advantages for small project or where it requires for Rapid Application Development (RAD).
  3. It is event model that preserves state over HTTP. It supports server side controllers.
  4. When you want benefits of server controls to avoid large amount of code lines.
  5. When you want to avail state management techniques.

We will discuss more about ASP.NET MVC 4 in next article.

September 23, 2013

Static Class and Static Constructor



Static class is the class having static member(s) only and it is called only once when it is loaded first time. The class loads at the time of its first use. It can have static contractor. Off course, static member cannot access non-static member (instance member) variable.


Static constructor is same as normal constructor which is used to initialize static variable(s), but the differences are- It cannot be overloaded nor takes any parameter as an argument. Normal constructor is used for instantiation of an object and to initialize it. It called as Instance Constructor where as Static Constructor is called as Class Constructor or Type Initializer. Static constructor is provided by C#.


Twist: In general, a constructor is called at the time of instantiation but we cannot create instance of a static class, then at what time it will execute? 

Answer is- only first time when it loads & being used to initialize its static variable.




public static class TestStatic
    {
        public static string _stest = string.Empty;

        static TestStatic()
        {
            _stest = "Hello! This is Static.";
        }
    }

public class TestNonStatic
{
        public string _nstest = "Hello! This is Non-Static.";
}

public partial class Form1 : Form
{
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Only first time class will execute.
            // After, it will execute as a variable.
            textBox1.Text = TestStatic._stest;
            TestStatic._stest = "Hii";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Each time constructor will be called.
            TestNonStatic t = new TestNonStatic();
            textBox2.Text = t._nstest;
        }
}


Output for button1:
Ist time : Hello! This is Static.
After : Hii




Why Static Method Cannot Access Non-Static Variables?
Non-static variables are the instance variable, they can be accessible by its object only where as static variable can be accessible by class name only, there scope is completely different. Since, non-static members are instance meber and each instance is considered as different object. Class is independent to its obaject, changing value of an object does not reflect on its class whereas static members are class members and is global in that scope. Changin its value at any place will also reflects in its class. Consider the below example:

public class Employee
{
        public int Age { get; set; }
        public string Name { get; set; }
}

Employee emp1= new Employee();
emp1.Age = 28;
emp1.Name = "Employee_1";

Employee emp2 = new Employee();
emp2.Age = 30;
emp2.Name = "Employee_2";

In above example, emp2 is completely independent from emp1 and they will never overwrite each other’s values anyhow. But if we define it as static, then it will will consider as single entity and will always overwrite with newly assigned value.  Consider the given example below:

public static class Employee
{
        public static int Age { get; set; }
        public static string Name { get; set; }
}

Employee.Age = 28;
Employee.Name = "Employee_1";

Employee.Age = 30;
Employee.Name = "Employee_2";