瀏覽代碼

made all curd methods virtual

tags/v0.2.4
Robin Thoni 8 年之前
父節點
當前提交
7cf9895a76

+ 2
- 2
Luticate2.Utils/Business/LuCrudBusiness.cs 查看文件

@@ -60,7 +60,7 @@ namespace Luticate2.Utils.Business
60 60
             return LuResult<TDboUpdate>.Ok(update);
61 61
         }
62 62
 
63
-        protected LuResult<TDboUpdate> GetAndCheckEdit(TId id, TDboUpdate update)
63
+        protected virtual LuResult<TDboUpdate> GetAndCheckEdit(TId id, TDboUpdate update)
64 64
         {
65 65
             var res = GetSingleById(id);
66 66
             if (!res)
@@ -138,7 +138,7 @@ namespace Luticate2.Utils.Business
138 138
         public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0,
139 139
             int perPage = int.MaxValue)
140 140
         {
141
-            return DataAccess.GetMultiple(orderBy, page, perPage);
141
+            return GetMultiple(orderBy, new LuFilterDbo { Query = ""}, page, perPage);
142 142
         }
143 143
 
144 144
 

+ 2
- 2
Luticate2.Utils/Controllers/LuController.cs 查看文件

@@ -10,7 +10,7 @@ namespace Luticate2.Utils.Controllers
10 10
 
11 11
         protected IDictionary<object, object> LuItems => HttpContext.GetLuItems();
12 12
 
13
-        protected LuApiWrapperDbo<T> Handle<T>(LuResult<T> result)
13
+        protected virtual LuApiWrapperDbo<T> Handle<T>(LuResult<T> result)
14 14
         {
15 15
             if (result)
16 16
             {
@@ -25,7 +25,7 @@ namespace Luticate2.Utils.Controllers
25 25
             throw new LuResultException(result.To<object>());
26 26
         }
27 27
 
28
-        protected LuApiWrapperDbo<LuBoxedValueDbo<T>> HandleBoxed<T>(LuResult<T> result)
28
+        protected virtual LuApiWrapperDbo<LuBoxedValueDbo<T>> HandleBoxed<T>(LuResult<T> result)
29 29
         {
30 30
             return Handle(result.To(arg => new LuBoxedValueDbo<T>
31 31
             {

+ 5
- 5
Luticate2.Utils/Controllers/LuCrudController.cs 查看文件

@@ -23,35 +23,35 @@ namespace Luticate2.Utils.Controllers
23 23
 
24 24
         [HttpGet]
25 25
         [Route("[controller]/{id}")]
26
-        public LuApiWrapperDbo<TDboRead> GetSingleById([Required]TId id)
26
+        public virtual LuApiWrapperDbo<TDboRead> GetSingleById([Required]TId id)
27 27
         {
28 28
             return Handle(Busines.GetSingleById(id));
29 29
         }
30 30
 
31 31
         [HttpGet]
32 32
         [Route("[controller]")]
33
-        public LuApiWrapperDbo<LuPaginatedDbo<TDboRead>> GetMultiple([Required]LuPaginatedRequestDbo request)
33
+        public virtual LuApiWrapperDbo<LuPaginatedDbo<TDboRead>> GetMultiple([Required]LuPaginatedRequestDbo request)
34 34
         {
35 35
             return Handle(Busines.GetMultiple(request.OrderBy, request.Filter, request.Page, request.PerPage));
36 36
         }
37 37
 
38 38
         [HttpPost]
39 39
         [Route("[controller]")]
40
-        public LuApiWrapperDbo<TDboRead> AddDbo([FromBody][Required]TDboCreate data)
40
+        public virtual LuApiWrapperDbo<TDboRead> AddDbo([FromBody][Required]TDboCreate data)
41 41
         {
42 42
             return Handle(Busines.AddDbo(data));
43 43
         }
44 44
 
45 45
         [HttpPost]
46 46
         [Route("[controller]/{id}")]
47
-        public LuApiWrapperDbo<TDboRead> EditSingleByIdDbo([Required]TId id, [FromBody][Required]TDboUpdate data)
47
+        public virtual LuApiWrapperDbo<TDboRead> EditSingleByIdDbo([Required]TId id, [FromBody][Required]TDboUpdate data)
48 48
         {
49 49
             return Handle(Busines.EditSingleByIdDbo(id, data));
50 50
         }
51 51
 
52 52
         [HttpDelete]
53 53
         [Route("[controller]/{id}")]
54
-        public LuApiWrapperDbo<TDboRead> Delete([Required]TId id)
54
+        public virtual LuApiWrapperDbo<TDboRead> Delete([Required]TId id)
55 55
         {
56 56
             return Handle(Busines.DeleteSingleByIdDbo(id));
57 57
         }

+ 36
- 36
Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs 查看文件

@@ -91,23 +91,23 @@ namespace Luticate2.Utils.DataAccess
91 91
             return table;
92 92
         }
93 93
 
94
-        protected Func<TDboRead, T> GetIdFunc<T>()
94
+        protected virtual Func<TDboRead, T> GetIdFunc<T>()
95 95
         {
96 96
             return x => ((dynamic) x).Id;
97 97
         }
98 98
 
99
-        protected TModel GetModelFromTUpdate(TDboUpdate obj, TModel model)
99
+        protected virtual TModel GetModelFromTUpdate(TDboUpdate obj, TModel model)
100 100
         {
101 101
             EditModelFromTUpdate(obj, model);
102 102
             return model;
103 103
         }
104 104
 
105
-        protected LuResult<T> GetNotFoundResult<T>()
105
+        protected virtual LuResult<T> GetNotFoundResult<T>()
106 106
         {
107 107
             return LuResult<T>.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", "");
108 108
         }
109 109
 
110
-        protected IQueryable<TModel> GetFilterQuery(LuFilterDbo filter, TDbContext db, DbSet<TModel> table)
110
+        protected virtual IQueryable<TModel> GetFilterQuery(LuFilterDbo filter, TDbContext db, DbSet<TModel> table)
111 111
         {
112 112
             return GetGetQueryable(db, table).Where(GetFilterExpression(filter));
113 113
         }
@@ -115,7 +115,7 @@ namespace Luticate2.Utils.DataAccess
115 115
 
116 116
 
117 117
 
118
-        public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
118
+        public virtual LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
119 119
         {
120 120
             var models = objs.Select(GetModelFromTCreate).ToList();
121 121
             var addRes = Execute((db, table) =>
@@ -135,30 +135,30 @@ namespace Luticate2.Utils.DataAccess
135 135
             return GetMultiple(models, returnFunc);
136 136
         }
137 137
 
138
-        public LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
138
+        public virtual LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
139 139
         {
140 140
             return Add(new List<TDboCreate> {obj}, list => returnFunc(list.First()));
141 141
         }
142 142
 
143 143
 
144
-        public LuResult<IEnumerable<TId>> AddId(IEnumerable<TDboCreate> objs)
144
+        public virtual LuResult<IEnumerable<TId>> AddId(IEnumerable<TDboCreate> objs)
145 145
         {
146 146
             var func = GetIdFunc<TId>();
147 147
             return Add(objs, list => list.Select(func));
148 148
         }
149 149
 
150
-        public LuResult<TId> AddId(TDboCreate obj)
150
+        public virtual LuResult<TId> AddId(TDboCreate obj)
151 151
         {
152 152
             return AddId(new List<TDboCreate> {obj}).To(list => list.First());
153 153
         }
154 154
 
155 155
 
156
-        public LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
156
+        public virtual LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
157 157
         {
158 158
             return Add(obj, read => read);
159 159
         }
160 160
 
161
-        public LuResult<TDboRead> AddDbo(TDboCreate obj)
161
+        public virtual LuResult<TDboRead> AddDbo(TDboCreate obj)
162 162
         {
163 163
             return AddDbo(new List<TDboCreate> {obj}).To(list => list.First());
164 164
         }
@@ -166,7 +166,7 @@ namespace Luticate2.Utils.DataAccess
166 166
 
167 167
 
168 168
 
169
-        public LuResult<TDboRead> GetSingle(Expression<Func<TModel, bool>> predicate)
169
+        public virtual LuResult<TDboRead> GetSingle(Expression<Func<TModel, bool>> predicate)
170 170
         {
171 171
             return Execute((db, table) =>
172 172
             {
@@ -180,12 +180,12 @@ namespace Luticate2.Utils.DataAccess
180 180
             });
181 181
         }
182 182
 
183
-        public LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
183
+        public virtual LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
184 184
         {
185 185
             return GetSingle(GetExpression(keys));
186 186
         }
187 187
 
188
-        public LuResult<TDboRead> GetSingleById(TId id)
188
+        public virtual LuResult<TDboRead> GetSingleById(TId id)
189 189
         {
190 190
             var guid = GetId(id);
191 191
             if (guid == null)
@@ -195,7 +195,7 @@ namespace Luticate2.Utils.DataAccess
195 195
             return GetSingleByKeys(new KeyValuePair<string, object>("id", guid));
196 196
         }
197 197
 
198
-        protected LuResult<T> GetMultiple<T>(IList<TModel> models, Func<IEnumerable<TDboRead>, T> returnFunc)
198
+        protected virtual LuResult<T> GetMultiple<T>(IList<TModel> models, Func<IEnumerable<TDboRead>, T> returnFunc)
199 199
         {
200 200
             var dbos = new List<TDboRead>();
201 201
             foreach (var model in models)
@@ -211,7 +211,7 @@ namespace Luticate2.Utils.DataAccess
211 211
             return LuResult<T>.Ok(res);
212 212
         }
213 213
 
214
-        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
214
+        public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
215 215
             Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
216 216
             params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
217 217
         {
@@ -234,7 +234,7 @@ namespace Luticate2.Utils.DataAccess
234 234
             });
235 235
         }
236 236
 
237
-        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
237
+        public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
238 238
             Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
239 239
             params Expression<Func<TModel, object>>[] otherOrderBy)
240 240
         {
@@ -243,19 +243,19 @@ namespace Luticate2.Utils.DataAccess
243 243
                     IOrderedQueryable<TModel>>>(expression => (t => t.ThenBy(expression))).ToArray());
244 244
         }
245 245
 
246
-        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
246
+        public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
247 247
             int page = 0, int perPage = int.MaxValue, params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
248 248
         {
249 249
             return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
250 250
         }
251 251
 
252
-        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
252
+        public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
253 253
             int page = 0, int perPage = int.MaxValue, params Expression<Func<TModel, object>>[] otherOrderBy)
