Skip to content

Add query root rewrite support #86

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 7 commits into from
Oct 24, 2023
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,6 @@ MigrationBackup/
FodyWeavers.xsd

# Received verify test results
*.received.*
*.received.*

.idea
10 changes: 7 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@
</PropertyGroup>

<PropertyGroup>
<TargetFrameworkVersion>net6.0</TargetFrameworkVersion>
<TargetFrameworkVersion>net7.0;net6.0</TargetFrameworkVersion>
<MicrosoftExtensionsVersion>6.0.0</MicrosoftExtensionsVersion>
<EFCoreVersion>6.0.0</EFCoreVersion>
<TestEFCoreVersion>$(EFCoreVersion)</TestEFCoreVersion>
</PropertyGroup>


</Project>
<PropertyGroup Condition="'$(TargetFramework)' == 'net7.0'">
<EFCoreVersion>7.0.0</EFCoreVersion>
<TestEFCoreVersion>$(EFCoreVersion)</TestEFCoreVersion>
</PropertyGroup>

</Project>
8 changes: 4 additions & 4 deletions samples/BasicSample/BasicSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Nullable>disable</Nullable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
72 changes: 53 additions & 19 deletions samples/BasicSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
using EntityFrameworkCore.Projectables;
using EntityFrameworkCore.Projectables.Extensions;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using EntityFrameworkCore.Projectables;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace BasicSample
{
Expand All @@ -22,12 +17,13 @@ public class User

public ICollection<Order> Orders { get; set; }

[Projectable]
public string FullName
=> FirstName + " " + LastName;
[Projectable(UseMemberBody = nameof(_FullName))]
public string FullName { get; set; }
private string _FullName => FirstName + " " + LastName;

[Projectable]
public double TotalSpent => Orders.Sum(x => x.PriceSum);
[Projectable(UseMemberBody = nameof(_TotalSpent))]
public double TotalSpent { get; set; }
private double _TotalSpent => Orders.Sum(x => x.PriceSum);

[Projectable]
public Order MostValuableOrder
Expand Down Expand Up @@ -86,7 +82,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

class Program
{
static void Main(string[] args)
public static void Main(string[] args)
{
using var dbConnection = new SqliteConnection("Filename=:memory:");
dbConnection.Open();
Expand All @@ -95,6 +91,8 @@ static void Main(string[] args)
.AddDbContext<ApplicationDbContext>((provider, options) => {
options
.UseSqlite(dbConnection)
// .LogTo(Console.WriteLine)
.EnableSensitiveDataLogging()
.UseProjectables();
})
.BuildServiceProvider();
Expand All @@ -105,9 +103,9 @@ static void Main(string[] args)
var product1 = new Product { Name = "Red pen", Price = 1.5 };
var product2 = new Product { Name = "Blue pen", Price = 2.1 };

var user = new User {
FirstName = "Jon",
LastName = "Doe",
var user = new User {
FirstName = "Jon",
LastName = "Doe",
Orders = new List<Order> {
new Order {
Items = new List<OrderItem> {
Expand All @@ -130,6 +128,42 @@ static void Main(string[] args)
dbContext.SaveChanges();

// What did our user spent in total

{
foreach (var u in dbContext.Users)
{
Console.WriteLine($"User name: {u.FullName}");
}

foreach (var u in dbContext.Users.ToList())
{
Console.WriteLine($"User name: {u.FullName}");
}

foreach (var u in dbContext.Users.OrderBy(x => x.FullName))
{
Console.WriteLine($"User name: {u.FullName}");
}
}

{
foreach (var u in dbContext.Users.Where(x => x.TotalSpent >= 1))
{
Console.WriteLine($"User name: {u.FullName}");
}
}

{
var result = dbContext.Users.FirstOrDefault();
Console.WriteLine($"Our first user {result.FullName} has spent {result.TotalSpent}");

result = dbContext.Users.FirstOrDefault(x => x.TotalSpent > 1);
Console.WriteLine($"Our first user {result.FullName} has spent {result.TotalSpent}");

var spent = dbContext.Users.Sum(x => x.TotalSpent);
Console.WriteLine($"Our users combined spent: {spent}");
}

{
var query = dbContext.Users
.Select(x => new {
Expand Down
6 changes: 3 additions & 3 deletions samples/ReadmeSample/ReadmeSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{pkgs ? import <nixpkgs> {}}: let
dotnet = with pkgs.dotnetCorePackages;
combinePackages [
sdk_7_0
aspnetcore_7_0
];
in
pkgs.mkShell {
packages = [dotnet];

DOTNET_ROOT = "${dotnet}";
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public static Expression ExpandQuaryables(this Expression expression)
/// Replaces all calls to properties and methods that are marked with the <C>Projectable</C> attribute with their respective expression tree
/// </summary>
public static Expression ExpandProjectables(this Expression expression)
=> new ProjectableExpressionReplacer(new ProjectionExpressionResolver()).Visit(expression);
=> new ProjectableExpressionReplacer(new ProjectionExpressionResolver()).Replace(expression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public TResult ExecuteAsync<TResult>(Expression query, CancellationToken cancell
=> _decoratedQueryCompiler.ExecuteAsync<TResult>(Expand(query), cancellationToken);

Expression Expand(Expression expression)
=> _projectableExpressionReplacer.Visit(expression);
=> _projectableExpressionReplacer.Replace(expression);
}
}
Loading