不可调用成员 'IUnitOfWork.AdminMerchants' 不能像方法一样使用
Non-invocable member 'IUnitOfWork.AdminMerchants' cannot be used like a method
在我的 ASP.NET Core 6 Web API 项目中,我正在实现 Repository 和 UnitOfWork:
我有这个代码:
IMerchantRepository:
public interface IAdminMerchantRepository : IGenericRepository<Merchant>
{
IQueryable<AllMerchantListDto> GetAllMerchantAsync(PagingFilter filter);
}
商家资料库:
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();
}
工作单位:
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);
}
}
我收到这个错误:
Non-invocable member 'IUnitOfWork.AdminMerchants' cannot be used like a method
然后这个AdminMerchants突出显示在:
var merchant = await _unitOfWork.AdminMerchants(filter);
你好像在打电话
var merchant = await _unitOfWork.AdminMerchants(filter);
你打算打电话的地方
var merchant = await _unitOfWork.AdminMerchants.GetAllMerchantAsync(filter);
.
GetAllMerchantAsync
不是异步方法。您需要调用 await _unitOfWork.AdminMerchants.GetAllMerchantAsync(filter).ToListAsync()
并且最好将方法重命名为 GetAllMerchant
.
您的查询将失败,因为您生成的不是 IQueryable<AllMerchantListDto>
,而是 IQueryable<Merchant>
。这就是为什么您应用了错误的显式转换。
应该是:
return merchants.Select(m => new AllMerchantListDto
{
... // assign properties
});
- 如果不需要,不要创建额外的抽象。
DbContext
已经是工作单元,DbSet
已经是存储库。 GetAllMerchant
可以只是扩展方法,不需要额外的抽象。
在我的 ASP.NET Core 6 Web API 项目中,我正在实现 Repository 和 UnitOfWork:
我有这个代码:
IMerchantRepository:
public interface IAdminMerchantRepository : IGenericRepository<Merchant>
{
IQueryable<AllMerchantListDto> GetAllMerchantAsync(PagingFilter filter);
}
商家资料库:
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();
}
工作单位:
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);
}
}
我收到这个错误:
Non-invocable member 'IUnitOfWork.AdminMerchants' cannot be used like a method
然后这个AdminMerchants突出显示在:
var merchant = await _unitOfWork.AdminMerchants(filter);
你好像在打电话
var merchant = await _unitOfWork.AdminMerchants(filter);
你打算打电话的地方
var merchant = await _unitOfWork.AdminMerchants.GetAllMerchantAsync(filter);
.
GetAllMerchantAsync
不是异步方法。您需要调用await _unitOfWork.AdminMerchants.GetAllMerchantAsync(filter).ToListAsync()
并且最好将方法重命名为GetAllMerchant
.您的查询将失败,因为您生成的不是
IQueryable<AllMerchantListDto>
,而是IQueryable<Merchant>
。这就是为什么您应用了错误的显式转换。
应该是:
return merchants.Select(m => new AllMerchantListDto
{
... // assign properties
});
- 如果不需要,不要创建额外的抽象。
DbContext
已经是工作单元,DbSet
已经是存储库。GetAllMerchant
可以只是扩展方法,不需要额外的抽象。