254 254
         {
255 255
             return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
256 256
         }
257 257
 
258
-        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, Func<TDbContext, DbSet<TModel>, IQueryable<TModel>> getQueryable,
258
+        public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, Func<TDbContext, DbSet<TModel>, IQueryable<TModel>> getQueryable,
259 259
             int page = 0, int perPage = int.MaxValue)
260 260
         {
261 261
             return Execute((db, table) =>
@@ -291,20 +291,20 @@ namespace Luticate2.Utils.DataAccess
291 291
             });
292 292
         }
293 293
 
294
-        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, LuFilterDbo filter,
294
+        public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, LuFilterDbo filter,
295 295
             int page = 0, int perPage = int.MaxValue)
296 296
         {
297 297
             return GetMultiple(orderBy, (db, table) => GetFilterQuery(filter, db, table), page, perPage);
298 298
         }
299 299
 
300
-        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
300
+        public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
301 301
         {
302 302
             return GetMultiple(orderBy, GetGetQueryable, page, perPage);
303 303
         }
304 304
 
305 305
 
306 306
 
307
-        public LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
307
+        public virtual LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
308 308
             Func<IEnumerable<TDboRead>, T> returnFunc)
309 309
         {
310 310
             IList<TModel> models = null;
@@ -326,19 +326,19 @@ namespace Luticate2.Utils.DataAccess
326 326
             return GetMultiple(models, returnFunc);
327 327
         }
328 328
 
329
-        public LuResult<IEnumerable<TId>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
329
+        public virtual LuResult<IEnumerable<TId>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
330 330
         {
331 331
             var func = GetIdFunc<TId>();
332 332
             return Edit(predicate, update, reads => reads.Select(func));
333 333
         }
334 334
 
335
-        public LuResult<IEnumerable<TDboRead>> EditDbo(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
335
+        public virtual LuResult<IEnumerable<TDboRead>> EditDbo(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
336 336
         {
337 337
             return Edit(predicate, update, read => read);
338 338
         }
339 339
 
340 340
 
341
-        public LuResult<T> EditSingleById<T>(TId id, Action<TModel> update, Func<TDboRead, T> returnFunc)
341
+        public virtual LuResult<T> EditSingleById<T>(TId id, Action<TModel> update, Func<TDboRead, T> returnFunc)
342 342
         {
343 343
             var guid = GetId(id);
344 344
             if (guid == null)
@@ -349,18 +349,18 @@ namespace Luticate2.Utils.DataAccess
349 349
                 list => returnFunc(list.FirstOrDefault()));
350 350
         }
351 351
 
352
-        public LuResult<TId> EditSingleByIdId(TId id, Action<TModel> update)
352
+        public virtual LuResult<TId> EditSingleByIdId(TId id, Action<TModel> update)
353 353
         {
354 354
             return EditSingleById(id, update, GetIdFunc<TId>());
355 355
         }
356 356
 
357
-        public LuResult<TDboRead> EditSingleByIdDbo(TId id, Action<TModel> update)
357
+        public virtual LuResult<TDboRead> EditSingleByIdDbo(TId id, Action<TModel> update)
358 358
         {
359 359
             return EditSingleById(id, update, read => read);
360 360
         }
361 361
 
362 362
 
363
-        public LuResult<T> EditSingleById<T>(TId id, TDboUpdate update, Func<TDboRead, T> returnFunc)
363
+        public virtual LuResult<T> EditSingleById<T>(TId id, TDboUpdate update, Func<TDboRead, T> returnFunc)
364 364
         {
365 365
             var guid = GetId(id);
366 366
             if (guid == null)
@@ -371,12 +371,12 @@ namespace Luticate2.Utils.DataAccess
371 371
                 model => EditModelFromTUpdate(update, model), list => returnFunc(list.FirstOrDefault()));
372 372
         }
