Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There are lot's of duplicated code #44

Open
Zhao-Michael opened this issue Jan 4, 2024 · 3 comments · May be fixed by #58
Open

There are lot's of duplicated code #44

Zhao-Michael opened this issue Jan 4, 2024 · 3 comments · May be fixed by #58
Labels
Improvement Need to improve the existing code

Comments

@Zhao-Michael
Copy link

The code to build 'expressionTree' are duplicated

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

Expression<Func<T, bool>> expressionTree = Expression.Lambda<Func<T, bool>>(body, new[] { pe });

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

@TanvirArjel
Copy link
Owner

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

@TanvirArjel TanvirArjel added the Improvement Need to improve the existing code label Jan 8, 2024
@Zhao-Michael
Copy link
Author

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 added a commit to dmitrii-kiselev/EFCore.GenericRepository that referenced this issue Sep 24, 2024
@dmitrii-kiselev
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Improvement Need to improve the existing code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants