Skip to content

Commit

Permalink
Proof of concept: ad-hoc entity types
Browse files Browse the repository at this point in the history
This is primarily for #10753

This is a draft; comments, exceptions, some testing intentionally left not done yet.

The idea is to build and ad-hoc entity type which can then used for a raw SQL query. However, it actually also allows composing over the raw SQL query and ad-hoc LINQ queries.

Things to consider:

- The entity type cannot have relationships
- Properties are mapped by convention, but mapping attributes are respected. This is more important for LINQ queries than raw SQL queries since it allows EF to target the correct table and columns.
- The entity types are keyless. We could support keys, which would open up inserts, updates, and deletes, but I'm not sure we should.
- This happens automatically if the CLR type is not mapped. This means no new API service, but it also means it could be easy to accidentally use an ad-hoc type if you forget to add the type to the model. On the other hand, and type with navigations will fail anyway, so perhaps with a reasonable exception message its okay.
  • Loading branch information
ajcvickers committed Dec 26, 2022
1 parent 30f26cf commit 74bd92d
Show file tree
Hide file tree
Showing 20 changed files with 3,046 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public override EntityFrameworkServicesBuilder TryAddCoreServices()
TryAdd<IRelationalParameterBasedSqlProcessorFactory, RelationalParameterBasedSqlProcessorFactory>();
TryAdd<IRelationalQueryStringFactory, RelationalQueryStringFactory>();
TryAdd<IQueryCompilationContextFactory, RelationalQueryCompilationContextFactory>();
TryAdd<IAdHocMapper, RelationalAdHocMapper>();

ServiceCollectionMap.GetInfrastructure()
.AddDependencySingleton<RelationalSqlGenerationHelperDependencies>()
Expand Down
65 changes: 65 additions & 0 deletions src/EFCore.Relational/Metadata/RelationalAdHocMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Microsoft.EntityFrameworkCore.Metadata;

#pragma warning disable CS1591

public class RelationalAdHocMapper : AdHocMapper
{
public RelationalAdHocMapper(AdHocMapperDependencies dependencies)
: base(dependencies)
{
}

public override IEntityType MapAdHocEntityType(Type clrType)
{
var entityType = CreateEntityType(clrType);

var relationalModel = (RelationalModel)Dependencies.Model.GetRelationalModel();

var tableMappings = new List<TableMapping>();
entityType.AddRuntimeAnnotation(RelationalAnnotationNames.DefaultMappings, tableMappings);
entityType.AddRuntimeAnnotation(RelationalAnnotationNames.TableMappings, tableMappings);

var (tableName, schema) = GetTableName(clrType);
var table = new Table(tableName, schema, relationalModel);
var tableMapping = new TableMapping(entityType, table, false);
tableMappings.Add(tableMapping);

foreach (var (member, field, propertyName) in GetMembersToMap(clrType))
{
var property = CreateProperty(propertyName, member, field, entityType);

var columnName = GetColumnName(propertyName, member, field);
var column = new Column(columnName, ((RelationalTypeMapping)property.TypeMapping).StoreType, table)
{
IsNullable = ((IReadOnlyProperty)property).IsNullable
};

table.Columns.Add(column.Name, column);
var columnMapping = new ColumnMapping(property, column, tableMapping);
tableMapping.AddColumnMapping(columnMapping);
column.AddPropertyMapping(columnMapping);

var columnMappings = new SortedSet<ColumnMapping>(ColumnMappingBaseComparer.Instance);
property.AddRuntimeAnnotation(RelationalAnnotationNames.TableColumnMappings, columnMappings);
columnMappings.Add(columnMapping);
}

return ((RuntimeModel)Dependencies.Model).GetOrAddAdHocEntityType(entityType);
}

protected virtual (string TableName, string? Schema) GetTableName(Type clrType)
{
var tableAttribute = clrType.GetCustomAttributes<TableAttribute>(inherit: false).FirstOrDefault();

return (tableAttribute?.Name ?? clrType.Name, tableAttribute?.Schema);
}

protected virtual string GetColumnName(string propertyName, MemberInfo member, FieldInfo? field)
=> member.GetCustomAttributes<ColumnAttribute>(inherit: false).FirstOrDefault()?.Name ?? propertyName;
}
10 changes: 10 additions & 0 deletions src/EFCore/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ public virtual DbContextId ContextId
IDbSetSource IDbContextDependencies.SetSource
=> DbContextDependencies.SetSource;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
IAdHocMapper IDbContextDependencies.AdHocMapper
=> DbContextDependencies.AdHocMapper;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
72 changes: 72 additions & 0 deletions src/EFCore/Infrastructure/AdHocMapperDependencies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Infrastructure;

