在我之前的项目中,我为每个实体拥有单独的存储库,但现在我正在将其重构为一个公共存储库,同时利用 Base Entity。BaseEntity.cs : public abstract class BaseEntity<T> : IEntity<T> { [NotMapped] public abstract T Id { get; set; } }EFRepository.cs: public class EFRepository<TEntity, TId> : IRepository<TEntity, TId> where TEntity : BaseEntity<TId>, new() { private readonly IDbContext _context; private DbSet<TEntity> _entities; public EFRepository(IDbContext context) { _context = context; } private DbSet<TEntity> Entities { get { return _entities ?? (_entities = _context.Set<TEntity>()); } } public async Task<TEntity> GetByIdAsync(TId id) { return await Entities.FindAsync(id); } public void Insert(TEntity entity) { Entities.Add(entity); _context.SaveChanges(); } public async Task UpdateAsync(TEntity entity) { await _context.SaveChangesAsync(); } public void Delete(TId id) { var entity = new TEntity { Id = id }; // Attach the entity to the context and call the delete method. Entities.Attach(entity); Delete(entity); } public void Delete(TEntity entity) { Entities.Remove(entity); _context.SaveChanges(); } public IList<TEntity> Table { get { return Entities.ToList(); } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); }况下都很完美,但是在我有一个客户实体并且该客户实体包含字段SearchTerms的情况下,如果我必须执行过滤器,SearchTerms我无法使用上述方法实现它. 此外,我不想在基本实体中添加该字段,因为它仅特定于客户实体。关于如何过滤结果的任何帮助SearchTerms?
1 回答
Cats萌萌
TA贡献1805条经验 获得超9个赞
创建一个单独的CustomerRepository类并实现您自己的过滤器逻辑,如下所示。
public class CustomerRepository : EFRepository<Customer, Guid>, ICustomerRepository
{
public CustomerRepository(IDbContext context): base(context) {}
public async Task<List<Customer>> GetCustomerBySearchTerms(string[] searchTerms)
{
//implement your logic here
}
}
- 1 回答
- 0 关注
- 153 浏览
添加回答
举报
0/150
提交
取消