cleaner like this. everyone respects the builder concept with a build method.

This commit is contained in:
David Lebée 2018-03-14 19:34:41 -05:00
parent 18a3756f51
commit 9ab186b811
3 changed files with 41 additions and 13 deletions

View File

@ -83,28 +83,23 @@ namespace PoweredSoft.DynamicLinq
public static IQueryable GroupBy(this IQueryable query, Type type, Action<GroupBuilder> callback)
{
var groupBuilder = new GroupBuilder();
var groupBuilder = new GroupBuilder(query);
callback(groupBuilder);
if (groupBuilder.Empty)
throw new Exception("No group specified, please specify at least one group");
return QueryableHelpers.GroupBy(query, type, groupBuilder.Parts, groupBuilder.Type, groupBuilder.EqualityComparerType);
var ret = groupBuilder.Build();
return ret;
}
public static IQueryable Select(this IQueryable query, Action<SelectBuilder> callback)
{
var sb = new SelectBuilder();
var sb = new SelectBuilder(query);
callback(sb);
if (sb.Empty)
throw new Exception("No select specified, please specify at least one select path");
return QueryableHelpers.Select(query,
sb.Parts.Select(t => (selectType: t.SelectType, propertyName: t.PropertyName, path: t.Path)).ToList(),
sb.DestinationType);
var ret = sb.Build();
return ret;
}
public static List<object> ToObjectList(this IQueryable query)
{
// Expression call tolist?
var ret = new List<object>();
foreach (var o in query)
ret.Add(o);

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Text;
using System.Linq;
using PoweredSoft.DynamicLinq.Helpers;
namespace PoweredSoft.DynamicLinq.Fluent
{
@ -12,6 +13,13 @@ namespace PoweredSoft.DynamicLinq.Fluent
public bool Empty => !Parts.Any();
public Type EqualityComparerType { get; set; }
public IQueryable Query { get; protected set; }
public GroupBuilder(IQueryable query)
{
Query = query;
}
public GroupBuilder Path(string path, string propertyName = null)
{
if (propertyName == null)
@ -44,5 +52,14 @@ namespace PoweredSoft.DynamicLinq.Fluent
EqualityComparerType = type;
return this;
}
public virtual IQueryable Build()
{
if (Empty)
throw new Exception("No group specified, please specify at least one group");
var ret = QueryableHelpers.GroupBy(Query, Query.ElementType, Parts, Type, EqualityComparerType);
return ret;
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using PoweredSoft.DynamicLinq.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -17,6 +18,12 @@ namespace PoweredSoft.DynamicLinq.Fluent
public List<SelectPart> Parts = new List<SelectPart>();
public Type DestinationType { get; set; }
public bool Empty => Parts?.Count == 0;
public IQueryable Query { get; protected set; }
public SelectBuilder(IQueryable query)
{
Query = query;
}
protected void throwIfUsedOrEmpty(string propertyName)
{
@ -108,5 +115,14 @@ namespace PoweredSoft.DynamicLinq.Fluent
});
return this;
}
public virtual IQueryable Build()
{
if (Empty)
throw new Exception("No select specified, please specify at least one select path");
var partsTuple = Parts.Select(t => (selectType: t.SelectType, propertyName: t.PropertyName, path: t.Path)).ToList();
return QueryableHelpers.Select(Query, partsTuple, DestinationType);
}
}
}