简体   繁体   English

通用存储库模式 ASP NET CORE Web Api:使用包含异步

[英]Generic repository pattern ASP NET CORE Web Api: using include with async

I have following problem.我有以下问题。 My exemplary models looks as follows:我的示例模型如下所示:

public class FacilityType
{
    public int Id { get; set; }
    public string Name { get; set; } 
    public bool IsDeleted { get; set; }
}

public class Training 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual FacilityType FacilityType { get; set; } 
    public int FacilityTypeID { get; set; } 
    public bool IsDeleted { get; set; }

}

When I create new object of training type, I want it to be returned by my API with not only FacilityTypeID but also FacilityType as object, serialized in JSON.当我创建训练类型的新对象时,我希望它由我的 API 返回,不仅有 FacilityTypeID,而且还有 FacilityType 作为对象,以 JSON 序列化。

Structure of my project is as follows.我的项目结构如下。 I have generic repository pattern which I use in my controllers我有我在控制器中使用的通用存储库模式

public interface IGenericRepository<TEntity> where TEntity: class, IEntity
{
    IEnumerable<TEntity> GetAllEntities();
    Task<TEntity> GetEntityById(int id );
    Task CreateEntity(TEntity entity);
    Task UpdateEntity(int id, TEntity entity);
    Task DeleteEntity(int id);
}
public async Task<TEntity> GetEntityById(int id )
    {

        return await context.Set<TEntity>().Where(ent => ent.IsDeleted != true && ent.Id == id).FirstOrDefaultAsync();
    }   

And what I try to accomplish is find a way how to include related facility type to training which I return in method below.我试图完成的是找到一种方法,如何将相关设施类型包含到我在下面的方法中返回的培训中。

    [HttpGet("{id}")]
    public async Task<ActionResult<Training>> GetTraining(int id)
    {
         return await repository.GetEntityById(id);

    }

Thank you in advance for your contributions :)预先感谢您的贡献:)

PS: PS:

Is using predicates in methods parameter a good idea?在方法参数中使用谓词是个好主意吗? Like this: And btw.像这样:顺便说一句。 how to call such method using for example include linq expression?如何使用例如包含 linq 表达式来调用这种方法?

   public async Task<ICollection<TEntity>> GetEntityById(Expression<Func<TEntity, bool>> predicate)
    {

        //return await context.Set<TEntity>().Where(ent => ent.IsDeleted != true && ent.Id == id).FirstOrDefaultAsync();
        return await context.Set<TEntity>().Where(predicate).ToListAsync();
    }

Option 1选项1

If you have the interface IEntity already implementing the property Id :如果您的接口IEntity已经实现了属性Id

public async Task<T> Get<T>(int id, Expression<Func<T, object>> include) where T : class, IEntity
{
    using (var context = new AppContext())
    {
        return await context.Set<T>()
                           .AsNoTracking()
                           .Where(x => x.Id == id)
                           .Include(include)
                           .FirstOrDefaultAsync();
    }
}

To call this method:要调用此方法:

var entity = await _context.Get<Training>(1, x => x.FacilityType);

Option 2选项 2

Without the interface IEntity (more generic way):没有接口IEntity (更通用的方式):

public async Task<T> Get<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> include) where T : class
{
    using (var context = new AppContext())
    {
        return await context.Set<T>()
                           .AsNoTracking()
                           .Where(predicate)
                           .Include(include)
                           .FirstOrDefaultAsync();
    }
}

To call this method:要调用此方法:

var entity = await _context.Get<Training>(x => x.Id == 1, x => x.FacilityType);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM