TanvirArjel / EFCore.GenericRepository

This repository contains Generic Repository implementation for Entity Framework Core
MIT License
599 stars 89 forks source link

There are lot's of duplicated code #44

Open Zhao-Michael opened 10 months ago

Zhao-Michael commented 10 months ago

The code to build 'expressionTree' are duplicated

https://github.com/TanvirArjel/EFCore.GenericRepository/blob/01a306dc7ed892806d669cdbc183138ad721c068/src/TanvirArjel.EFCore.QueryRepository/QueryRepository.cs#L330C2-L330C2

https://github.com/TanvirArjel/EFCore.GenericRepository/blob/01a306dc7ed892806d669cdbc183138ad721c068/src/TanvirArjel.EFCore.QueryRepository/QueryRepository.cs#L389

https://github.com/TanvirArjel/EFCore.GenericRepository/blob/01a306dc7ed892806d669cdbc183138ad721c068/src/TanvirArjel.EFCore.QueryRepository/QueryRepository.cs#L571C38-L571C38

TanvirArjel commented 10 months ago

@Zhao-Michael Thank you so much for reaching out. I shall take care of it. Or You can request an MR.

Zhao-Michael commented 9 months ago

Hi TanvirArjel, Actually, I'm not really understand why we need to build the lambda expressions here. Wouldn't there be a performance problem? or Isn't there a better and more elegant way?

dmitrii-kiselev commented 1 month ago

Hi @Zhao-Michael,

I removed the duplicate code (#58). I was waiting for the review and comments from @TanvirArjel.

About lambda expressions: DbContext provides several methods to choose from for searching key fields:

public virtual object? Find(
        [DynamicallyAccessedMembers(IEntityType.DynamicallyAccessedMemberTypes)] Type entityType,
        params object?[]? keyValues)

public virtual TEntity? Find<[DynamicallyAccessedMembers(IEntityType.DynamicallyAccessedMemberTypes)] TEntity>(
        params object?[]? keyValues)
        where TEntity : class

public virtual ValueTask<object?> FindAsync(
        [DynamicallyAccessedMembers(IEntityType.DynamicallyAccessedMemberTypes)] Type entityType,
        params object?[]? keyValues)

public virtual ValueTask<object?> FindAsync(
        [DynamicallyAccessedMembers(IEntityType.DynamicallyAccessedMemberTypes)] Type entityType,
        object?[]? keyValues,
        CancellationToken cancellationToken)

public virtual ValueTask<TEntity?> FindAsync<
        [DynamicallyAccessedMembers(IEntityType.DynamicallyAccessedMemberTypes)] TEntity>(params object?[]? keyValues)
        where TEntity : class

public virtual ValueTask<TEntity?> FindAsync<[DynamicallyAccessedMembers(IEntityType.DynamicallyAccessedMemberTypes)] TEntity>(
        object?[]? keyValues,
        CancellationToken cancellationToken)
        where TEntity : class

However, these methods do not support IIncludableQueryable or custom select projection.

Therefore, we use IQueryable<T> with FirstOrDefaultAsync to combine getByIdExpression (to filter by key value), IIncludableQueryable (to include related data), asNoTracking (track changes or not), and selectExpression (custom select projection). I don't know of any more elegant way to search for an entity by key, given a type, and include the related data and other settings. If you have any ideas, please share.

Unfortunately, the current implementation does not allow searching if an entity has multiple key fields. But that's a topic for another issue.

Best Regards, Dmitrii Kiselev