Browse Source

added virtual method to do eager loading

tags/v0.1.5
Robin Thoni 8 years ago
parent
commit
cf5217e624
1 changed files with 36 additions and 20 deletions
  1. 36
    20
      Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs

+ 36
- 20
Luticate2.Utils/DataAccess/LuEfCrudDataAccess.cs View File

50
             return model => true;
50
             return model => true;
51
         }
51
         }
52
 
52
 
53
-        protected Func<TDboRead, T> GetIdFunc<T>()
54
-        {
55
-            return x => ((dynamic) x).Id;
56
-        }
57
-
58
-        protected TModel GetModelFromTUpdate(TDboUpdate obj, TModel model)
59
-        {
60
-            EditModelFromTUpdate(obj, model);
61
-            return model;
62
-        }
63
-
64
         protected virtual object GetId(TId id)
53
         protected virtual object GetId(TId id)
65
         {
54
         {
66
             var s = id as string;
55
             var s = id as string;
77
             return id;
66
             return id;
78
         }
67
         }
79
 
68
 
69
+        protected virtual IQueryable<TModel> GetGetQueryable()
70
+        {
71
+            return Table;
72
+        }
73
+
74
+        protected virtual IQueryable<TModel> GetEditQueryable()
75
+        {
76
+            return Table;
77
+        }
78
+
79
+        protected virtual IQueryable<TModel> GetDeleteQueryable()
80
+        {
81
+            return Table;
82
+        }
83
+
84
+        protected Func<TDboRead, T> GetIdFunc<T>()
85
+        {
86
+            return x => ((dynamic) x).Id;
87
+        }
88
+
89
+        protected TModel GetModelFromTUpdate(TDboUpdate obj, TModel model)
90
+        {
91
+            EditModelFromTUpdate(obj, model);
92
+            return model;
93
+        }
94
+
80
         protected LuResult<T> GetNotFoundResult<T>()
95
         protected LuResult<T> GetNotFoundResult<T>()
81
         {
96
         {
82
             return LuResult<T>.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", "");
97
             return LuResult<T>.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", "");
84
 
99
 
85
         protected IQueryable<TModel> GetFilterQuery(LuFilterDbo filter)
100
         protected IQueryable<TModel> GetFilterQuery(LuFilterDbo filter)
86
         {
101
         {
87
-            return Table.Where(GetFilterExpression(filter));
102
+            return GetGetQueryable().Where(GetFilterExpression(filter));
88
         }
103
         }
89
 
104
 
90
 
105
 
138
         {
153
         {
139
             return Execute(() =>
154
             return Execute(() =>
140
             {
155
             {
141
-                var model = Table.FirstOrDefault(predicate);
156
+                var model = GetGetQueryable().FirstOrDefault(predicate);
142
                 if (model == default(TModel))
157
                 if (model == default(TModel))
143
                 {
158
                 {
144
                     return GetNotFoundResult<TDboRead>();
159
                     return GetNotFoundResult<TDboRead>();
164
         }
179
         }
165
 
180
 
166
 
181
 
167
-        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<DbSet<TModel>, IOrderedQueryable<TModel>> orderBy,
182
+        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
168
             Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
183
             Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
169
             params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
184
             params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
170
         {
185
         {
171
             return Execute(() =>
186
             return Execute(() =>
172
             {
187
             {
173
-                var count = Table.Count(predicate);
174
-                var ordered = orderBy(Table);
188
+                var queryable = GetGetQueryable();
189
+                var count = queryable.Count(predicate);
190
+                var ordered = orderBy(queryable);
175
                 foreach (var func in otherOrderBy)
191
                 foreach (var func in otherOrderBy)
176
                 {
192
                 {
177
                     ordered = func(ordered);
193
                     ordered = func(ordered);
195
                     IOrderedQueryable<TModel>>>(expression => (t => t.ThenBy(expression))).ToArray());
211
                     IOrderedQueryable<TModel>>>(expression => (t => t.ThenBy(expression))).ToArray());
196
         }
212
         }
197
 
213
 
198
-        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<DbSet<TModel>, IOrderedQueryable<TModel>> orderBy,
214
+        public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
199
             int page = 0, int perPage = int.MaxValue, params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
215
             int page = 0, int perPage = int.MaxValue, params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
200
         {
216
         {
201
             return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
217
             return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
250
 
266
 
251
         public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
267
         public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
252
         {
268
         {
253
-            return GetMultiple(orderBy, Table, page, perPage);
269
+            return GetMultiple(orderBy, GetGetQueryable(), page, perPage);
254
         }
270
         }
255
 
271
 
256
 
272
 
260
         {
276
         {
261
             return Execute(() =>
277
             return Execute(() =>
262
             {
278
             {
263
-                var models = Table.Where(predicate);
279
+                var models = GetEditQueryable().Where(predicate);
264
                 var editedDbos = new List<TDboRead>();
280
                 var editedDbos = new List<TDboRead>();
265
                 foreach (var model in models)
281
                 foreach (var model in models)
266
                 {
282
                 {
337
         {
353
         {
338
             return Execute(() =>
354
             return Execute(() =>
339
             {
355
             {
340
-                var models = Table.Where(predicate).ToList();
356
+                var models = GetDeleteQueryable().Where(predicate).ToList();
341
                 Table.RemoveRange(models);
357
                 Table.RemoveRange(models);
342
                 Db.SaveChanges();
358
                 Db.SaveChanges();
343
                 var dbos = models.Select(GetDboFromModel);
359
                 var dbos = models.Select(GetDboFromModel);

Loading…
Cancel
Save