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,17 +50,6 @@ namespace Luticate2.Utils.DataAccess
50 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 53
         protected virtual object GetId(TId id)
65 54
         {
66 55
             var s = id as string;
@@ -77,6 +66,32 @@ namespace Luticate2.Utils.DataAccess
77 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 95
         protected LuResult<T> GetNotFoundResult<T>()
81 96
         {
82 97
             return LuResult<T>.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", "");
@@ -84,7 +99,7 @@ namespace Luticate2.Utils.DataAccess
84 99
 
85 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,7 +153,7 @@ namespace Luticate2.Utils.DataAccess
138 153
         {
139 154
             return Execute(() =>
140 155
             {
141
-                var model = Table.FirstOrDefault(predicate);
156
+                var model = GetGetQueryable().FirstOrDefault(predicate);
142 157
                 if (model == default(TModel))
143 158
                 {
144 159
                     return GetNotFoundResult<TDboRead>();
@@ -164,14 +179,15 @@ namespace Luticate2.Utils.DataAccess
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 183
             Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
169 184
             params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
170 185
         {
171 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 191
                 foreach (var func in otherOrderBy)
176 192
                 {
177 193
                     ordered = func(ordered);
@@ -195,7 +211,7 @@ namespace Luticate2.Utils.DataAccess
195 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 215
             int page = 0, int perPage = int.MaxValue, params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
200 216
         {
201 217
             return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
@@ -250,7 +266,7 @@ namespace Luticate2.Utils.DataAccess
250 266
 
251 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,7 +276,7 @@ namespace Luticate2.Utils.DataAccess
260 276
         {
261 277
             return Execute(() =>
262 278
             {
263
-                var models = Table.Where(predicate);
279
+                var models = GetEditQueryable().Where(predicate);
264 280
                 var editedDbos = new List<TDboRead>();
265 281
                 foreach (var model in models)
266 282
                 {
@@ -337,7 +353,7 @@ namespace Luticate2.Utils.DataAccess
337 353
         {
338 354
             return Execute(() =>
339 355
             {
340
-                var models = Table.Where(predicate).ToList();
356
+                var models = GetDeleteQueryable().Where(predicate).ToList();
341 357
                 Table.RemoveRange(models);
342 358
                 Db.SaveChanges();
343 359
                 var dbos = models.Select(GetDboFromModel);

Loading…
Cancel
Save