简体   繁体   English

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

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

I have a repository pattern that I am using for an application.我有一个用于应用程序的存储库模式。 Everything is working perfectly today.今天一切正常。 However, I want to add ability to include relations to other models.但是,我想添加包含与其他模型的关系的能力。

Here is my current IRepository这是我目前的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);
}

Here is my Entity implementation这是我的实体实现

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

Now, I want to add another method to allow me to handle lazy loading.现在,我想添加另一种方法来处理延迟加载。 In another words, I want to be able to do something like this换句话说,我希望能够做这样的事情

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

Here in my Unit of work 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;
    }
}  

Here is my task model这是我的任务模型

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

How can I add a way to allow me to include relations to different models?如何添加一种方法来允许我包含与不同模型的关系?

Add an overload for your methods to the repository interface to accept a list of possible include-expressions.将您的方法的重载添加到存储库接口以接受可能的包含表达式列表。 Eg例如

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

And then you can just write:然后你可以写:

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

For the filtered case you can do something like this:对于过滤的情况,您可以执行以下操作:

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

And then you can just write:然后你可以写:

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

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

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