117
|
1 using System;
|
|
2 using System.Diagnostics.Contracts;
|
|
3 using System.IO;
|
|
4 using System.Runtime.Serialization.Json;
|
|
5 using System.ServiceModel.Activation;
|
|
6 using System.ServiceModel.Web;
|
|
7 using SilverlightAsyncRestWcf.Common;
|
|
8
|
|
9 namespace SilverlightAsyncRestWcf.Services
|
|
10 {
|
|
11 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
|
|
12 public class CarService : IService<Car>
|
|
13 {
|
|
14 private readonly IRepository<Car> _repo;
|
|
15 private readonly DataContractJsonSerializer _serializer;
|
|
16
|
|
17 public CarService()
|
|
18 {
|
|
19 _repo = new FakeCarRepository();
|
|
20 _serializer = new DataContractJsonSerializer(typeof(Car));
|
|
21 }
|
|
22
|
|
23 public CarService(IRepository<Car> repo)
|
|
24 {
|
|
25 _repo = repo;
|
|
26 }
|
|
27
|
|
28 [WebGet(UriTemplate = "Car/{id}")]
|
|
29 public string Get(string id)
|
|
30 {
|
|
31 Contract.Requires((bool)(id != null), "id != null");
|
|
32 return _repo.GetById(id).ToJSON();
|
|
33 }
|
|
34
|
|
35 [WebInvoke(UriTemplate = "Car", Method = "POST")]
|
|
36 public void Insert(Car car)
|
|
37 {
|
|
38 Contract.Requires((bool)(car != null), "car != null");
|
|
39 _repo.Insert(car);
|
|
40 }
|
|
41
|
|
42 [WebInvoke(UriTemplate = "Car/{id}", Method = "PUT")]
|
|
43 public void Update(string id, Car car)
|
|
44 {
|
|
45 Contract.Requires((bool)(car != null), "car != null");
|
|
46 _repo.Update(car);
|
|
47 }
|
|
48
|
|
49 [WebInvoke(UriTemplate = "Car({id})", Method = "DELETE")]
|
|
50 public void Delete(string id)
|
|
51 {
|
|
52 Contract.Requires((bool)(id != null), "id != null");
|
|
53 _repo.Delete(id);
|
|
54 }
|
|
55 }
|
|
56 }
|