comparison SilverlightAsyncRestWcf/SilverlightAsyncRestWcf.Services/CarService.cs @ 118:fa4ba8943048

final version
author stevenh7776
date Sun, 27 May 2012 19:53:23 +0100
parents 9eadccc3b46c
children
comparison
equal deleted inserted replaced
117:9eadccc3b46c 118:fa4ba8943048
1 using System; 1 using System;
2 using System.Collections.Generic;
2 using System.Diagnostics.Contracts; 3 using System.Diagnostics.Contracts;
3 using System.IO; 4 using System.IO;
4 using System.Runtime.Serialization.Json;
5 using System.ServiceModel.Activation; 5 using System.ServiceModel.Activation;
6 using System.ServiceModel.Web; 6 using System.ServiceModel.Web;
7 using System.Text;
7 using SilverlightAsyncRestWcf.Common; 8 using SilverlightAsyncRestWcf.Common;
8 9
9 namespace SilverlightAsyncRestWcf.Services 10 namespace SilverlightAsyncRestWcf.Services
10 { 11 {
11 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 12 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
12 public class CarService : IService<Car> 13 public class CarService : IService<Car>
13 { 14 {
14 private readonly IRepository<Car> _repo; 15 private readonly IRepository<Car> _repo;
15 private readonly DataContractJsonSerializer _serializer;
16 16
17 public CarService() 17 public CarService()
18 { 18 {
19 _repo = new FakeCarRepository(); 19 _repo = new FakeCarRepository();
20 _serializer = new DataContractJsonSerializer(typeof(Car));
21 } 20 }
22 21
23 public CarService(IRepository<Car> repo) 22 public CarService(IRepository<Car> repo)
24 { 23 {
25 _repo = repo; 24 _repo = repo;
26 } 25 }
27 26
28 [WebGet(UriTemplate = "Car/{id}")] 27 [WebGet(UriTemplate = "Cars", ResponseFormat = WebMessageFormat.Json)]
29 public string Get(string id) 28 public IList<Car> GetAll()
30 { 29 {
31 Contract.Requires((bool)(id != null), "id != null"); 30 var cars = _repo.GetAll();
32 return _repo.GetById(id).ToJSON(); 31 return cars;
32 }
33
34 [WebGet(UriTemplate = "Car/{id}", ResponseFormat = WebMessageFormat.Json)]
35 public Car Get(string id)
36 {
37 var car = _repo.GetById(id);
38 return car;
33 } 39 }
34 40
35 [WebInvoke(UriTemplate = "Car", Method = "POST")] 41 [WebInvoke(UriTemplate = "Car", Method = "POST")]
36 public void Insert(Car car) 42 public void Insert(Car car)
37 { 43 {
38 Contract.Requires((bool)(car != null), "car != null");
39 _repo.Insert(car); 44 _repo.Insert(car);
40 } 45 }
41 46
42 [WebInvoke(UriTemplate = "Car/{id}", Method = "PUT")] 47 [WebInvoke(UriTemplate = "Car/{id}", Method = "PUT")]
43 public void Update(string id, Car car) 48 public void Update(string id, Car car)
44 { 49 {
45 Contract.Requires((bool)(car != null), "car != null");
46 _repo.Update(car); 50 _repo.Update(car);
47 } 51 }
48 52
49 [WebInvoke(UriTemplate = "Car({id})", Method = "DELETE")] 53 [WebInvoke(UriTemplate = "Car/{id}", Method = "DELETE")]
50 public void Delete(string id) 54 public void Delete(string id)
51 { 55 {
52 Contract.Requires((bool)(id != null), "id != null");
53 _repo.Delete(id); 56 _repo.Delete(id);
54 } 57 }
55 } 58 }
56 } 59 }