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.

public interface IService
    Employee GetEmployee();

public class Employee
    string name, job;
    public string Name
        set { name = value; }
        get { return name; }
    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);
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.


The # selector selects an element with a unique id.

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 />