MVC Series - Basics of MVC Architectural Pattern



Recently few days back i gone through numerous of blogs and articles ………… as usual , I came to a conclusion that very few writers have explained the topic from the basic, while including a working application. Most of the articles based on questions and answers which increase confidence of person for interview but not knowledge. I am going to put my effort in this articles series to cover almost all the aspects of MVC starting from small examples including database operations with various Microsoft providers. We’ll be gradually moving forward part by part so we can understand and practically implement every scenario.

Let’s start with following points.
  1. What does MVC mean.
  2. Understanding MVC Architecture.
What does MVC mean.
MVC is not a design pattern, it is an Architectural pattern that describes a way to structure our application and the responsibilities and interactions for each part in that structure.
MVC separate the application object model from GUI, originally invented around 80s. Then later on it has become a widely accepted common design pattern. The main objective behind this pattern is to decouple the view of the data (presentation layer) from the actual data processing so that the same model can be used for various views. This is achieved by using three different types of objects that interact with each other in loosely coupled manner with their discreet set of tasks.
·         Model
·         View
·         Controller
The model is responsible for managing the data of the application. It responds to the request from the view and it also responds to instructions from the controller to update itself.
View is the presentation of data in a particular format, triggered by a controller's decision to present the data. They are script based templating systems like JSP, ASP, PHP and very easy to integrate with AJAX technology.
The controller is responsible for responding to user input and performs interactions on the data model objects. The controller receives the input, it validates the input and then performs the business operation that modifies the state of the data model.






MVC Architecture
Following diagram is self explanatory to understand the architecture and flow data



Ø  Request is being taken from User to controller.
Ø  Controller processes the request from the user and creates a data Model of that particular request.
Ø  Data model that is being created is then passed to View that handles the frontend or the design.
Ø  View then transforms the Data Model by using its own functions in an appropriate output format.
Ø  The output format that is being given by the View is then gets rendered to the Browser and the View will be seen by the user.

Let’s have practical look over mvc components
                                                                                      

Model

Ø  MVC model is basically a C# or VB.NET class
Ø  A model is accessible by both controller and view
Ø  A model can be used to pass data from Controller to view
Ø  A view can use model to display data in page.

View

Ø  View is an ASPX page without having a code behind file
Ø  All page specific HTML generation and formatting can be done inside view
Ø  One can use Inline code (server tags ) to develop dynamic pages
Ø  A request to view (ASPX page) can be made only from a controller’s action method
Controller
Ø  Controller is basically a C# or VB.NET class which inherits system.mvc.controller
Ø  Controller is a heart of the entire MVC architecture
Ø  Inside Controller’s class action methods can be implemented which are responsible for responding to browser OR calling views.
Ø  Controller can access and use model class to pass data to views
Ø  Controller uses ViewData to pass any data to view

.
.
.
.
We will discuss one working example in next post……..thanks………

OOPS Series - Difference between method overloading and overriding

method overloading
Method Overloading and Method Overriding are both the techniques used to implement POLYMORPHISM  

Method Overloading -
Method overloading known as compile time polymorphism, Method Overloading means having two or more methods with the same name but with different signature(different parameters list and different type of parameters) in same class or in different classes.

Lets see a example to simplify Method overloading

Example
 :
We will create a method that will calculate the square of a given number having same name but different data type.In this program we have use different methods having same name but different parameters this is called overloading.


  



method overriding

Method overriding -
Method overriding known as runtime polymorphism , Method overriding means having two methods with same name and same signature, one method in base class and other method in derived class.

A subclass inherits methods from a base class. Sometimes, it is necessary for the subclass to modify the methods defined in the base class. This is referred to as method overriding.

This can be achieved by using the virtual and override keywords. we have to use the virtual keyword for the method which in base class and override keyword  for the method in subclass.

By default functions are not virtual in C# and so you need to write “virtual” explicitly.

Example
 :



Conceptual C#: What is the Cloud OS?

Conceptual C#: What is the Cloud OS?

What is the Cloud OS?

What is the Cloud OS?

Hello friends during some search i found these links which helps me a lot to understand the "cloud computing" and "cloud" operating system.
.
.

cloud  cloud   clou

.

.

http://progressforge.com/brief-introduction-to-cloud-computing/

.

.

.

.

Nice Video

http://www.youtube.com/watch?v=QYzJl0Zrc4M

.
.
.
.
.
.
Now read what is microsof's "Cloud Operating Systemthats nothing but your "windows 8"
.
.
.
.

http://blogs.technet.com/b/microsoft_blog/archive/2013/01/15/what-is-the-cloud-os.aspx 

 

 

 

Exporting data grid in to Excel file


Step  1 : Create a web form containing a datagrid and a Button named Export
Step 2 :  On  Page_Load event write code to retrieve data from database and display in to grid
               In following example I have used storeprocedure  spGetExpiredUsers To retrieve data
Step 3 :  Write code in Button1_Click Event  to export data from grid to excel sheet




Refer the code below

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        SqlCommand strCommand = null;

SqlConnection conn = new SqlConnection("Data                             Source=@@@.@@@.@@.@@;Initial Catalog=DBName;User ID=Sunil;Password=Sunil123");

        strCommand = new SqlCommand("spGetExpiredUsers", conn);
        strCommand.CommandType = CommandType.StoredProcedure;
        strCommand.CommandTimeout = 0;
        conn.Open();


        SqlDataAdapter strDA = new SqlDataAdapter(strCommand);
        strDA.Fill(dt);
        dtgrid.DataSource = dt;
        dtgrid.DataBind();

        conn.Close();



    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string fileName = "reportsSun.xls";
        string Extension = ".xls";
        if (Extension == ".xls")
        {
            PrepareControlForExport(dtgrid);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            try
            {
                using (StringWriter sw = new StringWriter())
                {
                    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                    {
                        //  Create a form to contain the grid
                        System.Web.UI.WebControls.Table table = new System.Web.UI.WebControls.Table();
                        table.GridLines = dtgrid.GridLines;

                        //  add the header row to the table
                        if (dtgrid.HeaderRow != null)
                        {
                            PrepareControlForExport(dtgrid.HeaderRow);
                            table.Rows.Add(dtgrid.HeaderRow);
                        }

                        //  add each of the data rows to the table
                        foreach (GridViewRow row in dtgrid.Rows)
                        {
                            PrepareControlForExport(row);
                            table.Rows.Add(row);
                        }

                        //  add the footer row to the table
                        if (dtgrid.FooterRow != null)
                        {
                            PrepareControlForExport(dtgrid.FooterRow);
                            table.Rows.Add(dtgrid.FooterRow);
                        }

                        //  render the table into the htmlwriter
                        dtgrid.GridLines = GridLines.Both;
                        table.RenderControl(htw);

                        //  render the htmlwriter into the response
                        HttpContext.Current.Response.Write(sw.ToString());
                        HttpContext.Current.Response.End();
                    }
                }
            }
            catch (HttpException ex)
            {
                throw ex;
            }
        }

    }


    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                //control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                //control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                //control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                //control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }
            else if (current is HiddenField)
            {
                control.Controls.Remove(current);
                //control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }

            if (current.HasControls())
            {
                PrepareControlForExport(current);
            }
        }
    }
}