Skip to content

Merge for 1.2.1 release #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions DDDToolkit.Querying.Tests/DDDToolkit.Querying.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Entities\SimpleEntity.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="4.19.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\DDDToolkit.Querying\DDDToolkit.Querying.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Entities\" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions DDDToolkit.Querying.Tests/Entities/SimpleEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DDDToolkit.Querying.Tests.Entities
{
public class SimpleEntity
{
public string
}
}
35 changes: 35 additions & 0 deletions DDDToolkit.Querying.Tests/Query_NestingShould.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using FluentAssertions;
using Xunit;

namespace DDDToolkit.Querying.Tests
{
public class Query_OrShould
{
private Query<string> _c = Query<string>.Is.EqualTo("c");
private Query<string> _a = Query<string>.Is.EqualTo("a");
private Query<string> _b = Query<string>.Is.EqualTo("b");
[Fact]
public void CorrectlyHandleNesting()
{
_b.And(_c.Or(_a)).EvaluateOn("b").Should().BeFalse();
}

[Fact]
public void ReturnTrueWhenRightHandSideIsTrue()
{
_b.And(_c).Or(_a).EvaluateOn("a").Should().BeTrue();
}

[Fact]
public void ReturnTrueWhenLeftHandSideIsTrue()
{
_a.And(_a).Or(_b).EvaluateOn("a").Should().BeTrue();
}

[Fact]
public void ReturnFalseWhenBothSidesAreFalse()
{
_b.And(_a).Or(_c).EvaluateOn("a").Should().BeFalse();
}
}
}
3 changes: 3 additions & 0 deletions DDDToolkit.Querying.Tests/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"methodDisplay": "method"
}
9 changes: 8 additions & 1 deletion DDDToolkit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DDDToolkit.Samples.Library.
EndProject
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}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DDDToolkit.Querying", "src\DDDToolkit.Querying\DDDToolkit.Querying.csproj", "{AAAF943F-C3DE-4BD5-BC2D-850E77A9B607}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DDDToolkit.Querying", "src\DDDToolkit.Querying\DDDToolkit.Querying.csproj", "{AAAF943F-C3DE-4BD5-BC2D-850E77A9B607}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DDDToolkit.Querying.Tests", "DDDToolkit.Querying.Tests\DDDToolkit.Querying.Tests.csproj", "{4D18A7F4-169C-4399-BFF3-F2F8AE665C55}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -97,6 +99,10 @@ Global
{AAAF943F-C3DE-4BD5-BC2D-850E77A9B607}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAAF943F-C3DE-4BD5-BC2D-850E77A9B607}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAAF943F-C3DE-4BD5-BC2D-850E77A9B607}.Release|Any CPU.Build.0 = Release|Any CPU
{4D18A7F4-169C-4399-BFF3-F2F8AE665C55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4D18A7F4-169C-4399-BFF3-F2F8AE665C55}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D18A7F4-169C-4399-BFF3-F2F8AE665C55}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D18A7F4-169C-4399-BFF3-F2F8AE665C55}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -116,6 +122,7 @@ Global
{6DD4F5BC-B48B-43A6-A048-28F8BD04D7F6} = {BDD881C3-118B-42D6-9D77-DDAB213CA4B0}
{1D41D8D2-B6A5-47DD-8E98-9512EC48B429} = {BDD881C3-118B-42D6-9D77-DDAB213CA4B0}
{AAAF943F-C3DE-4BD5-BC2D-850E77A9B607} = {EF054C6D-056E-417A-A40A-376A20DF90A6}
{4D18A7F4-169C-4399-BFF3-F2F8AE665C55} = {57967491-B4BB-44B9-B270-6533A1E388B0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E3DCC906-DD65-4478-A617-AC943C719834}
Expand Down
3 changes: 1 addition & 2 deletions src/DDDToolkit.Querying/IQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ namespace DDDToolkit.Querying
{
public interface IQuery<T>
{
IQuery<T> And(Expression<Func<T, bool>> query);
IQuery<T> And(IQuery<T> query);
IQuery<T> Or(Expression<Func<T, bool>> query);
IQuery<T> Or(IQuery<T> query);
Expression<Func<T, bool>> AsExpression();
bool EvaluateOn(T subject);
}
}
11 changes: 11 additions & 0 deletions src/DDDToolkit.Querying/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DDDToolkit.Querying
{
public static class ObjectExtensions
{
public static bool Evaluate<T>(this T obj, IQuery<T> query) => query.EvaluateOn(obj);
}
}
25 changes: 10 additions & 15 deletions src/DDDToolkit.Querying/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,35 @@ namespace DDDToolkit.Querying
{
public class Query<T> : IQuery<T>
{
private readonly Expression<Func<T, bool>> _expression = null;
private readonly Expression<Func<T, bool>> _expression;

public Query(Expression<Func<T, bool>> expression)
{
_expression = expression;
}

private Query<T> CreateNewQuery(Expression<Func<T, bool>> e2, Func<Expression, Expression, Expression> combiner)
private Query<T> CreateNewQuery(IQuery<T> query, Func<Expression, Expression, Expression> combiner)
{
var newE2 = new ParameterVisitor(e2.Parameters, _expression.Parameters)
.VisitAndConvert(e2.Body, nameof(CreateNewQuery));
var e2 = query.AsExpression();
var newE2 = new ParameterVisitor(e2.Parameters, _expression.Parameters).VisitAndConvert(e2.Body, nameof(CreateNewQuery));
var e3 = combiner(_expression.Body, newE2);

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

public IQuery<T> And(Expression<Func<T, bool>> query) => CreateNewQuery(query, Expression.AndAlso);
public IQuery<T> And(IQuery<T> query) => And(query.AsExpression());
public IQuery<T> Or(Expression<Func<T, bool>> query) => CreateNewQuery(query, Expression.OrElse);
public IQuery<T> Or(IQuery<T> query) => Or(query.AsExpression());
public IQuery<T> And(IQuery<T> query) => CreateNewQuery(query, Expression.AndAlso);
public IQuery<T> Or(IQuery<T> query) => CreateNewQuery(query, Expression.OrElse);

public Expression<Func<T, bool>> AsExpression()
=> _expression;
public Expression<Func<T, bool>> AsExpression() => _expression;

public static Query<T> Has(Expression<Func<T, bool>> query)
=> new Query<T>(query);
public bool EvaluateOn(T subject) => AsExpression().Compile()(subject);

public static Query<T> Has(IQuery<T> query)
=> new Query<T>(query.AsExpression());
public static Query<T> Has(Expression<Func<T, bool>> query) => new Query<T>(query);

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

public static QueryBuilderExpression<T, T> Is => new QueryBuilderExpression<T, T>(e => e);
public static QueryBuilderExpression<T, T> Is => Has(e => e);
}
}