Wednesday, September 29, 2010

Defining Structural Contracts

Defining Structural Contracts
Data Contracts:

Data contracts are the contractual agreement about the format and structure of the payload data (that is, the SOAP body) in the messages exchanged between a service and its consumer.
Data contracts are the preferred WCF way to enable serialization of complex types included in operation signatures, either as parameters or as return values.

Using a data contract, you can specify exactly how the service expects the data to be formatted as XML. The data contract is used by a data contract serializer in WCF client applications to describe how to serialize the data for parameters into XML, and by a data contract serializer in the service to deserialize the XML back into data values that it can process. Values returned by a service are similarly serialized as XML and transmitted back to the client application, which deserializes them.


DataContract Attribute
The DataContract Attribute, defined in the System.Runtime.Serialization namespace, is used to declare a type as a Data contract.
It can be applied to enums, structs, and classes only.


DataMember Attribute
The DataMember Attribute, also part of the System.Runtime.Serialization namespace, is applied only to members and is used to declare that the member should be included in the serialization of the data structure.


EnumMember Attribute
The final Data contract–related attribute to consider is the EnumMemberAttribute. As the name suggests, it is used to declare that a given element of an enum that has been declared with a DataContractAttribute should be considered as part of the Data contract.

Eg: Create a WCF Service and write the following code in the IService.cs file.

[ServiceContract]
public interface IService
{
    [OperationContract]
    Employee GetEmployee();
}

[DataContract]
public class Employee
{
    string name, job;
    [DataMember]
    public string Name
    {
        set { name = value; }
        get { return name; }
    }
    [DataMember]
    public string Job
    {
        set { job = value; }
        get { return job; }
    }
}

Write the following code in the Service.cs

public class Service : IService
{
    public Employee GetEmployee()
    {
        Employee emp = new Employee
        {
            Name = "Jobin John",
            Job = "Software Eng"
        };
        return emp;
    }
}


Add a new website to Solution. Place a button and write the following code.

        protected void btnDisplay_Click(object sender, EventArgs e)
        {
            EmployeeServiceReference.ServiceClient client = new WebApplication1.EmployeeServiceReference.ServiceClient();

            EmployeeServiceReference.Employee emp = client.GetEmployee();
            Response.Write("Name : " + emp.Name);
            Response.Write("
Job : " + emp.Job); }

Returning Views in MVC Application

Create an ASP.NET MVC 2 empty application. Add a controller named HomeController and write the following action method.

public ViewResult Index()
        {
            return View();
        }


 

ActionResult Class:

All methods within a controller are Action methods, and returns ActionResult object.

The above function returns ViewResult
object; which is a subclass of ActionResult

Adding View:

    Right click in the method and select add view option. Name the View as "Index" and add the view.

Run the application. Hence you have created an MVC application which returns a View.

jQuery Selectors: Select elements using Id

Selecting HTML elements with Id.

syntax:
$("#elementid")

The # selector selects an element with a unique id.

eg:
write the following in html head section
<script type="text/javascript">
$(
}
function DisplayAll() {"#divDetails").text("Welcome to my first jQuery application.");</script>

write the following in the body.

<div id="divDetails"></div><hr />
<div></div><hr /><div></div><hr /><div></div><hr />

Tuesday, September 14, 2010

Using jQuery Selectors : Using tag name

Using jQuery Selectors
  tag name, ID, and class. They can be used either on their own or in combination with other selectors.             
Three building blocks of selectors are

Selecting HTML elements with tag name.


The following example demonstrates how to select all div elements and assign a text value into it within  an HTML page.
syntax:
 $(element name)  : which returns all matched html elements.
 $('element name').text('text to display'); : which assign the text value into all matched element.
Eg:
Add following in html head

<script type="text/javascript">
$(
}
function DisplayAll() {"div").text("Welcome to my first jQuery application.");</script>
Add following in html body

<input id="Button1" type="button" value="Display Text onclick="DisplayAll()" /><div></div><hr />
<div></div><hr /><div></div><hr /><div></div><hr />

Introduction to jQuery & What jQuery does

Introduction to jQuery

jQuery is a library of JavaScript Functions, ie; a group of built-in javascript function to improve the interaction with web development.

Using jQuery you can do the following:
  • Simplify common JavaScript tasks
  • Access HTML elements in a document
  • HTML element manipulation
  • Modify the appearance of a web page
  • Manipulate CSS properties 
  • Alter the content of a document
  • Respond to a user's interaction
  • Animate changes being made to a document. 
  • Retrieve information from a server without refreshing a page ( AJAX).
  •  

Thursday, September 9, 2010

First MVC 2 Application with Visual Studio 2010

Creating your First MVC Application with Visual Studio 2010

Eg: Follow the following steps.

  • Create an empty MVC 2 web application.


  • Add a controller named HomeController and write the following method.
public string Index()
{
  return "My first MVC Application";
}

  • Run the application.

The above application just returns a string value. We dont have any view. The browser will display only a string.