Tuesday, February 7, 2012

Asp .Net MVC 3 Getting Started

Asp .Net MVC is a complete alternative to Asp .Net Web Forms. Asp .Net emphasizes testability, doesn't conceal how the web works and has excellent control over HTML.

Asp .Net MVC is based on MVC pattern which dates back to 1978 and the Smalltalk project at XEROX PARC. Asp .Net MVC is built as a  series of independent replaceable components and the developers can choose  the routing system, the view engine, the controller factory  or ORM. 

Asp .Net MVC 3 is built on .Net 4. It comes with a new view engine called Razor and tighter integration with jQuery.

Still Asp .Net Web Forms is the superior technology when it comes to intranet applications. ASP .Net 4 has become more web standard compliant and you can use Razor view engine. Just like Asp .Net MVC, Asp .Net 4 also support excellent URL routing features.

Let's start creating an Asp .Net MVC 3 project. The blog Asp .Net MVC 2 with Visual Studio 2010 or Visual Web Developer 2010 I had covered the basics of getting started with MVC 2.

Asp .Net MVC New Project Shalvin
As already mentioned Asp .Net MVC 3 is coming with the Razor View Engine, thought you can use the Web Forms View Engine with Asp .Net MVC that is not a recommended practice. Microsoft has made it clear that the future developments will be based on Razor view engine.

So I am selecting Razor as the View engine.

If you navigate to Scripts folder in the Solution Explorer of Asp .Net MVC 3 project you can lot more jQuery files that the previous version.

Controllers and Action Methods
In MVC architecture, incoming requests are handled by controllers. In ASP.NET MVC, controllers are just simple C# classes .
Each public method in a controller is known as an action method.  You
can invoke an Action Method from the Web via some URL to perform an action.
The MVC convention is to put controllers in a folder called Controllers, which Visual Studio created for us when it set up the project.


The controller’s job to construct some data, and it’s the view’s job to render it as HTML. The
data is passed from the controller to the view.
One way to pass data from the controller to the view is by using the ViewBag object. This is a member
of the Controller base class. ViewBag is a dynamic object to which you can assign arbitrary properties,
making those values available in whatever view is subsequently rendered.



I have already covered creating controllers in the blog Asp .Net MVC 2 with Visual Studio 2010 or Visual Web Developer 2010 which remains the same in Asp .Net MVC 3.

When we return a ViewResult object from an action method, we are instructing MVC to render a view. We create the ViewResult by calling the View method with no parameters. This tells MVC to render the default view for the action.


We can return other results from action methods besides strings and ViewResult objects. For instance a RedirectResult can be used to  redirected browser  to another URL. If we return an HttpUnauthorizedResult, we force the user to log in. These objects are collectively known as action results, and they are all derived from the ActionResult class.


The Razor view is quite different from that of Web Forms View Engine. Refer my blog Razor View Engine and Visual Studio for more information on Razor View Engine.


@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>






using System.Web.Mvc;

namespace MvcApplicationShalvin.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
          ViewBag.Name = "Shalvin P D";
          ViewBag.Site = "http://shalvin.net";
         
            return View();
        }

    }
}





@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@ViewBag.Name

<br />
@ViewBag.Site


F# 2.0 Console Application with Visual Studio - II

This is part of the blog that explore F# 2.0

F# 2.0 with Visual Studio 2010


 In the previous blog we explored a Hello world application with F# using the REPL support it is providing.


open System

Console.Write "Enter your name : "
let strName = Console.ReadLine()
Console.WriteLine("Hello " + strName)
let c = System.Console.ReadLine()

let is the single most important keyword you use in F# programming: it’s used to define data,
computed values, functions, and procedures.

F# is statically typed. F# also supports type inferencing

What you see is a full functioning F# application. Notice the code reduction in comparison to other .Net languages like C# or VB .Net.

Let's run the application instead of using F# interactive.
F# Console Result Shalvin

Creating and calling functions


let square n = n * n

let result = square 3
Console.WriteLine(result)


Functions are central to Functional programming. Functional programming views all programs as collections of functions that accept arguments and return values.


Function with Parameters


open System

let add(a, b) =
  let sum = a + b
  sum

Console.WriteLine(add(45, 45))

Indenting denotes the body. There is no curly brackets to denote the body of a function. White spaces are relevant in F#.

Creating a Windows Form

open System.Windows.Forms 

let form = Form(Visible = true, Text = "Shalvin")
let btn = new Button(Text = "Helo")
form.Controls.Add(btn)

Here I am setting a reference to System.Windows.Forms and creating a form with a Button.

What enthralls me the most in F# is its terseness.

Monday, February 6, 2012

F# 2.0 with Visual Studio 2010

F# is a functional programming language initially developed by Don Syme now developed by Microsoft. Though it is inherently functional in nature it supports imperative and Object Oriented programming.

It is yet another .Net language ie. it emits IL and an excellent starting point for .Net professionals to explore functional programming.

F# supports Read Eval Print Loop (REPL) which makes it easy to learn a language.



F# Project in Visual Studio

 F# is included in Visual Studio along with C#, VB .Net and Visual C++.

I am starting an F# application and creating a conventional Console Application.


open System

Console.WriteLine "Shalvin"

