Skip to content

Commit b939bef

Browse files
committed
Renaming a few things
1 parent f4c2e0c commit b939bef

File tree

1 file changed

+35
-50
lines changed

1 file changed

+35
-50
lines changed

src/DatatablesParser/DatatablesParser.cs

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ namespace DataTablesParser
1010
{
1111
public class Parser<T> where T : class
1212
{
13-
private IQueryable<T> _queriable;
13+
private IQueryable<T> _queryable;
1414
private readonly Dictionary<string,string> _config;
1515
private readonly Type _type;
16-
private IDictionary<int, PropertyMapping> _propertyMap ;
16+
private IDictionary<int, PropertyMap> _propertyMap ;
1717

1818
//Global configs
1919
private int _take;
@@ -35,9 +35,9 @@ public class Parser<T> where T : class
3535
typeof(Nullable<DateTime>)
3636
};
3737

38-
public Parser(IEnumerable<KeyValuePair<string, StringValues>> configParams, IQueryable<T> queriable)
38+
public Parser(IEnumerable<KeyValuePair<string, StringValues>> configParams, IQueryable<T> queryable)
3939
{
40-
_queriable = queriable;
40+
_queryable = queryable;
4141
_config = configParams.ToDictionary(k => k.Key,v=> v.Value.First().Trim());
4242
_type = typeof(T);
4343

@@ -55,7 +55,7 @@ where Regex.IsMatch(param.Key,Constants.COLUMN_PROPERTY_PATTERN)
5555
select new
5656
{
5757
index = int.Parse(index),
58-
map = new PropertyMapping
58+
map = new PropertyMap
5959
{
6060
Property = prop,
6161
Searchable = searchable,
@@ -82,15 +82,15 @@ where Regex.IsMatch(param.Key,Constants.COLUMN_PROPERTY_PATTERN)
8282
_sortDisabled = _config.ContainsKey(Constants.ORDERING_ENABLED) && _config[Constants.ORDERING_ENABLED] == "false";
8383
}
8484

85-
public FormatedList<T> Parse()
85+
public Results<T> Parse()
8686
{
87-
var list = new FormatedList<T>();
87+
var list = new Results<T>();
8888

89-
// parse the echo property (must be returned as int to prevent XSS-attack)
89+
// parse the echo property
9090
list.draw = int.Parse(_config[Constants.DRAW]);
9191

9292
// count the record BEFORE filtering
93-
list.recordsTotal = _queriable.Count();
93+
list.recordsTotal = _queryable.Count();
9494

9595
//sort results if sorting isn't disabled or skip needs to be called
9696
if(!_sortDisabled || _skip > 0)
@@ -104,26 +104,26 @@ public FormatedList<T> Parse()
104104
//Use query expression to return filtered paged list
105105
//This is a best effort to avoid client evaluation whenever possible
106106
//No good api to determine support for .ToString() on a type
107-
if(_queriable.Provider is System.Linq.EnumerableQuery && hasFilterText)
107+
if(_queryable.Provider is System.Linq.EnumerableQuery && hasFilterText)
108108
{
109-
resultQuery = _queriable.Where(EnumerablFilter)
109+
resultQuery = _queryable.Where(EnumerablFilter)
110110
.Skip(_skip)
111111
.Take(_take);
112112

113-
list.recordsFiltered = _queriable.Count(EnumerablFilter);
113+
list.recordsFiltered = _queryable.Count(EnumerablFilter);
114114
}
115115
else if(hasFilterText)
116116
{
117117
var entityFilter = GenerateEntityFilter();
118-
resultQuery = _queriable.Where(entityFilter)
118+
resultQuery = _queryable.Where(entityFilter)
119119
.Skip(_skip)
120120
.Take(_take);
121121

122-
list.recordsFiltered = _queriable.Count(entityFilter);
122+
list.recordsFiltered = _queryable.Count(entityFilter);
123123
}
124124
else
125125
{
126-
resultQuery = _queriable
126+
resultQuery = _queryable
127127
.Skip(_skip)
128128
.Take(_take);
129129

@@ -132,18 +132,15 @@ public FormatedList<T> Parse()
132132
}
133133

134134

135-
list.data = resultQuery
136-
.ToList();
137-
138-
list.SetQuery(resultQuery.ToString());
135+
list.data = resultQuery.ToList();
139136

140137
return list;
141138
}
142139

143140
private void ApplySort()
144141
{
145142
var sorted = false;
146-
var paramExpr = Expression.Parameter(typeof(T), "val");
143+
var paramExpr = Expression.Parameter(_type, "val");
147144

148145
// Enumerate the keys sort keys in the order we received them
149146
foreach (var param in _config.Where(k => Regex.IsMatch(k.Key, Constants.ORDER_PATTERN)))
@@ -167,7 +164,7 @@ private void ApplySort()
167164
var sortProperty = _propertyMap[sortcolumn].Property;
168165
var expression1 = Expression.Property(paramExpr, sortProperty);
169166
var propType = sortProperty.PropertyType;
170-
var delegateType = Expression.GetFuncType(typeof(T), propType);
167+
var delegateType = Expression.GetFuncType(_type, propType);
171168
var propertyExpr = Expression.Lambda(delegateType, expression1, paramExpr);
172169

173170
// apply the sort (default is ascending if not specified)
@@ -181,13 +178,13 @@ private void ApplySort()
181178
methodName = sorted ? "ThenByDescending" : "OrderByDescending";
182179
}
183180

184-
_queriable = typeof(Queryable).GetMethods().Single(
181+
_queryable = typeof(Queryable).GetMethods().Single(
185182
method => method.Name == methodName
186183
&& method.IsGenericMethodDefinition
187184
&& method.GetGenericArguments().Length == 2
188185
&& method.GetParameters().Length == 2)
189-
.MakeGenericMethod(typeof(T), propType)
190-
.Invoke(null, new object[] { _queriable, propertyExpr }) as IOrderedQueryable<T>;
186+
.MakeGenericMethod(_type, propType)
187+
.Invoke(null, new object[] { _queryable, propertyExpr }) as IOrderedQueryable<T>;
191188

192189
sorted = true;
193190
}
@@ -198,16 +195,16 @@ private void ApplySort()
198195
{
199196
var firstProp = Expression.Property(paramExpr, _propertyMap.First().Value.Property);
200197
var propType = _propertyMap.First().Value.Property.PropertyType;
201-
var delegateType = Expression.GetFuncType(typeof(T), propType);
198+
var delegateType = Expression.GetFuncType(_type, propType);
202199
var propertyExpr = Expression.Lambda(delegateType, firstProp, paramExpr);
203200

204-
_queriable = typeof(Queryable).GetMethods().Single(
201+
_queryable = typeof(Queryable).GetMethods().Single(
205202
method => method.Name == "OrderBy"
206203
&& method.IsGenericMethodDefinition
207204
&& method.GetGenericArguments().Length == 2
208205
&& method.GetParameters().Length == 2)
209-
.MakeGenericMethod(typeof(T), propType)
210-
.Invoke(null, new object[] { _queriable, propertyExpr }) as IOrderedQueryable<T>;
206+
.MakeGenericMethod(_type, propType)
207+
.Invoke(null, new object[] { _queryable, propertyExpr }) as IOrderedQueryable<T>;
211208

212209
}
213210

@@ -232,7 +229,7 @@ private bool EnumerablFilter(T item)
232229
}
233230

234231
/// <summary>
235-
/// Expression for an all column search, which will filter the result based on this criterion
232+
/// Generate a lamda expression based on a search filter for all mapped columns
236233
/// </summary>
237234
private Expression<Func<T, bool>> GenerateEntityFilter()
238235
{
@@ -241,23 +238,23 @@ private Expression<Func<T, bool>> GenerateEntityFilter()
241238

242239
// invariant expressions
243240
var searchExpression = Expression.Constant(search.ToLower());
244-
var paramExpression = Expression.Parameter(typeof(T), "val");
241+
var paramExpression = Expression.Parameter(_type, "val");
245242
List<MethodCallExpression> searchProps = new List<MethodCallExpression>();
246243

247244
foreach (var propMap in _propertyMap)
248245
{
249-
var property = propMap.Value.Property;
250-
var isString = property.PropertyType == typeof(string);
251-
if (!property.CanWrite || !propMap.Value.Searchable || (!_convertable.Any(t => t == property.PropertyType) && !isString ) )
246+
var prop = propMap.Value.Property;
247+
var isString = prop.PropertyType == typeof(string);
248+
if (!prop.CanWrite || !propMap.Value.Searchable || (!_convertable.Any(t => t == prop.PropertyType) && !isString ) )
252249
{
253250
continue;
254251
}
255252

256-
Expression propExp = Expression.Property(paramExpression, property);
253+
Expression propExp = Expression.Property(paramExpression, prop);
257254

258255
if (!isString)
259256
{
260-
var toString = property.PropertyType.GetMethod("ToString", Type.EmptyTypes);
257+
var toString = prop.PropertyType.GetMethod("ToString", Type.EmptyTypes);
261258

262259
propExp = Expression.Call(propExp, toString);
263260

@@ -280,14 +277,14 @@ private Expression<Func<T, bool>> GenerateEntityFilter()
280277
compoundExpression = Expression.Or(compoundExpression, propertyQuery[i]);
281278
}
282279

283-
// compile the expression into a lambda
280+
// return the expression into a lambda
284281
return Expression.Lambda<Func<T, bool>>(compoundExpression, paramExpression);
285282

286283
}
287284

288285

289286

290-
private class PropertyMapping
287+
private class PropertyMap
291288
{
292289
public PropertyInfo Property { get; set; }
293290
public bool Orderable { get; set; }
@@ -299,20 +296,8 @@ private class PropertyMapping
299296

300297

301298
}
302-
public class FormatedList<T>
299+
public class Results<T>
303300
{
304-
private string _query;
305-
306-
internal void SetQuery(string query)
307-
{
308-
_query = query;
309-
}
310-
311-
public string GetQuery()
312-
{
313-
return _query;
314-
}
315-
316301
public int draw { get; set; }
317302
public int recordsTotal { get; set; }
318303
public int recordsFiltered { get; set; }

0 commit comments

Comments
 (0)