annotate SilverlightAsyncRestWcf/SilverlightAsyncRestWcf.Services/FakeCarRepository.cs @ 117:9eadccc3b46c

REST working, next step unit tests
author stevenh7776
date Sun, 27 May 2012 15:06:35 +0100
parents
children fa4ba8943048
rev   line source
117
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
1 using System;
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
2 using System.Collections.Generic;
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
3 using System.Linq;
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
4 using SilverlightAsyncRestWcf.Common;
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
5
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
6 namespace SilverlightAsyncRestWcf.Services
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
7 {
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
8 public class FakeCarRepository : IRepository<Car>
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
9 {
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
10 private readonly IList<Car> _data;
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
11
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
12 public FakeCarRepository()
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
13 {
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
14 _data = new List<Car>
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
15 {
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
16 new Car() {Id = 1, Make = "Rolls Royce", Model = "Bentley", Year = 1996},
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
17 new Car() {Id = 2, Make = "Ford", Model = "Fiesta", Year = 1996},
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
18 new Car() {Id = 3, Make = "Mercedes", Model = "C Class", Year = 1996},
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
19 new Car() {Id = 4, Make = "BMW", Model = "7 Series", Year = 1996},
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
20 new Car() {Id = 5, Make = "Jaguar", Model = "XKS", Year = 1996},
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
21 new Car() {Id = 6, Make = "Audi", Model = "R8", Year = 1996}
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
22 };
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
23 }
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
24
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
25 public Car GetById(string id)
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
26 {
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
27 int number;
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
28 return Int32.TryParse(id, out number)
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
29 ? _data.SingleOrDefault(c => c.Id == number)
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
30 : null;
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
31 }
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
32
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
33 public IQueryable<Car> GetAll()
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
34 {
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
35 return _data.AsQueryable();
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
36 }
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
37
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
38 public void Insert(Car entity)
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
39 {
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
40 _data.Add(entity);
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
41 }
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
42
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
43 public void Update(Car entity)
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
44 {
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
45 var lookup = GetById(entity.Id.ToString());
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
46 if (lookup == null) throw new Exception("Car not found");
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
47
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
48 lookup.Make = entity.Make;
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
49 lookup.Model = entity.Model;
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
50 lookup.Year = entity.Year;
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
51 }
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
52
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
53 public void Delete(string id)
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
54 {
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
55 var lookup = GetById(id);
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
56 if (lookup == null) throw new Exception("Car not found");
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
57 _data.Remove(lookup);
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
58 }
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
59 }
9eadccc3b46c REST working, next step unit tests
stevenh7776
parents:
diff changeset
60 }