/// <summary>
/// <para>
/// Service dependencies parameter class for <see cref="IAdHocMapper" />
/// </para>
/// <para>
/// This type is typically used by database providers (and other extensions). It is generally
/// not used in application code.
/// </para>
/// </summary>
/// <remarks>
/// <para>
/// Do not construct instances of this class directly from either provider or application code as the
/// constructor signature may change as new dependencies are added. Instead, use this type in
/// your constructor so that an instance will be created and injected automatically by the
/// dependency injection container. To create an instance with some dependent services replaced,
/// first resolve the object from the dependency injection container, then replace selected
/// services using the 'With...' methods. Do not call the constructor at any point in this process.
/// </para>
/// <para>
/// The service lifetime is <see cref="ServiceLifetime.Scoped" />. This means that each
/// <see cref="DbContext" /> instance will use its own instance of this service.
/// The implementation may depend on other services registered with any lifetime.
/// The implementation does not need to be thread-safe.
/// </para>
/// </remarks>
public sealed record AdHocMapperDependencies
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
/// <remarks>
/// Do not call this constructor directly from either provider or application code as it may change
/// as new dependencies are added. Instead, use this type in your constructor so that an instance
/// will be created and injected automatically by the dependency injection container. To create
/// an instance with some dependent services replaced, first resolve the object from the dependency
/// injection container, then replace selected services using the 'With...' methods. Do not call
/// the constructor at any point in this process.
/// </remarks>
[EntityFrameworkInternal]
public AdHocMapperDependencies(
IModel model,
ITypeMappingSource typeMappingSource,
IDiagnosticsLogger<DbLoggerCategory.Model> logger)
{
Model = model;
TypeMappingSource = typeMappingSource;
Logger = logger;
}

/// <summary>
/// The <see cref="IModel"/>.
/// </summary>
public IModel Model { get; init; }

/// <summary>
/// The <see cref="TypeMappingSource"/>.
/// </summary>
public ITypeMappingSource TypeMappingSource { get; init; }

/// <summary>
/// The logger.
/// </summary>
public IDiagnosticsLogger<DbLoggerCategory.Model> Logger { get; init; }
}
5 changes: 4 additions & 1 deletion src/EFCore/Infrastructure/EntityFrameworkServicesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public static readonly IDictionary<Type, ServiceCharacteristics> CoreServices
{ typeof(IQueryTranslationPostprocessorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IShapedQueryCompilingExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IDbContextLogger), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IAdHocMapper), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ILazyLoader), new ServiceCharacteristics(ServiceLifetime.Transient) },
{ typeof(IParameterBindingFactory), new ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: true) },
{ typeof(ITypeMappingSourcePlugin), new ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: true) },
Expand Down Expand Up @@ -301,6 +302,7 @@ public virtual EntityFrameworkServicesBuilder TryAddCoreServices()
TryAdd<IQueryTranslationPostprocessorFactory, QueryTranslationPostprocessorFactory>();
TryAdd<INavigationExpansionExtensibilityHelper, NavigationExpansionExtensibilityHelper>();
TryAdd<IExceptionDetector, ExceptionDetector>();
TryAdd<IAdHocMapper, AdHocMapper>();

