Drag and Drop Programming in Microsoft ASP.NET AJAX - Write the Web Service
(Page 4 of 4 )
By using Web Service, we can utilize the standard way to wrap the basic database operation and supply the data to the client side. Right click the project, choose "Add new item" and create a new Web Service named ShoppingService.asmx. Next, we will open the ShoppingService.cs file and create the required web methods.
First, as required by the framework, we must put the ScriptService attribute before the Web Service so that the MS AJAX JavaScript framework can call it correctly. The following is the key code snippet:
[System.Web.Script.Services.ScriptService]
public class ShoppingService : System.Web.Services.WebService
Next, we should define a private field, Pets, to represent all the pet information in the online store:
private List<Pet> _pets;
private List<Pet> Pets
{
get
{
if (_pets == null)
{
_pets = new List<Pet>();
_pets.Add(new Pet(1, "Flex", "Cat","Gray",390));
_pets.Add(new Pet(2, "Fido", "Dog", "Brown", 490));
_pets.Add(new Pet(3, "Rover", "Dog", "Brown", 550));
_pets.Add(new Pet(4, "Daisy", "Dog", "Black and White", 390));
_pets.Add(new Pet(5, "Polly", "Cat", "Green", 490));
}
return _pets;
}
}
Note here, just for the purposes of the demo, we've hard coded some commodities instead of inquiring for the information from the database. In real world scenarios, this kind of data is typically acquired from inside some database.
Next, we are to define the Pet class as an OOP wrapper of the pet information, with the fields called Id, Name and Price representing the identification, name and price properties of the pet, respectively:
public class Pet
{
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _category;
public int Category
{
get { return _category; }
set { _category = value; }
}
private int _color;
public int Color
{
get { return _color; }
set { _color = value; }
}
private int _price;
public int Price
{
get { return _price; }
set { _price = value; }
}
public Pet()
{
}
public Pet(int id, string name, string category, string color, int price)
{
this._id = id;
this._name = name;
this._category = category;
this._color = color;
this._price = price;
}
}
From now on, we'll define the necessary WebMethods. The first one named GetPets() is used to return all current commodities stored in the warehouse:
[WebMethod]
public Pet[] GetPets()
{
return Pets.ToArray();
}
As is hinted from the above figures, we should also define a WebMethod named Order which is used to process pet information posted from the client side shopping cart and then return the final result. Here, the one parameter of the Order method is a Dictionary structure; the Key corresponds to the Id of the pet and Value to the number of the selected pet inside the shopping cart:
[WebMethod]
public string Order(Dictionary<string, int> petsToBeOrdered)
{
// total number of pets
int totalQuantity = 0;
// total price of pets
int totalPrice = 0;
//figure out the total price of all the pets to buy
foreach (KeyValuePair<string, int> petQuantity in petsToBeOrdered)
{
foreach (Pet pet in Pets)
{
if (pet.Id.ToString() == petQuantity.Key)
{
totalQuantity += petQuantity.Value;
totalPrice += (pet.Price * petQuantity.Value);
break;
}
}
}
//return the dealt result
return string.Format(
"You've ordered {0} pet(s) at the total price {1}. Thank you!",
totalQuantity,
totalPrice
);
}
That's all we have room for right now. In the second part, we will look at the really interesting aspects -- defining the drag and drop behaviors to achieve our goal. You won't want to miss it!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |