0

My repository class has a few methods for operations with the database. I recently developed the FindAsync() method, which allows you to grab only a few rows from the database based on an expression.

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Orbit.Infrastructure.Repositories.Interfaces;
using System.Linq.Expressions;

namespace Orbit.Infrastructure.Repositories
{
    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        protected readonly DbContext Context;

        public Repository(DbContext context, IConfiguration configuration)
        {
            IConfigurationSection sectNamespaces = configuration.GetSection("AllowedEntityNamespaces");
            List<string> namespacesInConfig = [];

            foreach (IConfigurationSection child in sectNamespaces.GetChildren())
            {
                namespacesInConfig.Add(child.Path);
            }

            if (namespacesInConfig.Contains(typeof(TEntity).Namespace!))
            {
                throw new ArgumentException($"O namespace ${typeof(TEntity).Namespace} não está habilitado!");
            }

            Context = context;
        }

        public async Task AddAsync(TEntity entity)
        {
            ArgumentNullException.ThrowIfNull(nameof(entity));
            _ = await Context.Set<TEntity>().AddAsync(entity);
        }

        public async Task AddRangeAsync(IEnumerable<TEntity> entities)
        {
            ArgumentNullException.ThrowIfNull(nameof(entities));
            await Context.Set<TEntity>().AddRangeAsync(entities);
        }

        public async Task<IEnumerable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate)
        {
            ArgumentNullException.ThrowIfNull(predicate);
            return await Context.Set<TEntity>().Where(predicate).ToListAsync();
        }

        public async Task<IEnumerable<TEntity>> GetAllAsync()
        {
            return await Context.Set<TEntity>().ToListAsync();
        }

        public async Task<IEnumerable<TEntity>> GetAllAsync(params string[] navProperties)
        {
            IQueryable<TEntity> entities = Context.Set<TEntity>().AsQueryable();

            foreach (string prop in navProperties)
            {
                entities = entities.Include(prop);
            }

            return await entities.ToListAsync();
        }

        public async Task<TEntity?> GetAsync(int id)
        {
            ArgumentNullException.ThrowIfNull(id);
            return await Context.Set<TEntity>().FindAsync(id);
        }

        public async Task RemoveAsync(TEntity entity)
        {
            ArgumentNullException.ThrowIfNull(entity);
            await Task.Run(() =>
            {
                _ = Context.Set<TEntity>().Remove(entity);
            });
        }

        public async Task RemoveRangeAsync(IEnumerable<TEntity> entities)
        {
            ArgumentNullException.ThrowIfNull(entities);
            await Task.Run(() =>
            {
                Context.Set<TEntity>().RemoveRange(entities);
            });
        }

    }
}

Up until now, in most operations where I needed to grab one or more users based on a specific criterion, I pulled in all the users (using GetAllAsync()) and only then did I filter the users using LINQ. I realized that this was not the best solution. Ideally, you would want to throw this load of pulling a few specific users into the efcore and balance the load on the application.

In my class of services I also developed a method that allows you to find a user based on a predicate and that's where the problem lies.

UserAddRequest (DTO)

using Orbit.Domain.Entities;
using System.ComponentModel.DataAnnotations;

namespace Orbit.Application.Dtos.Requests
{
    public class UserAddRequest
    {
        [Required(ErrorMessage = "Insira o nome do usuário!")]
        [StringLength(100, MinimumLength = 5, ErrorMessage = "O nome do usuário deve ter no máximo 100 caracteres.")]
        [RegularExpression(@"^[a-zA-Z0-9_]*$", ErrorMessage = "O nome do usuário só pode conter letras, números e underline.")]
        [Display(Name = "Name")]
        public string UserName { get; set; } = null!;

        [Required(ErrorMessage = "Insira o email do usuário!")]
        [StringLength(200, ErrorMessage = "O email do usuário deve ter no máximo 200 caracteres.")]
        [EmailAddress(ErrorMessage = "O email do usuário não é válido.")]
        [Display(Name = "Email")]
        public string UserEmail { get; set; } = null!;

        [Required(ErrorMessage = "Insira a data de nascimento do usuário!")]
        [DataType(DataType.Date, ErrorMessage = "Formato de data inválido.")]
        [Display(Name = "Data de Nascimento")]
        public DateOnly UserDateOfBirth { get; set; }

        [Required(ErrorMessage = "Insira a senha do usuário!")]
        [StringLength(200, ErrorMessage = "A senha do usuário deve ter no máximo 200 caracteres.")]
        [RegularExpression(@"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$ %^&*-]).{8,}$", ErrorMessage = "A senha deve conter pelo menos uma letra minúscula, uma letra maiúscula, um caractere especial (@$!%*?&)")]
        [Display(Name = "Senha")]
        public string UserPassword { get; set; } = null!;

        [Required(ErrorMessage = "O campo {0} é obrigatório.")]
        [Range(1, 31, ErrorMessage = "O campo {0} deve estar entre 1 e 31.")]
        [Display(Name = "Dia")]
        public int Day { get; set; }

        [Required(ErrorMessage = "O campo {0} é obrigatório.")]
        [Range(1, 12, ErrorMessage = "O campo {0} deve estar entre 1 e 12.")]
        [Display(Name = "Mês")]
        public int Month { get; set; }

        [Required(ErrorMessage = "O campo {0} é obrigatório.")]
        [Range(1, 9999, ErrorMessage = "O campo {0} deve estar entre 1 e 9999.")]
        [Display(Name = "Ano")]
        public int Year { get; set; }