TryAdd(
p => p.GetService<IDbContextOptions>()?.FindExtension<CoreOptionsExtension>()?.DbContextLogger
Expand Down Expand Up @@ -338,7 +340,8 @@ public virtual EntityFrameworkServicesBuilder TryAddCoreServices()
.AddDependencyScoped<ValueGeneratorSelectorDependencies>()
.AddDependencyScoped<DatabaseDependencies>()
.AddDependencyScoped<ModelDependencies>()
.AddDependencyScoped<ModelCreationDependencies>();
.AddDependencyScoped<ModelCreationDependencies>()
.AddDependencyScoped<AdHocMapperDependencies>();

ServiceCollectionMap.TryAddSingleton<IRegisteredServices>(
new RegisteredServices(ServiceCollectionMap.ServiceCollection.Select(s => s.ServiceType)));
Expand Down
10 changes: 10 additions & 0 deletions src/EFCore/Internal/DbContextDependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public DbContextDependencies(
ICurrentDbContext currentContext,
IChangeDetector changeDetector,
IDbSetSource setSource,
IAdHocMapper adHocMapper,
IEntityFinderSource entityFinderSource,
IEntityGraphAttacher entityGraphAttacher,
IAsyncQueryProvider queryProvider,
Expand All @@ -44,6 +45,7 @@ public DbContextDependencies(
{
ChangeDetector = changeDetector;
SetSource = setSource;
AdHocMapper = adHocMapper;
EntityGraphAttacher = entityGraphAttacher;
QueryProvider = queryProvider;
StateManager = stateManager;
Expand All @@ -61,6 +63,14 @@ public DbContextDependencies(
/// </summary>
public IDbSetSource SetSource { get; init; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public IAdHocMapper AdHocMapper { get; init; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore/Internal/IDbContextDependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public interface IDbContextDependencies
/// </summary>
IDbSetSource SetSource { get; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
IAdHocMapper AdHocMapper { get; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
25 changes: 15 additions & 10 deletions src/EFCore/Internal/InternalDbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Query.Internal;

namespace Microsoft.EntityFrameworkCore.Internal;
Expand All @@ -29,6 +28,7 @@ public class InternalDbSet<[DynamicallyAccessedMembers(IEntityType.DynamicallyAc
private EntityQueryable<TEntity>? _entityQueryable;
private LocalView<TEntity>? _localView;
private IEntityFinder<TEntity>? _finder;
private IAdHocMapper? _adHocMapper;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -70,15 +70,17 @@ public override IEntityType EntityType
throw new InvalidOperationException(CoreStrings.InvalidSetSharedType(typeof(TEntity).ShortDisplayName()));
}

var findSameTypeName = _context.Model.FindSameTypeNameWithDifferentNamespace(typeof(TEntity));
//if the same name exists in your entity types we will show you the full namespace of the type
if (!string.IsNullOrEmpty(findSameTypeName))
{
throw new InvalidOperationException(
CoreStrings.InvalidSetSameTypeWithDifferentNamespace(typeof(TEntity).DisplayName(), findSameTypeName));
}

throw new InvalidOperationException(CoreStrings.InvalidSetType(typeof(TEntity).ShortDisplayName()));
_entityType = AdHocMapper.MapAdHocEntityType(typeof(TEntity));

// var findSameTypeName = _context.Model.FindSameTypeNameWithDifferentNamespace(typeof(TEntity));
// // If the same name exists in your entity types we will show you the full namespace of the type
// if (!string.IsNullOrEmpty(findSameTypeName))
// {
// throw new InvalidOperationException(
// CoreStrings.InvalidSetSameTypeWithDifferentNamespace(typeof(TEntity).DisplayName(), findSameTypeName));
// }
//
// throw new InvalidOperationException(CoreStrings.InvalidSetType(typeof(TEntity).ShortDisplayName()));
}

if (_entityType.IsOwned())
Expand Down Expand Up @@ -466,6 +468,9 @@ private IEntityFinder<TEntity> Finder
}
}

private IAdHocMapper AdHocMapper
=> _adHocMapper ??= _context.GetDependencies().AdHocMapper;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
Loading

0 comments on commit 74bd92d

Please sign in to comment.