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