Skip to content

Automatically perform required conversions when query filter is defined on interface or base type #10257

Closed as duplicate

Description

@nphmuller

Say I have the following interface that can be implemented on any entity that I want to support soft-deletion.

public interface ISoftDeletableEntity
{
    bool IsDeleted { get; set; }
}

It would be nice to be able to define a global query filter for any entity type that implements this interface like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<ISoftDeletableEntity>().HasQueryFilter(e => e.IsDeleted == false);
}

Currently this throws the following exception: System.ArgumentException: 'The entity type 'MyApp.ISoftDeletableEntity' provided for the argument 'clrType' must be a reference type.'

Activity

  1. ajcvickers commented on Nov 10, 2017

    @ajcvickers
    Contributor

    @nphmuller This is something that can be done with bulk configuration on the mapped entity types:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        foreach (var entityType in modelBuilder.Model.GetEntityTypes()
            .Where(e => typeof(ISoftDeletableEntity).IsAssignableFrom(e.ClrType)))
        {
            modelBuilder
                .Entity(entityType.ClrType)
                .HasQueryFilter(ConvertFilterExpression<ISoftDeletableEntity>(e => !e.IsDeleted, entityType.ClrType));
        }
    }
    
    private static LambdaExpression ConvertFilterExpression<TInterface>(
        Expression<Func<TInterface, bool>> filterExpression, 
        Type entityType)
    {
        var newParam = Expression.Parameter(entityType);
        var newBody = ReplacingExpressionVisitor.Replace(filterExpression.Parameters.Single(), newParam, filterExpression.Body);
    
        return Expression.Lambda(newBody, newParam);
    }

    Making a better experience around this is covered by #9117 and #6787.

    The code is more complicated in this case than in many others because it requires constructing a lambda expression where the parameter is validated to be the exact entity type--not the interface. I'm going to re-purpose this bug to automatically handle base types/interfaces in EF code. After this issue is fixed it should be possible to simplify the above to:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        Expression<Func<ISoftDeletableEntity, bool>> expression = e => !e.IsDeleted;
    
        foreach (var entityType in modelBuilder.Model.GetEntityTypes()
            .Where(e => typeof(ISoftDeletableEntity).IsAssignableFrom(e.ClrType)))
        {
            modelBuilder.Entity(entityType.ClrType).HasQueryFilter(expression);
        }
    }
  2. changed the title [-]Allow model-level filters by base-type or interface.[/-] [+]Automatically perform required conversions when filter is defined on interface of base type[/+] on Nov 10, 2017
  3. changed the title [-]Automatically perform required conversions when filter is defined on interface of base type[/-] [+]Automatically perform required conversions when query filter is defined on interface or base type[/+] on Nov 10, 2017
  4. HappyNomad commented on Nov 11, 2017

    @HappyNomad

    @ajcvickers Wouldn't be more efficient to set IMutableEntityType.QueryFilter like my example at #8881 (comment)? That way you don't look up every affected entity type twice. Please "Automatically perform required conversions when query filter is defined" in that manner, too.

  5. nphmuller commented on Nov 12, 2017

    @nphmuller
    Author

    @ajcvickers
    Nice. Was hoping it was possible already, but got stuck rewriting the expression. Thanks for the sample. It helps out a lot!

    Totally agree with the repurpose. The sample after the fix is way easier to write.

  6. added this to the Backlog milestone on Nov 17, 2017
  7. ajcvickers commented on Oct 27, 2021

    @ajcvickers
    Contributor

    Note from triage: if we implement this, then it will be as part of general mapping configuration for all uses of an interface, tracked by #6787.

  8. removed this from the Backlog milestone on Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions