Skip to content

Commit 4a83905

Browse files
authored
Merge pull request #86 from zoriya/master
Add query root rewrite support
2 parents 24dcbb7 + c15b970 commit 4a83905

15 files changed

+309
-59
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,6 @@ MigrationBackup/
363363
FodyWeavers.xsd
364364

365365
# Received verify test results
366-
*.received.*
366+
*.received.*
367+
368+
.idea

Directory.Build.props

+7-3
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@
2323
</PropertyGroup>
2424

2525
<PropertyGroup>
26-
<TargetFrameworkVersion>net6.0</TargetFrameworkVersion>
26+
<TargetFrameworkVersion>net7.0;net6.0</TargetFrameworkVersion>
2727
<MicrosoftExtensionsVersion>6.0.0</MicrosoftExtensionsVersion>
2828
<EFCoreVersion>6.0.0</EFCoreVersion>
2929
<TestEFCoreVersion>$(EFCoreVersion)</TestEFCoreVersion>
3030
</PropertyGroup>
31-
3231

33-
</Project>
32+
<PropertyGroup Condition="'$(TargetFramework)' == 'net7.0'">
33+
<EFCoreVersion>7.0.0</EFCoreVersion>
34+
<TestEFCoreVersion>$(EFCoreVersion)</TestEFCoreVersion>
35+
</PropertyGroup>
36+
37+
</Project>

samples/BasicSample/BasicSample.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net7.0</TargetFramework>
66
<Nullable>disable</Nullable>
77
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
88
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>
99
<IsPackable>false</IsPackable>
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0" />
14-
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
15-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0" />
14+
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
15+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

samples/BasicSample/Program.cs

+53-19
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
using EntityFrameworkCore.Projectables;
2-
using EntityFrameworkCore.Projectables.Extensions;
3-
using Microsoft.Data.Sqlite;
4-
using Microsoft.EntityFrameworkCore;
5-
using Microsoft.Extensions.Caching.Memory;
6-
using Microsoft.Extensions.DependencyInjection;
7-
using Microsoft.Extensions.Logging;
8-
using System;
9-
using System.Collections;
1+
using System;
102
using System.Collections.Generic;
113
using System.ComponentModel.DataAnnotations.Schema;
12-
using System.Diagnostics;
134
using System.Linq;
5+
using EntityFrameworkCore.Projectables;
6+
using Microsoft.Data.Sqlite;
7+
using Microsoft.EntityFrameworkCore;
8+
using Microsoft.Extensions.DependencyInjection;
149

1510
namespace BasicSample
1611
{
@@ -22,12 +17,13 @@ public class User
2217

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

25-
[Projectable]
26-
public string FullName
27-
=> FirstName + " " + LastName;
20+
[Projectable(UseMemberBody = nameof(_FullName))]
21+
public string FullName { get; set; }
22+
private string _FullName => FirstName + " " + LastName;
2823

29-
[Projectable]
30-
public double TotalSpent => Orders.Sum(x => x.PriceSum);
24+
[Projectable(UseMemberBody = nameof(_TotalSpent))]
25+
public double TotalSpent { get; set; }
26+
private double _TotalSpent => Orders.Sum(x => x.PriceSum);
3127

3228
[Projectable]
3329
public Order MostValuableOrder
@@ -86,7 +82,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
8682

8783
class Program
8884
{
89-
static void Main(string[] args)
85+
public static void Main(string[] args)
9086
{
9187
using var dbConnection = new SqliteConnection("Filename=:memory:");
9288
dbConnection.Open();
@@ -95,6 +91,8 @@ static void Main(string[] args)
9591
.AddDbContext<ApplicationDbContext>((provider, options) => {
9692
options
9793
.UseSqlite(dbConnection)
94+
// .LogTo(Console.WriteLine)
95+
.EnableSensitiveDataLogging()
9896
.UseProjectables();
9997
})
10098
.BuildServiceProvider();
@@ -105,9 +103,9 @@ static void Main(string[] args)
105103
var product1 = new Product { Name = "Red pen", Price = 1.5 };
106104
var product2 = new Product { Name = "Blue pen", Price = 2.1 };
107105

108-
var user = new User {
109-
FirstName = "Jon",
110-
LastName = "Doe",
106+
var user = new User {
107+
FirstName = "Jon",
108+
LastName = "Doe",
111109
Orders = new List<Order> {
112110
new Order {
113111
Items = new List<OrderItem> {
@@ -130,6 +128,42 @@ static void Main(string[] args)
130128
dbContext.SaveChanges();
131129

132130
// What did our user spent in total
131+
132+
{
133+
foreach (var u in dbContext.Users)
134+
{
135+
Console.WriteLine($"User name: {u.FullName}");
136+
}
137+
138+
foreach (var u in dbContext.Users.ToList())
139+
{
140+
Console.WriteLine($"User name: {u.FullName}");
141+
}
142+
143+
foreach (var u in dbContext.Users.OrderBy(x => x.FullName))
144+
{
145+
Console.WriteLine($"User name: {u.FullName}");
146+
}
147+
}
148+
149+
{
150+
foreach (var u in dbContext.Users.Where(x => x.TotalSpent >= 1))
151+
{
152+
Console.WriteLine($"User name: {u.FullName}");
153+
}
154+
}
155+
156+
{
157+
var result = dbContext.Users.FirstOrDefault();
158+
Console.WriteLine($"Our first user {result.FullName} has spent {result.TotalSpent}");
159+
160+
result = dbContext.Users.FirstOrDefault(x => x.TotalSpent > 1);
161+
Console.WriteLine($"Our first user {result.FullName} has spent {result.TotalSpent}");
162+
163+
var spent = dbContext.Users.Sum(x => x.TotalSpent);
164+
Console.WriteLine($"Our users combined spent: {spent}");
165+
}
166+
133167
{
134168
var query = dbContext.Users
135169
.Select(x => new {

samples/ReadmeSample/ReadmeSample.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
14-
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
15-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
14+
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
15+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

shell.nix

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{pkgs ? import <nixpkgs> {}}: let
2+
dotnet = with pkgs.dotnetCorePackages;
3+
combinePackages [
4+
sdk_7_0
5+
aspnetcore_7_0
6+
];
7+
in
8+
pkgs.mkShell {
9+
packages = [dotnet];
10+
11+
DOTNET_ROOT = "${dotnet}";
12+
}

src/EntityFrameworkCore.Projectables/EntityFrameworkCore.Projectables.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
55
<PackageReadmeFile>README.md</PackageReadmeFile>
66
</PropertyGroup>
77

src/EntityFrameworkCore.Projectables/Extensions/ExpressionExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public static Expression ExpandQuaryables(this Expression expression)
1818
/// Replaces all calls to properties and methods that are marked with the <C>Projectable</C> attribute with their respective expression tree
1919
/// </summary>
2020
public static Expression ExpandProjectables(this Expression expression)
21-
=> new ProjectableExpressionReplacer(new ProjectionExpressionResolver()).Visit(expression);
21+
=> new ProjectableExpressionReplacer(new ProjectionExpressionResolver()).Replace(expression);
2222
}
2323
}

src/EntityFrameworkCore.Projectables/Infrastructure/Internal/CustomQueryCompiler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ public TResult ExecuteAsync<TResult>(Expression query, CancellationToken cancell
3434
=> _decoratedQueryCompiler.ExecuteAsync<TResult>(Expand(query), cancellationToken);
3535

3636
Expression Expand(Expression expression)
37-
=> _projectableExpressionReplacer.Visit(expression);
37+
=> _projectableExpressionReplacer.Replace(expression);
3838
}
3939
}

0 commit comments

Comments
 (0)