如何使用 C# 添加与我的存储库模式的关系?

How can I add relation to my repository pattern with c#?

我有一个用于应用程序的存储库模式。今天一切正常。但是,我想添加包含与其他模型的关系的功能。

这是我现在的 IRepository

public interface IRepository<TModel>
    where TModel : class
{

    // Get records by it's primary key
    TModel Get(int id);

    // Get all records
    IEnumerable<TModel> GetAll();

    // Get all records matching a lambda expression
    IEnumerable<TModel> Find(Expression<Func<TModel, bool>> predicate);

    // Get the a single matching record or null
    TModel SingleOrDefault(Expression<Func<TModel, bool>> predicate);

    // Add single record
    TModel Add(TModel entity);

    // Add multiple records
    IEnumerable<TModel> AddRange(IEnumerable<TModel> entities);

    // Remove records
    void Remove(TModel entity);

    // remove multiple records
    void RemoveRange(IEnumerable<TModel> entities);
}

这是我的实体实现

public class EntityRepository<TEntity> : IRepository<TEntity>
where TEntity : class
{
protected readonly DbContext Context;

protected readonly DbSet<TEntity> DbSet;

public EntityRepository(DbContext context)
{
    Context = context;
    DbSet = context.Set<TEntity>();
}

public TEntity Get(int id)
{
    return DbSet.Find(id);
}

public IEnumerable<TEntity> GetAll()
{
    return DbSet.ToList();
}

public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
{
    return DbSet.Where(predicate);
}

public TEntity SingleOrDefault(Expression<Func<TEntity, bool>> predicate)
{
    return DbSet.SingleOrDefault(predicate);
}

public TEntity Add(TEntity entity)
{
    TEntity record = DbSet.Add(entity);

    return record;
}

public IEnumerable<TEntity> AddRange(IEnumerable<TEntity> entities)
{
    IEnumerable<TEntity> records = DbSet.AddRange(entities);

    return records;
}

public void Remove(TEntity entity)
{
    DbSet.Remove(entity);
}

public void RemoveRange(IEnumerable<TEntity> entities)
{
    DbSet.RemoveRange(entities);
}

现在,我想添加另一种方法来处理延迟加载。 换句话说,我希望能够做这样的事情

using(var con = new UnitOfWork())
{
    var task = con.Tasks.With(x => x.Owner).GetAll();
}

在我的工作单位class

public sealed class UnitOfWork : IUnitOfWork
{
    private bool Disposed = false;
    private readonly ModuleContext Context;

    public ITaskRepository Tasks { get; private set; }

    public UnitOfWork(ModuleContext context)
    {
        Context = context;
        Tasks = new TaskRepository(Context);
    }

    public int Save()
    {
        return Context.SaveChanges();
    }

    public void Dispose()
    {
        Dispose(true);
    }

    private void Dispose(bool disposing)
    {
        if (!Disposed && Context != null && disposing)
        {
            Context.Dispose();
        }

        Disposed = true;
    }
}  

这是我的任务模型

public class Task
{
    public string Name { get; set; }

    [ForeignKey("Client")]
    public int ClientId { get; set; }

    [ForeignKey("Owner")]
    public int? OwnerId { get; set; }

    public virtual Client Client { get; set; }
    public virtual User Owner { get; set; }
}

如何添加一种方法来允许我包含与不同模型的关系?

将您的方法重载添加到存储库接口以接受可能的包含表达式列表。例如

public IEnumerable<TEntity> FindAll(params Expression<Func<TEntity,object>>[] includes)
{
   var query = DbSet;
   foreach (var include in includes)
   {
      query = query.Include(include);
   }
   return query.ToList();
 }

然后你可以写:

uow.Tasks.GetAll(t=>t.Owner);

对于过滤后的情况,您可以这样做:

public IEnumerable<TEntity> Find(Expression<Func<TEntity,bool>> filter, params Expression<Func<TEntity,object>>[] includes)
{
   var query = DbSet;
   foreach (var include in includes)
   {
      query = query.Include(include);
   }
   return query.Where(filter).ToList();
 }

然后你可以写:

uow.Tasks.Find(t=>t.Something==2, t=>t.Owner);