Home > Mobile >  Non-invocable member 'IUnitOfWork.AdminMerchants' cannot be used like a method
Non-invocable member 'IUnitOfWork.AdminMerchants' cannot be used like a method

Time:05-19

In my ASP.NET Core 6 Web API project, I am implementing Repository and UnitOfWork:

I have this code:

IMerchantRepository:

public interface IAdminMerchantRepository : IGenericRepository<Merchant>
{
    IQueryable<AllMerchantListDto> GetAllMerchantAsync(PagingFilter filter);
}

MerchantRepository:

public class AdminMerchantRepository : GenericRepository<Merchant>, IAdminMerchantRepository
{
    private readonly ApplicationDbContext _dbContext;
    private readonly DbSet<Merchant> _adminMerchants;
    public AdminMerchantRepository(ApplicationDbContext dbContext) : base(dbContext)
    {
        _dbContext = dbContext;
        _adminMerchants = _dbContext.Set<Merchant>();
    }
    public IQueryable<AllMerchantListDto> GetAllMerchantAsync(PagingFilter filter)
    {
        var merchants = _dbContext.Merchants
                        .Where(x => string.IsNullOrEmpty(filter.SearchQuery) || x.User.UserName.ToLower().Contains(filter.SearchQuery.ToLower())
                    || x.User.Email.ToLower().Contains(filter.SearchQuery.ToLower())
                    || x.User.FirstName.ToLower().Contains(filter.SearchQuery.ToLower())
                    || x.User.LastName.ToLower().Contains(filter.SearchQuery.ToLower())
                    || x.MerchantName.ToLower().Contains(filter.SearchQuery.ToLower()))
                        .Include(x => x.User)
                        .OrderByDescending(x => x.CreatedAt);
        return (IQueryable<AllMerchantListDto>)merchants;
    }
}

IUnitOfWork:

public interface IUnitOfWork : IDisposable
{
    IAdminMerchantRepository AdminMerchants { get; }
    Task Save();
}

UnitOfWork:

public class UnitOfWork : IUnitOfWork
{
    private readonly ApplicationDbContext _dbContext;
    private IAdminMerchantRepository _adminMerchants;

    public UnitOfWork(ApplicationDbContext dbContext)
    {
        _dbContext = dbContext;
    }
    public IAdminMerchantRepository AdminMerchants => _adminMerchants ??= new AdminMerchantRepository(_dbContext);
    public async Task Save()
    {
        await _dbContext.SaveChangesAsync();
    }

    public void Dispose()
    {
        _dbContext.Dispose();
        GC.SuppressFinalize(this);
    }
}

I got this error:

Non-invocable member 'IUnitOfWork.AdminMerchants' cannot be used like a method

Then this AdminMerchants highlighted in:

var merchant = await _unitOfWork.AdminMerchants(filter);

CodePudding user response:

It look like you're calling
var merchant = await _unitOfWork.AdminMerchants(filter);
where you mean to be calling
var merchant = await _unitOfWork.AdminMerchants.GetAllMerchantAsync(filter);.

CodePudding user response:

  1. GetAllMerchantAsync is not async method. You need call await _unitOfWork.AdminMerchants.GetAllMerchantAsync(filter).ToListAsync() and better to rename method to GetAllMerchant.

  2. Your query will fail, because you have produced not IQueryable<AllMerchantListDto> but IQueryable<Merchant>. That's why you have applied wrong explicit cast.

It should be:

return merchants.Select(m => new AllMerchantListDto
{
   ... // assign properties
});
  1. Do not create additional abstractions if they are not needed. DbContext is already Unit Of Work and DbSet is already Repository. GetAllMerchant can be just extension method and no additional abstractions are needed.
  • Related