Skip to content

Complex code. Part 2 SoftDeleteQueryExtensions #14

@gmaFFFFF

Description

@gmaFFFFF

Thanks for the good book. It helps a lot in the study of EF Core.

In one place it seemed to me that the code could be simplified. Maybe my proposal will come in handy for you for the next publication of the book.

File: DataLayer/EfCode/SoftDeleteQueryExtensions.cs

public static void AddSoftDeleteQueryFilter(            
            this IMutableEntityType entityData,                 
            MyQueryFilterTypes queryFilterType,                 
            IUserId userIdProvider = null)                      
        {
            switch (queryFilterType) {
                case MyQueryFilterTypes.SoftDelete:
                    entityData.SetQueryFilter((Expression<Func<ISoftDelete,bool>>)(x => !x.SoftDeleted));
                    entityData.AddIndex(entityData.FindProperty(nameof(ISoftDelete.SoftDeleted)));
                    break;
                case MyQueryFilterTypes.UserId:
                    entityData.SetQueryFilter((Expression<Func<IUserId,bool>>)(x => x.UserId == userIdProvider.UserId));
                    entityData.AddIndex(entityData.FindProperty(nameof(IUserId.UserId)));
                    break;
            }
        }

P.s. To complete the picture, can an index should add a call .SetFilter("[SoftDeleted] = 0")?
P.p.s. I didn't test my code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions