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

时间:2017-03-27 17:34:33

标签: c# asp.net-mvc entity-framework repository repository-pattern

我有一个我用于应用程序的存储库模式。今天一切都很完美。但是,我想添加将关系包含到其他模型的能力。

这是我当前的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();
}

这是我的工作单元课程

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

如何添加一种方法以允许我将关系包含在不同的模型中?

1 个答案:

答案 0 :(得分:1)

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

foreach ($fileData() as $line) {
    // $line contains current line
}

然后你可以写:

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