Archive for March, 2009

MVC ASP.NET VS2008 - quick overview

Monday, March 30th, 2009

There is a template for Visual Studio 2008 for MVC template. You can download it from here. After Installation, run VS 2008 and create new project.

VS2008 asks you about test project for your solution:

You can choose test project name and testing framework. And ‘voila’. This is you solution explorer and you can find bunch of some files there.


Some basic functionality is already there. “Home” and “About” pages and “Login” and “Register” functionality is created too.

The LINQ (Language-Integrated Query)

Wednesday, March 25th, 2009

LINQ is a combination of namespaces and C# 3.0 language enhancements. LINQ provides a high-level abstraction of virtually any data and emulates the query operations of the relational model. The LINQ Project seems to be just the beginning of many other future dramatic enhancements to .NET and .NET languages.

LINQ has three major components:

  1. LINQ to Objects
  2. LINQ to ADO.NET, which includes
    • LINQ to DataSet (originally called LINQ over DataSet) LINQ to Entities
    • LINQ to SQL (originally called DLinq)
  3. LINQ to XML (originally called XLinq)
To start playing with LINQ, first install it from here.

and here is sample of code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;

namespace Linq
{

[Table]
public class Customers
{
[Column(Id=true)]
public string customerId;
[Column]
public string companyName;
[Column]
public string city;
[Column]
public string country;
}

class Program
{
static void Main(string[] args)
{
// connection string
string connString = @"server = .\sqlexpress;integrated security = true; database = northwind ";
// create data context
DataContext db = new DataContext(connString);

// create typed table
Table customers = db.GetTable();

// query database
var custs =
from c in customers
select
c
;

// display customers
foreach (var c in custs)
Console.WriteLine(
"{0} {1} {2} {3}",
c.customerId,
c.companyName,
c.city,
c.country
);
}
}
}

ahd that is all.

ASP.NET MVC 1.0 now Live!

Friday, March 20th, 2009

ASP.NET MVC 1.0 finally published. You can download it from here. For tutorials, videos, and more please check out the official site.
more about asp.net mvc you can read on other blogs:
here:
ASP.NET MVC 1.0 now Live! and Free ASP.NET MVC eBook Tutorial.

MVC - Model-View-Controller

Friday, March 13th, 2009

Model–View–Controller (MVC) is an architectural pattern used in software engineering. It allows software developers to build a Web application as a composition of three roles: Model, View and Controller. A Model represents the state of a particular aspect of the application. Frequently, a model maps to a database table with the entries in the table representing the state of the table. A Controller handles interactions and updates the model to reflect a change in state of the application. A View extracts necessary information from a model and renders a user interface to display that”.<br/>

Here is the code for simple Calculator application written in Java which is organized according the the Model-View-Controller (MVC) pattern.<br/>

The main program initializes everything and ties everything together.

Main.java:

import javax.swing.*;

public class Main{
    public static void main(String[] args) {

        CalcModel      model      = new CalcModel();
        CalcView       view        = new CalcView(model);
        CalcController controller = new CalcController(model, view);

        view.setVisible(true);
    }
}

here is the View class:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class CalcView extends JFrame {
    private static final String INITIAL_VALUE = "1";

    private JTextField m_userInputTf = new JTextField(5);
    private JTextField m_totalTf     = new JTextField(20);
    private JButton    m_multiplyBtn = new JButton("Multiply");
    private JButton    m_clearBtn    = new JButton("Clear");

    private CalcModel m_model;

    CalcView(CalcModel model) {
        m_model = model;
        m_model.setValue(INITIAL_VALUE);

        m_totalTf.setText(m_model.getValue());
        m_totalTf.setEditable(false);

        JPanel content = new JPanel();
        content.setLayout(new FlowLayout());
        content.add(new JLabel("Input"));
        content.add(m_userInputTf);
        content.add(m_multiplyBtn);
        content.add(new JLabel("Total"));
        content.add(m_totalTf);
        content.add(m_clearBtn);

        //... finalize layout
        this.setContentPane(content);
        this.pack();

        this.setTitle("Simple Calc - MVC");

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    void reset() {
        m_totalTf.setText(INITIAL_VALUE);
    }

    String getUserInput() {
        return m_userInputTf.getText();
    }

    void setTotal(String newTotal) {
        m_totalTf.setText(newTotal);
    }

    void showError(String errMessage) {
        JOptionPane.showMessageDialog(this, errMessage);
    }

    void addMultiplyListener(ActionListener mal) {
        m_multiplyBtn.addActionListener(mal);
    }

    void addClearListener(ActionListener cal) {
        m_clearBtn.addActionListener(cal);
    }
}

Here is the Controller class:

import java.awt.event.*;

public class CalcController {

    private CalcModel m_model;
    private CalcView  m_view;

    CalcController(CalcModel model, CalcView view) {
        m_model = model;
        m_view  = view;

        view.addMultiplyListener(new MultiplyListener());
        view.addClearListener(new ClearListener());
    }

    /*
     *  1. Get the user input number from the View.
     *  2. Call the model to mulitply by this number.
     *  3. Get the result from the Model.
     *  4. Tell the View to display the result.
     * If there was an error, tell the View to display it.
     */
    class MultiplyListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String userInput = "";
            try {
                userInput = m_view.getUserInput();
                m_model.multiplyBy(userInput);
                m_view.setTotal(m_model.getValue());

            } catch (NumberFormatException nfex) {
                m_view.showError("Bad input: '" + userInput + "'");
            }
        }
    }
    class ClearListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            m_model.reset();
            m_view.reset();
        }
    }
}

and here is the Model class:

import java.math.BigInteger;

public class CalcModel {

    private static final String INITIAL_VALUE = "0";

    private BigInteger m_total;  // The total current value state.

    CalcModel() {
        reset();
    }

    public void reset() {
        m_total = new BigInteger(INITIAL_VALUE);
    }

    public void multiplyBy(String operand) {
        m_total = m_total.multiply(new BigInteger(operand));
    }

    public void setValue(String value) {
        m_total = new BigInteger(value);
    }

    public String getValue() {
        return m_total.toString();
    }
}

and here is the result:
Calculator MVC Model

have a nice day :)

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in