Added compatiblity mode

This commit is contained in:
Koen Bekkenutte
2021-06-03 04:46:25 +08:00
parent 898909660c
commit 11582d467c
9 changed files with 249 additions and 75 deletions
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using EntityFrameworkCore.Projectables.Extensions;
using EntityFrameworkCore.Projectables.Infrastructure;
namespace EntityFrameworkCore.Projectables.Benchmarks.Helpers
{
class TestDbContext : DbContext
{
readonly bool _useProjectables;
readonly bool _useFullCompatibiltyMode;
public TestDbContext(bool useProjectables, bool useFullCompatibiltyMode = true)
{
_useProjectables = useProjectables;
_useFullCompatibiltyMode = useFullCompatibiltyMode;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=ReadmeSample;Trusted_Connection=True");
if (_useProjectables)
{
optionsBuilder.UseProjectables(projectableOptions => {
projectableOptions.CompatibilityMode(_useFullCompatibiltyMode ? CompatibilityMode.Full : CompatibilityMode.Limited);
});
}
}
public DbSet<TestEntity> Entities => Set<TestEntity>();
}
}
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityFrameworkCore.Projectables.Benchmarks.Helpers
{
public class TestEntity
{
public int Id { get; set; }
[Projectable]
public int IdPlus1 => Id + 1;
}
}
@@ -1,40 +1,16 @@
using System;
using System.IO.Compression;
using System.Linq;
using BenchmarkDotNet.Attributes;
using EntityFrameworkCore.Projectables.Benchmarks.Helpers;
using EntityFrameworkCore.Projectables.Extensions;
using EntityFrameworkCore.Projectables.Infrastructure;
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkCore.Projectables.Benchmarks
{
public class PlainOverhead
{
class TestEntity
{
public int Id { get; set; }
}
class TestDbContext : DbContext
{
readonly bool _useProjectables;
public TestDbContext(bool useProjectables)
{
_useProjectables = useProjectables;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=ReadmeSample;Trusted_Connection=True");
if (_useProjectables)
{
optionsBuilder.UseProjectables();
}
}
public DbSet<TestEntity> Entities => Set<TestEntity>();
}
[Benchmark(Baseline = true)]
public void WithoutProjectables()
{
@@ -47,7 +23,7 @@ namespace EntityFrameworkCore.Projectables.Benchmarks
}
[Benchmark]
public void WithProjectables()
public void WithProjectablesWithFullCompatibility()
{
using var dbContext = new TestDbContext(true);
@@ -57,5 +33,17 @@ namespace EntityFrameworkCore.Projectables.Benchmarks
}
}
[Benchmark]
public void WithProjectablesWithLimitedCompatibility()
{
using var dbContext = new TestDbContext(true, false);
for (int i = 0; i < 10000; i++)
{
dbContext.Entities.Select(x => x.Id).ToQueryString();
}
}
}
}
@@ -1,43 +1,15 @@
using System;
using System.Linq;
using BenchmarkDotNet.Attributes;
using EntityFrameworkCore.Projectables.Benchmarks.Helpers;
using EntityFrameworkCore.Projectables.Extensions;
using EntityFrameworkCore.Projectables.Infrastructure;
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkCore.Projectables.Benchmarks
{
public class ProjectableProperties
{
public class TestEntity
{
public int Id { get; set; }
[Projectable]
public int IdPlus1 => Id + 1;
}
class TestDbContext : DbContext
{
readonly bool _useProjectables;
public TestDbContext(bool useProjectables)
{
_useProjectables = useProjectables;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=ReadmeSample;Trusted_Connection=True");
if (_useProjectables)
{
optionsBuilder.UseProjectables();
}
}
public DbSet<TestEntity> Entities => Set<TestEntity>();
}
[Benchmark(Baseline = true)]
public void WithoutProjectables()
{
@@ -50,7 +22,7 @@ namespace EntityFrameworkCore.Projectables.Benchmarks
}
[Benchmark]
public void WithProjectables()
public void WithProjectablesWithFullCompatibility()
{
using var dbContext = new TestDbContext(true);
@@ -60,5 +32,15 @@ namespace EntityFrameworkCore.Projectables.Benchmarks
}
}
[Benchmark]
public void WithProjectablesWithLimitedCompatibility()
{
using var dbContext = new TestDbContext(true, false);
for (int i = 0; i < 10000; i++)
{
dbContext.Entities.Select(x => x.IdPlus1).ToQueryString();
}
}
}
}
@@ -1,4 +1,5 @@
using EntityFrameworkCore.Projectables.Infrastructure.Internal;
using EntityFrameworkCore.Projectables.Infrastructure;
using EntityFrameworkCore.Projectables.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
@@ -12,15 +13,17 @@ namespace EntityFrameworkCore.Projectables.Extensions
{
public static class DbContextOptionsExtensions
{
public static DbContextOptionsBuilder<TContext> UseProjectables<TContext>(this DbContextOptionsBuilder<TContext> optionsBuilder)
public static DbContextOptionsBuilder<TContext> UseProjectables<TContext>(this DbContextOptionsBuilder<TContext> optionsBuilder, Action<ProjectableOptionsBuilder>? configure = null)
where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>)UseProjectables((DbContextOptionsBuilder)optionsBuilder);
=> (DbContextOptionsBuilder<TContext>)UseProjectables((DbContextOptionsBuilder)optionsBuilder, configure);
public static DbContextOptionsBuilder UseProjectables(this DbContextOptionsBuilder optionsBuilder)
public static DbContextOptionsBuilder UseProjectables(this DbContextOptionsBuilder optionsBuilder, Action<ProjectableOptionsBuilder>? configure = null)
{
var extension = optionsBuilder.Options.FindExtension<ProjectionOptionsExtension>() ?? new ProjectionOptionsExtension();
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
configure?.Invoke(new ProjectableOptionsBuilder(optionsBuilder));
return optionsBuilder;
}
}
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityFrameworkCore.Projectables.Infrastructure
{
public enum CompatibilityMode
{
Full,
Limited
}
}
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.Services;
using Microsoft.EntityFrameworkCore.Query;
namespace EntityFrameworkCore.Projectables.Infrastructure.Internal
{
public class CustomQueryTranslationPreprocessorFactory : IQueryTranslationPreprocessorFactory
{
readonly IQueryTranslationPreprocessorFactory _decoratedFactory;
readonly QueryTranslationPreprocessorDependencies _queryTranslationPreprocessorDependencies;
readonly ProjectableExpressionReplacer _projectableExpressionReplacer = new(new ProjectionExpressionResolver());
public CustomQueryTranslationPreprocessorFactory(IQueryTranslationPreprocessorFactory decoratedFactory, QueryTranslationPreprocessorDependencies queryTranslationPreprocessorDependencies)
{
_decoratedFactory = decoratedFactory;
_queryTranslationPreprocessorDependencies = queryTranslationPreprocessorDependencies;
}
public QueryTranslationPreprocessor Create(QueryCompilationContext queryCompilationContext)
=> new CustomQueryTranslationPreprocessor(_queryTranslationPreprocessorDependencies, queryCompilationContext, _projectableExpressionReplacer);
}
public class CustomQueryTranslationPreprocessor : QueryTranslationPreprocessor
{
readonly ProjectableExpressionReplacer _projectableExpressionReplacer;
public CustomQueryTranslationPreprocessor(QueryTranslationPreprocessorDependencies dependencies, QueryCompilationContext queryCompilationContext, ProjectableExpressionReplacer projectableExpressionReplacer) : base(dependencies, queryCompilationContext)
{
_projectableExpressionReplacer = projectableExpressionReplacer;
}
public override Expression Process(Expression query)
=> base.Process(_projectableExpressionReplacer.Visit(query));
}
}
@@ -14,11 +14,21 @@ namespace EntityFrameworkCore.Projectables.Infrastructure.Internal
{
public class ProjectionOptionsExtension : IDbContextOptionsExtension
{
CompatibilityMode _compatibilityMode = CompatibilityMode.Full;
public ProjectionOptionsExtension()
{
Info = new ExtensionInfo(this);
}
public ProjectionOptionsExtension(ProjectionOptionsExtension copyFrom)
: this()
{
_compatibilityMode = copyFrom._compatibilityMode;
}
protected ProjectionOptionsExtension Clone() => new(this);
public DbContextOptionsExtensionInfo Info { get; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "EF1001:Internal EF Core API usage.", Justification = "Needed")]
@@ -37,19 +47,47 @@ namespace EntityFrameworkCore.Projectables.Infrastructure.Internal
return ActivatorUtilities.GetServiceOrCreateInstance(services, descriptor.ImplementationType!);
}
var targetDescriptor = services.FirstOrDefault(x => x.ServiceType == typeof(IQueryCompiler));
if (targetDescriptor is null)
if (_compatibilityMode is CompatibilityMode.Full)
{
throw new InvalidOperationException("No QueryProvider is configured yet. Please make sure to configure a database provider first"); ;
}
var decoratorObjectFactory = ActivatorUtilities.CreateFactory(typeof(CustomQueryProvider), new [] { targetDescriptor.ServiceType });
var targetDescriptor = services.FirstOrDefault(x => x.ServiceType == typeof(IQueryCompiler));
if (targetDescriptor is null)
{
throw new InvalidOperationException("No QueryProvider is configured yet. Please make sure to configure a database provider first"); ;
}
services.Replace(ServiceDescriptor.Describe(
targetDescriptor.ServiceType,
serviceProvider => decoratorObjectFactory(serviceProvider, new[] { CreateTargetInstance(serviceProvider, targetDescriptor) }),
targetDescriptor.Lifetime
));
var decoratorObjectFactory = ActivatorUtilities.CreateFactory(typeof(CustomQueryProvider), new[] { targetDescriptor.ServiceType });
services.Replace(ServiceDescriptor.Describe(
targetDescriptor.ServiceType,
serviceProvider => decoratorObjectFactory(serviceProvider, new[] { CreateTargetInstance(serviceProvider, targetDescriptor) }),
targetDescriptor.Lifetime
));
}
else
{
var targetDescriptor = services.FirstOrDefault(x => x.ServiceType == typeof(IQueryTranslationPreprocessorFactory));
if (targetDescriptor is null)
{
throw new InvalidOperationException("No QueryTranslationPreprocessorFactory is configured yet. Please make sure to configure a database provider first"); ;
}
var decoratorObjectFactory = ActivatorUtilities.CreateFactory(typeof(CustomQueryTranslationPreprocessorFactory), new[] { targetDescriptor.ServiceType });
services.Replace(ServiceDescriptor.Describe(
targetDescriptor.ServiceType,
serviceProvider => decoratorObjectFactory(serviceProvider, new[] { CreateTargetInstance(serviceProvider, targetDescriptor) }),
targetDescriptor.Lifetime
));
}
}
public ProjectionOptionsExtension WithCompatibilityMode(CompatibilityMode compatibilityMode)
{
var clone = Clone();
clone._compatibilityMode = compatibilityMode;
return clone;
}
public void Validate(IDbContextOptions options)
@@ -58,13 +96,29 @@ namespace EntityFrameworkCore.Projectables.Infrastructure.Internal
sealed class ExtensionInfo : DbContextOptionsExtensionInfo
{
private int? _serviceProviderHash;
public ExtensionInfo(IDbContextOptionsExtension extension) : base(extension)
{
}
public override bool IsDatabaseProvider => false;
public override string LogFragment => string.Empty;
public override long GetServiceProviderHashCode() => 0;
public override long GetServiceProviderHashCode()
{
if (_serviceProviderHash == null)
{
var hashCode = nameof(ProjectionOptionsExtension).GetHashCode();
var extension = (ProjectionOptionsExtension)Extension;
hashCode ^= extension._compatibilityMode.GetHashCode();
_serviceProviderHash = hashCode;
}
return _serviceProviderHash.Value;
}
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
{
@@ -72,6 +126,8 @@ namespace EntityFrameworkCore.Projectables.Infrastructure.Internal
{
throw new ArgumentNullException(nameof(debugInfo));
}
debugInfo["Projectables:CompatibilityMode"] = ((ProjectionOptionsExtension)Extension)._compatibilityMode.ToString();
}
}
}
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace EntityFrameworkCore.Projectables.Infrastructure
{
public class ProjectableOptionsBuilder
{
readonly DbContextOptionsBuilder _optionsBuilder;
public ProjectableOptionsBuilder(DbContextOptionsBuilder optionsBuilder)
{
_optionsBuilder = optionsBuilder ?? throw new ArgumentNullException(nameof(optionsBuilder));
}
public ProjectableOptionsBuilder CompatibilityMode(CompatibilityMode mode)
=> WithOption(x => x.WithCompatibilityMode(mode));
/// <summary>
/// Sets an option by cloning the extension used to store the settings. This ensures the builder
/// does not modify options that are already in use elsewhere.
/// </summary>
/// <param name="setAction"> An action to set the option. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
protected virtual ProjectableOptionsBuilder WithOption(Func<ProjectionOptionsExtension, ProjectionOptionsExtension> setAction)
{
((IDbContextOptionsBuilderInfrastructure)_optionsBuilder).AddOrUpdateExtension(
setAction(_optionsBuilder.Options.FindExtension<ProjectionOptionsExtension>() ?? new ProjectionOptionsExtension()));
return this;
}
}
}