CRUD stands for "Create, Read, Update and Delete," which are the four basic database operations. Many HTTP services also model CRUD operations through REST or REST-like APIs.
Introduction
This article explains the walk-through of how to perform "CRUD" operations in a HTTP service using web API
Lets start creating a very simple web API to manage a list of Books. Each Book will contain a name, price,category and Book ID.
Step 1: Create a New Project Open visual studio 2013 -> click on File -> New Project -> Create new ASP.NET WebApplication ->
select web API and click ok
Step 2 : Now Lets add a Model which represents the data of our Application
In Solution Explorer, right-click the Models folder-->select add-->then select Class and name it as Books as shown
Step 3 :Add the Following Properties to the Books class
namespace BooksStore.Models
{
public class Books
{
public int Id { get; set; }
public string Author { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
}
Step 4 :Add the Repository class to the Models Folder
In the Templates pane, select Installed Templates and expand the C# node. Under C#, select Code. In the list of code templates, select Interface. Name the interface "
IBooksRepository
".
Here we store a collection of products by separating the collection from our service implementation......This helps us to change the backing store without rewriting the service class, which is called as repository pattern
using System.Collections.Generic;
namespace BooksStore.Models
{
public class IBooksRepository
{
IEnumerable<Books> GetAll();
Books Get(int id);
Books Add(Books item);
void Remove(int id);
bool Update(Books item);
}
}
Step 5: Now lets add another class to the Models folder, named "
BooksRepository
." This class will implement the
IBooksRespository
interface. Add the following code to it
using System;
using System.Collections.Generic;
namespace BooksStore.Models
{
public class BooksRepository
{
private List<Books> books = new List<Books>();
private int _nextId = 1;
public BooksRepository()
{
Add(new Books { Author = "Sheo Narayan", Category = "C#", Price = 2.49M });
Add(new Books { Author = "Shivprasad Koirala", Category = "Interview Questions", Price = 5.65M });
Add(new Books { Author = "Vakul More", Category = "Sql Server", Price = 18.89M });
}
public IEnumerable<Books> GetAll()
{
return books;
}
public Books Get(int id)
{
return books.Find(p => p.Id == id);
}
public Books Add(Books item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
item.Id = _nextId++;
books.Add(item);
return item;
}
public void Remove(int id)
{
books.RemoveAll(p => p.Id == id);
}
public bool Update(Books item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
int index = books.FindIndex(p => p.Id == item.Id);
if (index == -1)
{
return false;
}
books.RemoveAt(index);
books.Add(item);
return true;
}
}
}
Conclusion
Now we are ready with the model in the next part we will see the API controller and the crud Operations