不可用的成员' iunitofwork.adminmerchants'不能像方法一样使用

发布于 2025-01-30 04:52:48 字数 2552 浏览 2 评论 0原文

在我的ASP.NET Core 6 Web API项目中,我正在实施存储库和Unitofwork:

我有此代码:

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:unitofwork:

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);
    }
}

我有此错误:

不可用的成员'iunitofwork.adminmerchants'不能像方法

那样使用

,然后在以下方式中突出显示此管理员

var merchant =等待_unitofwork.adminmerchants(filter);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别挽留 2025-02-06 04:52:48

看起来您正在打电话
var merchant =等待_unitofwork.adminmerchants(filter);
您的意思是打电话
var merchant =等待_unitofwork.adminmerchants.getAllmerantAsync(filter);

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);.

当梦初醒 2025-02-06 04:52:48
  1. getAllmerantAsync不是异步方法。您需要调用等待_unitofwork.adminmerchants.getAllMerChantAsync(filter).tolistasync(),更好地将方法重命名为getallmerchant


  2. 您的查询将失败,因为您没有产生iqueryable&lt; allmerchantlistdto&gt;,但iqueryable&lt; merchant&gt;。这就是为什么您应用了错误的显式演员。

应该是:

return merchants.Select(m => new AllMerchantListDto
{
   ... // assign properties
});
  1. 如果不需要,请勿创建其他抽象。 dbContext已经是工作的单位,dbset已经是存储库。 getallmerant可以只是扩展方法,不需要其他抽象。
  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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文