Skip to content

Commit ca3749a

Browse files
authored
Merge pull request #29 from codepb/develop
Merge for 1.2.1 release
2 parents 08cb0ca + bc061aa commit ca3749a

File tree

8 files changed

+111
-18
lines changed

8 files changed

+111
-18
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Remove="Entities\SimpleEntity.cs" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="FluentAssertions" Version="4.19.4" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
14+
<PackageReference Include="xunit" Version="2.3.1" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\src\DDDToolkit.Querying\DDDToolkit.Querying.csproj" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<Folder Include="Entities\" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<None Update="xunit.runner.json">
28+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
29+
</None>
30+
</ItemGroup>
31+
32+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace DDDToolkit.Querying.Tests.Entities
6+
{
7+
public class SimpleEntity
8+
{
9+
public string
10+
}
11+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using FluentAssertions;
2+
using Xunit;
3+
4+
namespace DDDToolkit.Querying.Tests
5+
{
6+
public class Query_OrShould
7+
{
8+
private Query<string> _c = Query<string>.Is.EqualTo("c");
9+
private Query<string> _a = Query<string>.Is.EqualTo("a");
10+
private Query<string> _b = Query<string>.Is.EqualTo("b");
11+
[Fact]
12+
public void CorrectlyHandleNesting()
13+
{
14+
_b.And(_c.Or(_a)).EvaluateOn("b").Should().BeFalse();
15+
}
16+
17+
[Fact]
18+
public void ReturnTrueWhenRightHandSideIsTrue()
19+
{
20+
_b.And(_c).Or(_a).EvaluateOn("a").Should().BeTrue();
21+
}
22+
23+
[Fact]
24+
public void ReturnTrueWhenLeftHandSideIsTrue()
25+
{
26+
_a.And(_a).Or(_b).EvaluateOn("a").Should().BeTrue();
27+
}
28+
29+
[Fact]
30+
public void ReturnFalseWhenBothSidesAreFalse()
31+
{
32+
_b.And(_a).Or(_c).EvaluateOn("a").Should().BeFalse();
33+
}
34+
}
35+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"methodDisplay": "method"
3+
}

DDDToolkit.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DDDToolkit.Samples.Library.
3535
EndProject
3636
Project("{00D1A9C2-B5F0-4AF3-8072-F6C62B433612}") = "DDDToolkit.Samples.Library.Repository.Database", "samples\library\DDDToolkit.Samples.Library.Repository.Database\DDDToolkit.Samples.Library.Repository.Database.sqlproj", "{1D41D8D2-B6A5-47DD-8E98-9512EC48B429}"
3737
EndProject
38-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DDDToolkit.Querying", "src\DDDToolkit.Querying\DDDToolkit.Querying.csproj", "{AAAF943F-C3DE-4BD5-BC2D-850E77A9B607}"
38+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DDDToolkit.Querying", "src\DDDToolkit.Querying\DDDToolkit.Querying.csproj", "{AAAF943F-C3DE-4BD5-BC2D-850E77A9B607}"
39+
EndProject
40+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DDDToolkit.Querying.Tests", "DDDToolkit.Querying.Tests\DDDToolkit.Querying.Tests.csproj", "{4D18A7F4-169C-4399-BFF3-F2F8AE665C55}"
3941
EndProject
4042
Global
4143
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -97,6 +99,10 @@ Global
9799
{AAAF943F-C3DE-4BD5-BC2D-850E77A9B607}.Debug|Any CPU.Build.0 = Debug|Any CPU
98100
{AAAF943F-C3DE-4BD5-BC2D-850E77A9B607}.Release|Any CPU.ActiveCfg = Release|Any CPU
99101
{AAAF943F-C3DE-4BD5-BC2D-850E77A9B607}.Release|Any CPU.Build.0 = Release|Any CPU
102+
{4D18A7F4-169C-4399-BFF3-F2F8AE665C55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
103+
{4D18A7F4-169C-4399-BFF3-F2F8AE665C55}.Debug|Any CPU.Build.0 = Debug|Any CPU
104+
{4D18A7F4-169C-4399-BFF3-F2F8AE665C55}.Release|Any CPU.ActiveCfg = Release|Any CPU
105+
{4D18A7F4-169C-4399-BFF3-F2F8AE665C55}.Release|Any CPU.Build.0 = Release|Any CPU
100106
EndGlobalSection
101107
GlobalSection(SolutionProperties) = preSolution
102108
HideSolutionNode = FALSE
@@ -116,6 +122,7 @@ Global
116122
{6DD4F5BC-B48B-43A6-A048-28F8BD04D7F6} = {BDD881C3-118B-42D6-9D77-DDAB213CA4B0}
117123
{1D41D8D2-B6A5-47DD-8E98-9512EC48B429} = {BDD881C3-118B-42D6-9D77-DDAB213CA4B0}
118124
{AAAF943F-C3DE-4BD5-BC2D-850E77A9B607} = {EF054C6D-056E-417A-A40A-376A20DF90A6}
125+
{4D18A7F4-169C-4399-BFF3-F2F8AE665C55} = {57967491-B4BB-44B9-B270-6533A1E388B0}
119126
EndGlobalSection
120127
GlobalSection(ExtensibilityGlobals) = postSolution
121128
SolutionGuid = {E3DCC906-DD65-4478-A617-AC943C719834}

src/DDDToolkit.Querying/IQuery.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ namespace DDDToolkit.Querying
55
{
66
public interface IQuery<T>
77
{
8-
IQuery<T> And(Expression<Func<T, bool>> query);
98
IQuery<T> And(IQuery<T> query);
10-
IQuery<T> Or(Expression<Func<T, bool>> query);
119
IQuery<T> Or(IQuery<T> query);
1210
Expression<Func<T, bool>> AsExpression();
11+
bool EvaluateOn(T subject);
1312
}
1413
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace DDDToolkit.Querying
6+
{
7+
public static class ObjectExtensions
8+
{
9+
public static bool Evaluate<T>(this T obj, IQuery<T> query) => query.EvaluateOn(obj);
10+
}
11+
}

src/DDDToolkit.Querying/Query.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,35 @@ namespace DDDToolkit.Querying
55
{
66
public class Query<T> : IQuery<T>
77
{
8-
private readonly Expression<Func<T, bool>> _expression = null;
8+
private readonly Expression<Func<T, bool>> _expression;
99

1010
public Query(Expression<Func<T, bool>> expression)
1111
{
1212
_expression = expression;
1313
}
1414

15-
private Query<T> CreateNewQuery(Expression<Func<T, bool>> e2, Func<Expression, Expression, Expression> combiner)
15+
private Query<T> CreateNewQuery(IQuery<T> query, Func<Expression, Expression, Expression> combiner)
1616
{
17-
var newE2 = new ParameterVisitor(e2.Parameters, _expression.Parameters)
18-
.VisitAndConvert(e2.Body, nameof(CreateNewQuery));
17+
var e2 = query.AsExpression();
18+
var newE2 = new ParameterVisitor(e2.Parameters, _expression.Parameters).VisitAndConvert(e2.Body, nameof(CreateNewQuery));
1919
var e3 = combiner(_expression.Body, newE2);
2020

2121
var lambda = Expression.Lambda<Func<T, bool>>(e3, _expression.Parameters);
2222
return new Query<T>(lambda);
2323
}
2424

25-
public IQuery<T> And(Expression<Func<T, bool>> query) => CreateNewQuery(query, Expression.AndAlso);
26-
public IQuery<T> And(IQuery<T> query) => And(query.AsExpression());
27-
public IQuery<T> Or(Expression<Func<T, bool>> query) => CreateNewQuery(query, Expression.OrElse);
28-
public IQuery<T> Or(IQuery<T> query) => Or(query.AsExpression());
25+
public IQuery<T> And(IQuery<T> query) => CreateNewQuery(query, Expression.AndAlso);
26+
public IQuery<T> Or(IQuery<T> query) => CreateNewQuery(query, Expression.OrElse);
2927

30-
public Expression<Func<T, bool>> AsExpression()
31-
=> _expression;
28+
public Expression<Func<T, bool>> AsExpression() => _expression;
3229

33-
public static Query<T> Has(Expression<Func<T, bool>> query)
34-
=> new Query<T>(query);
30+
public bool EvaluateOn(T subject) => AsExpression().Compile()(subject);
3531

36-
public static Query<T> Has(IQuery<T> query)
37-
=> new Query<T>(query.AsExpression());
32+
public static Query<T> Has(Expression<Func<T, bool>> query) => new Query<T>(query);
3833

3934
public static QueryBuilderExpression<T, TProp> Has<TProp>(Expression<Func<T, TProp>> expression)
4035
=> new QueryBuilderExpression<T, TProp>(expression);
4136

42-
public static QueryBuilderExpression<T, T> Is => new QueryBuilderExpression<T, T>(e => e);
37+
public static QueryBuilderExpression<T, T> Is => Has(e => e);
4338
}
4439
}

0 commit comments

Comments
 (0)