Contact Manager Web API

This sample uses ASP.NET Web API to build a simple contact manager application. The application consists of a contact manager web API that is used by an ASP.NET MVC application, a Windows Phone application, and a Windows 8 app to display and manage a list of contacts.

C# (1.9 MB)
 
 
 
 
 
4.4 Star
(37)
93,797 times
Add to favorites
8/16/2012
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
using ContactManager.Models;

namespace ContactManager.Models
{
    using System.Collections.Generic;
    using System.Linq;
    
    public class SampleContactRepository : IContactRepository
    {
        private int nextContactID;

        private IList<Contact> contacts; 

        public SampleContactRepository()
        {
            contacts = new List<Contact>(SampleData.Contacts);
            nextContactID = contacts.Count + 1;
        }

        public void Update(Contact updatedContact)
        {
            var contact = this.Get(updatedContact.ContactId);
            contact.Name = updatedContact.Name;
            contact.Address = updatedContact.Address;
            contact.City = updatedContact.City;
            contact.State = updatedContact.State;
            contact.Zip = updatedContact.Zip;
            contact.Email = updatedContact.Email;
            contact.Twitter = updatedContact.Twitter;
        }

        public Contact Get(int id)
        {
            return contacts.SingleOrDefault(c => c.ContactId == id);
        }

        public IQueryable<Contact> GetAll()
        {
            return contacts.AsQueryable();
        }

        public void Post(Contact contact)
        {
            contact.ContactId = nextContactID++;
            contacts.Add(contact);
        }

        public Contact Delete(int id)
        {
            var contact = this.Get(id);
            contacts.Remove(contact);
            return contact;
        }
    }
}