EfCoreinAction-SecondEdition icon indicating copy to clipboard operation
EfCoreinAction-SecondEdition copied to clipboard

Complex code. Part 2 SoftDeleteQueryExtensions

Open gmaFFFFF opened this issue 9 months ago • 0 comments

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

gmaFFFFF avatar Apr 26 '25 17:04 gmaFFFFF