Nancy + rest mow2012
View more presentations from Christian Horsdal.
11 public class DeleteDeviceWriteRequest : DataStoreWriteRequest
12 {
13 private readonly Device device;
14
15 public DeleteDeviceWriteRequest(Device device)
16 {
17 this.device = device;
18 }
19
20 protected override void DoExecute()
21 {
22 // NHibernate trickery cut out for brewity
23 Session.Delete(device);
24 }
25 }
229 var deleteDeviceRequest = new DeleteDeviceWriteRequest(meter);
230 dataStore.Execute(deleteDeviceRequest);
8 public interface IDataStore
9 {
10 void Execute(DataStoreWriteRequest req);
11 T Execute<T>(DataStoreReadRequest<T> req);
12 T Get<T>(long id) where T : class;
13 T Get<T>(Expression<Func<T,bool>> where) where T : class;
14 void Add<T>(T entity) where T : class;
15 void Delete<T>(T entity) where T : class;
16 int Count<T>(Expression<Func<T, bool>> where = null) where T : class;
17 }
173 public void Execute(DataStoreWriteRequest req)
174 {
175 WithTryCatch(() => req.ExecuteWith(Session));
176 }
177
178 public T Execute<T>(DataStoreReadRequest<T> req)
179 {
180 return WithTryCatch(() => req.ExecuteWith(Session));
181 }
182
183 private void WithTryCatch(Action operation)
184 {
185 WithTryCatch(() => { operation(); return 0; });
186 }
187
188 private TResult WithTryCatch<TResult>(Func<TResult> operation)
189 {
190 try
191 {
192 return operation();
193 }
194 catch (Exception)
195 {
196 Dispose(); // ISession must be disposed
197 throw;
198 }
199 }
5 public abstract class DataStoreWriteRequest
6 {
7 protected ISession Session { get; private set; }
8
9 internal void ExecuteWith(ISession seesion)
10 {
11 Session = seesion;
12 DoExecute();
13 }
14
15 protected abstract void DoExecute();
16 }
39 [Test]
40 public void ShouldDeleteDeviceWithNoRelations()
41 {
42 var device = new Device();
43 using (var arrangeUnitOfWork = CreateUnitOfWork())
44 {
45 arrangeUnitOfWork.Add(device);
46 }
47
48 using (var actUnitOfWork = CreateUnitOfWork())
49 {
50 var sut = new DeleteDeviceWriteRequest(device);
51 actUnitOfWork.Execute(sut);
52 }
53
54 using (var assertUnitOfWork = CreateUnitOfWork())
55 {
56 Assert.That(assertUnitOfWork.Get<Device>(device.Id), Is.Null);
57 }
58 }
100 [Test]
101 public void DeleteDeviceRestCallExectuesDeleteOnDevice()
102 {
103 var dataStore = mock.StrictMock<IDataStore>();
104 var sut = new RestApi(dataStore);
105
106 var device = new Device { Id = 123 };
107
108 Expect.Call(unitOfWork.Get<Device>(device.Id)).Return(device);
109 Expect.Call(() =>
110 unitOfWork.Execute(
111 Arg<DeleteMeterWriteRequest>
112 .Is.Equal(new DeleteMeterWriteRequest(device))));
113
114 mock.ReplayAll();
115
116 sut.DeleteMeter(device.Id.ToString());
117
118 mock.VerifyAll();
119 }
120
9 public interface IDeviceRepository
10 {
11 TDeviceType Get<TDeviceType>(long id) where TDeviceType : Device;
12 TDeviceType FindByControllerIdentifier<TDeviceType>(string controllerIdentifer)
13 where TDeviceType : Device;
14
15 IList<TDeviceType> GetAll<TDeviceType>(int offset, int max,
16 DateTimeOffset? createdAfter = null, DateTimeOffset? updatedAfter = null,
17 DateTimeOffset? disconnectedAfter = null)
18 where TDeviceType : Device;
19
20 int Count<TDeviceType>(DateTimeOffset? createdAfter = null,
21 DateTimeOffset? updatedAfter = null, DateTimeOffset? disconnectedAfter = null)
22 where TDeviceType: Device;
23
24 void Add<TDeviceType>(TDeviceType device) where TDeviceType : Device;
25 IList<TDeviceType> GetAll<TDeviceType>() where TDeviceType : Device;
26 IList<TDeviceType> GetAll<TDeviceType>(long[] deviceIds) where TDeviceType : Device;
27 IList<TDeviceType> FindByControllerIdentifiers<TDeviceType>(string[] controllerIdentifiers)
28 where TDeviceType : Device;
29 IList<NodeDevice> FindNodesDevicesForRootDevice(string controllerIdentifier,
30 bool active = false);
31 ICollection<NodeDevice> FindNodeDevicesForRootDevice(long rootDeviceId, bool active = false);
32
33 IEnumerable<DeviceLink> GetTopology<T>(long id, bool active) where T : Device;
34 DeviceLink GetDeviceLink(string controllerIdentifierA, string controllerIdentifierB);
35 DeviceLink GetDeviceLink(long id);
36
37 void AddDeviceLink(DeviceLink link);
38
39 IList<Route> FindRoutesByDeviceLink(DeviceLink deviceLink);
40 void Delete(Device device);
41 }
11 public class DeleteDeviceRequest : NHibernateWriteRequest
12 {
13 private readonly Device device;
14
15 public DeleteDeviceRequest(Device device)
16 {
17 this.device = device;
18 }
19
20 protected override void DoExecute()
21 {
22 var links =
23 Session.QueryOver<DeviceLinkReference>().Where(x => x.ControllerIdentifier == device.ControllerIdentifier).
24 List();
25
26 links.ForEach(x => x.Device = null);
27
28 DeleteStatistics();
29 new DeleteRoutesWriteRequest(device.Routes).ExecuteWith(DataStore);
30 DeleteFromGroup();
31 DeleteFromOrder();
32 DeleteProfiles();
33 DataStore.Delete(device);
34 }
35
36 // more private methods...
37
38