373 373
 
374
-        public LuResult<TId> EditSingleByIdId(TId id, TDboUpdate update)
374
+        public virtual LuResult<TId> EditSingleByIdId(TId id, TDboUpdate update)
375 375
         {
376 376
             return EditSingleById(id, update, GetIdFunc<TId>());
377 377
         }
378 378
 
379
-        public LuResult<TDboRead> EditSingleByIdDbo(TId id, TDboUpdate update)
379
+        public virtual LuResult<TDboRead> EditSingleByIdDbo(TId id, TDboUpdate update)
380 380
         {
381 381
             return EditSingleById(id, update, read => read);
382 382
         }
@@ -384,7 +384,7 @@ namespace Luticate2.Utils.DataAccess
384 384
 
385 385
 
386 386
 
387
-        public LuResult<T> Delete<T>(Expression<Func<TModel, bool>> predicate,
387
+        public virtual LuResult<T> Delete<T>(Expression<Func<TModel, bool>> predicate,
388 388
             Func<IEnumerable<TDboRead>, T> returnFunc)
389 389
         {
390 390
             return Execute((db, table) =>
@@ -401,19 +401,19 @@ namespace Luticate2.Utils.DataAccess
401 401
             });
402 402
         }
403 403
 
404
-        public LuResult<IEnumerable<TId>> DeleteId(Expression<Func<TModel, bool>> predicate)
404
+        public virtual LuResult<IEnumerable<TId>> DeleteId(Expression<Func<TModel, bool>> predicate)
405 405
         {
406 406
             var func = GetIdFunc<TId>();
407 407
             return Delete(predicate, reads => reads.Select(func));
408 408
         }
409 409
 
410
-        public LuResult<IEnumerable<TDboRead>> DeleteDbo(Expression<Func<TModel, bool>> predicate)
410
+        public virtual LuResult<IEnumerable<TDboRead>> DeleteDbo(Expression<Func<TModel, bool>> predicate)
411 411
         {
412 412
             return Delete(predicate, read => read);
413 413
         }
414 414
 
415 415
 
416
-        public LuResult<T> DeleteSingleById<T>(TId id, Func<TDboRead, T> returnFunc)
416
+        public virtual LuResult<T> DeleteSingleById<T>(TId id, Func<TDboRead, T> returnFunc)
417 417
         {
418 418
             var guid = GetId(id);
419 419
             if (guid == null)
@@ -424,13 +424,13 @@ namespace Luticate2.Utils.DataAccess
424 424
                 reads => returnFunc(reads.FirstOrDefault()));
425 425
         }
426 426
 
427
-        public LuResult<TId> DeleteSingleByIdId(TId id)
427
+        public virtual LuResult<TId> DeleteSingleByIdId(TId id)
428 428
         {
429 429
             var func = GetIdFunc<TId>();
430 430
             return DeleteSingleById(id, func);
431 431
         }
432 432
 
433
-        public LuResult<TDboRead> DeleteSingleByIdDbo(TId id)
433
+        public virtual LuResult<TDboRead> DeleteSingleByIdDbo(TId id)
434 434
         {
435 435
             return DeleteSingleById(id, read => read);
436 436
         }

+ 1
- 1
Luticate2.Utils/DataAccess/LuEfDataAccess.cs 查看文件

@@ -19,7 +19,7 @@ namespace Luticate2.Utils.DataAccess
19 19
 
20 20
         protected abstract DbSet<TModel> GetTable(TDbContext db);
21 21
 
22
-        public LuResult<T> Execute<T>(Func<TDbContext, DbSet<TModel>, LuResult<T>> func)
22
+        public virtual LuResult<T> Execute<T>(Func<TDbContext, DbSet<TModel>, LuResult<T>> func)
23 23
         {
24 24
             try
25 25
             {

Loading…
取消
儲存