Mercurial > silverbladetech
diff SilverlightAsyncRestWcf/SilverlightAsyncRestWcf.Services/CarService.cs @ 117:9eadccc3b46c
REST working, next step unit tests
author | stevenh7776 |
---|---|
date | Sun, 27 May 2012 15:06:35 +0100 |
parents | |
children | fa4ba8943048 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SilverlightAsyncRestWcf/SilverlightAsyncRestWcf.Services/CarService.cs Sun May 27 15:06:35 2012 +0100 @@ -0,0 +1,56 @@ +using System; +using System.Diagnostics.Contracts; +using System.IO; +using System.Runtime.Serialization.Json; +using System.ServiceModel.Activation; +using System.ServiceModel.Web; +using SilverlightAsyncRestWcf.Common; + +namespace SilverlightAsyncRestWcf.Services +{ + [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] + public class CarService : IService<Car> + { + private readonly IRepository<Car> _repo; + private readonly DataContractJsonSerializer _serializer; + + public CarService() + { + _repo = new FakeCarRepository(); + _serializer = new DataContractJsonSerializer(typeof(Car)); + } + + public CarService(IRepository<Car> repo) + { + _repo = repo; + } + + [WebGet(UriTemplate = "Car/{id}")] + public string Get(string id) + { + Contract.Requires((bool)(id != null), "id != null"); + return _repo.GetById(id).ToJSON(); + } + + [WebInvoke(UriTemplate = "Car", Method = "POST")] + public void Insert(Car car) + { + Contract.Requires((bool)(car != null), "car != null"); + _repo.Insert(car); + } + + [WebInvoke(UriTemplate = "Car/{id}", Method = "PUT")] + public void Update(string id, Car car) + { + Contract.Requires((bool)(car != null), "car != null"); + _repo.Update(car); + } + + [WebInvoke(UriTemplate = "Car({id})", Method = "DELETE")] + public void Delete(string id) + { + Contract.Requires((bool)(id != null), "id != null"); + _repo.Delete(id); + } + } +}