        public User ToUser()
        {
            return new User
            {
                UserName = UserName,
                UserDateOfBirth = new DateOnly(Year, Month, Day),
                UserEmail = UserEmail,
                UserPassword = UserPassword
            };
        }
    }
}

UserResponse (DTO)

using Orbit.Domain.Entities;

namespace Orbit.Application.Dtos.Responses
{
    public class UserResponse
    {
        public uint UserId { get; set; }
        public string UserName { get; set; } = null!;
        public string UserEmail { get; set; } = null!;
        public DateOnly UserDateOfBirth { get; set; }
        public string UserPassword { get; set; } = null!;
        public string? UserDescription { get; set; }
        public byte[]? UserImageByteType { get; set; }
        public string? UserProfileName { get; set; }
        public ICollection<UserResponse>? Followers { get; set; }
        public ICollection<UserResponse>? Users { get; set; }
    }

    public static class UserExtensions
    {
        public static UserResponse ToUserResponse(this User user)
        {
            return user.ToUserResponseInternal([]);
        }

        private static UserResponse ToUserResponseInternal(this User user, List<uint> processedUserIds)
        {
            if (processedUserIds.Contains(user.UserId))
            {
                return new UserResponse
                {
                    UserId = user.UserId,
                    UserName = user.UserName,
                    UserEmail = user.UserEmail,
                    UserDateOfBirth = user.UserDateOfBirth,
                    UserPassword = user.UserPassword,
                    UserDescription = user.UserDescription,
                    UserProfileName = user.UserProfileName,
                    UserImageByteType = user.UserImageByteType,
                    Followers = [],
                    Users = []
                };
            }

            processedUserIds.Add(user.UserId);

            UserResponse response = new()
            {
                UserId = user.UserId,
                UserName = user.UserName,
                UserEmail = user.UserEmail,
                UserDateOfBirth = user.UserDateOfBirth,
                UserPassword = user.UserPassword,
                UserDescription = user.UserDescription,
                UserProfileName = user.UserProfileName,
                UserImageByteType = user.UserImageByteType,
                Followers = user.Followers.Select(u => u.ToUserResponseInternal(new List<uint>(processedUserIds))).ToList(),
                Users = user.Users.Select(u => u.ToUserResponseInternal(new List<uint>(processedUserIds))).ToList()
            };

            return response;
        }
    }
}

User model (EF Core and repository model):

namespace Orbit.Domain.Entities;

public partial class User
{
    public uint UserId { get; set; }

    public string UserName { get; set; } = null!;
    public string UserEmail { get; set; } = null!;

    public DateOnly UserDateOfBirth { get; set; }

    public string UserPassword { get; set; } = null!;
    public string? UserDescription { get; set; }

    public byte[]? UserImageByteType { get; set; }

    public string? UserProfileName { get; set; }

    public virtual ICollection<User> Followers { get; set; } = [];
    public virtual ICollection<User> Users { get; set; } = [];
}

My FindUserAsync method of the UserService class has an Expression<Func<UserResponse, bool>> as a parameter, and the FindAsync method of the repository takes an Expression<Func<TEntity, bool>> as an argument. In the service class, the generic TEntity is actually the User model

public async Task<IEnumerable<UserResponse>> FindUsersAsync(Func<UserResponse, bool> predicate)
{
    // This isn't allowed. Causes compilation error
    return await _unitOfWork.User.FindAsync(predicate); 
}

How can I convert one expression to another? Is there a way to convert the expressions or use string to map? I don't have the slightest idea how to proceed from here.

I've tried to find some solutions on the internet, but I haven't found anything that I understand in the slightest bit about what's going on.

Thanks!

12
  • Check ExpressionTypeMapper, But better to do not use generic repository at all. Commented Jul 1 at 4:19
  • #1. Never use Generic Repositories with EF. #2. Don't use repositories if the sole reason for the repository is to try and abstract EF... The biggest argument for Repositories I come across is "so that my code is not dependent on EF-isms".. Except when you introduce complexities like Expressions, those expressions must still conform to EF-isms. I.e. an expression that calls a function still won't work. So you are doing a lot of work and re-inventing the wheel for no good reason.
    – Steve Py
    Commented Jul 1 at 5:27
  • All generic repositories accomplish with EF is making you do a lot more work, encounter a lot more problems (both complexity and performance), and put a straight-jacket around what EF's existing Generic Repository, the DbSet<T> already provides.
    – Steve Py
    Commented Jul 1 at 5:29
  • @StevePy I would not judge so hard over generic repositories. I always say: EF Core is an abstraction to the database and generic repositories are an abstraction to the OR-mapper. And I have had projects where we had an generic repository implementation with EF and another generic repository implementation to a MongoDB and we could switch on a request base to switch between these implementations without affecting our business commands. So that can make sense, also for unify Unit Tests. But I know this discussion is very opinionated.
    – decius
    Commented Jul 1 at 6:47
  • @decius Something like that would be for data layer polymorphism more-so than a Repository where there is a distinct business requirement to support two dissimilar data sources in a common way. There are certainly trade-offs for performance/flexibility which is fine when that is a requirement. The problem with 99% of Generic Repository implementations is there is no such requirement. The justification is maybe EF will be "too slow" so abstract so it can be swapped out if that is the case, or too complicated for Jr devs. It leads to a self-fulfilling prophecy by causing the problems they fear.
    – Steve Py
    Commented Jul 1 at 8:52

0