open in F#  is equivalent to using statement in C#
For using REPL highlight the code segment and press Alt + Enter. You can see the output in the F# Interactive at the bottom of the IDE.

Friday, February 3, 2012

Timeline of major programming languages

1957 Fortran
1958 Lisp
1959 COBOL
1968 Smalltalk
1972 C
1972 Prolog
1975 Scheme
1983 C++
1986 Erlang
1987 Perl
1990 Haskell
1991 Visual Basic
1995 Java
2001 C#, VB .Net
2007 Clojure

Friday, January 20, 2012

jQuery with Visual Studio 2010

JQuery is a free open source JavaScript Library.It simplifies the task of creating highly responsive web pages and works across all modern browsers.


jQuery makes it simple getting and manipulating page contents and has a sophisticated effects library like animation without using additional plugins.

JQuery leverages your existing knowledge of CSS. JQuery makes it easy working with sets of elements like say paragraphs in a page of list items in an ordered list.

It is possible to perform multiple operations on a set of elements with onle line of code known as statement chaining.

Visual Studio 2010 has default support for jQuery.


If you start a New Web Site in Visual Studio 2010 you can see that a Script folder is present.

Inside that you can see 3 jquery files. jquery-1.4.1.js is the developer version which is of 165 kb size. jquery-1.4.1.min.js is the minified or production version which is of  71 kb and jquery-1.4.1-vsdoc.js which is the version with extra comments that enable visual studio intellisense for jQuery.

I strongly recommend you exploring these three jquery files.

Add a script referencet to  the  jquery library and let's explore the first example.
$("document").ready(function Hello() {
            alert("Welcome to JQeury");
        });
   

The $() function (an alias for the jQuery() function) returns a special JavaScript object containing an
array of the DOM elements that match the selector.
document.ready() function gets triggered after the whole DOM tree of the page gets loaded.

 jQuery Selectors and filters
jQuery selectors and filters retrieve contents from the document so it can be manipulated using other functions. This is the query part of jQuery.

JQuery selectors return an array of objects that match the selection criteria. JQuery filters operate on a selector to further refine the results array that the selector returns.

Thing to be noted is  that the array that selector returns is not a set of DOM elements. It is a collection of jQuery objects that provide a large number of predefined functions for further operating on the objects.


 


Selector

Intend

tagname

Find all elements that are named tagname

#identifier

Finds all elements with ID of identifier

.className

Finds all elements that have class attibute with the value of className

tag.className

Gets elements of  type tag that have a class attribute with the value of
className

tat#id.className

Retrive the tag element that has a ID of id and a class attribute with the value
of className


You can observe that jQuery have borrowed heavily from CSS making it easy for a person with knowledge in CSS to the transformed to jQuery.

For example, if you want to hide all the paragrahs in a page. The jQuey command will be :

$("p").hide()



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script>
        $("document").ready(function () {
            $("p").hide()
        });
    </script>
    <title>Shalvin jQuery</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
    <p>
        Shalvin Specializations</p>
    <p>
        Asp .net</p>
    <p Id = "Orm" >
        Entity Framework</p>
    <p>
        C#</p>
    <p>
        VB .Net</p>
    <p>
        SQL</p>
</body>
</html>


Suppose you want to hide only the paragrph with the id Orm the jQuery command will be as follows:
$("#Orm").hide()

To be continued..

Thursday, November 10, 2011

Barcamp Kerala 11

Barcamp Kerala 11

Venue : IIM Calicut
Date : 20th November 2011

Sessions
Microsoft Silverlight                                            -  Shalvin P D
IITs & IIMs - Are they (still) Socially Relevant ?  - Praseed Pai
Making your First Dollar with your PC                 - Arun Basil Lal
Html 5 and Beyond                                             -  Shwetank Dixit
Social media and Business                                   - Hari K T
The Conversation Club                                        - Sebastian Panakal

Session details

 

Wednesday, August 17, 2011

Kerala Microsoft User Group - August 1011 Meeting

20th Aug 2011 - Kochi



Venue: Orion India Systems Pvt Ltd, 103, 2nd floor, Tejomaya, Info park SEZ, Kakkanad, Kochin-682030


Agenda

09:30 - 09:40 Community updates

09:40 - 11:00 "Design Patterns in .Net" by Yanesh Tyagi

11:00 - 11:45 JavaScript MVVM in ASP.NET with KnockoutJS by Shiju Varghese

11:45 - 12:00 Tea Break (15 min)

12:00 - 12:45 Instrumenting & Profiling in .NET Apps" by Abraham Peter

12.45 - 01:00 Ask the experts





Mono

Mono is open source cross platform implementation of .Net I am using Mono in Ubuntu 10.10.
The preferred way to develop Mono in Linux is using MonoDevelop. MonoDevelop is a Mono IDE.

First let's see how to develop Console Application in Mono.
Once you have installed Mono you can go to the Terminal by going to Application, Accessories and select Teminal. The mono C# compiler is mcs.

class Hello
{
  public static void Main()
  {
    System.Console.WriteLine("Shalvin");
  }
}



So type mcs .cs in the terminal for compiling the C# file.
Once it is compiler you can access the exe using
mono .exe.


















Windows Forms
using System;
using System.Windows.Forms; 

class frmHello : Form
{
  public static void Main()
  {
    Application.Run(new frmHello());
  }
}