title: How can I add relation to my repository pattern with c#?
body: <p>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.</p>
  
  <p>Here is my current <code>IRepository</code></p>
  
  <pre><code>public interface IRepository&lt;TModel&gt;
      where TModel : class
  {
  
      // Get records by it's primary key
      TModel Get(int id);
  
      // Get all records
      IEnumerable&lt;TModel&gt; GetAll();
  
      // Get all records matching a lambda expression
      IEnumerable&lt;TModel&gt; Find(Expression&lt;Func&lt;TModel, bool&gt;&gt; predicate);
  
      // Get the a single matching record or null
      TModel SingleOrDefault(Expression&lt;Func&lt;TModel, bool&gt;&gt; predicate);
  
      // Add single record
      TModel Add(TModel entity);
  
      // Add multiple records
      IEnumerable&lt;TModel&gt; AddRange(IEnumerable&lt;TModel&gt; entities);
  
      // Remove records
      void Remove(TModel entity);
  
      // remove multiple records
      void RemoveRange(IEnumerable&lt;TModel&gt; entities);
  }
  </code></pre>
  
  <p>Here is my Entity implementation</p>
  
  <pre><code>public class EntityRepository&lt;TEntity&gt; : IRepository&lt;TEntity&gt;
  where TEntity : class
  {
  protected readonly DbContext Context;
  
  protected readonly DbSet&lt;TEntity&gt; DbSet;
  
  public EntityRepository(DbContext context)
  {
      Context = context;
      DbSet = context.Set&lt;TEntity&gt;();
  }
  
  public TEntity Get(int id)
  {
      return DbSet.Find(id);
  }
  
  public IEnumerable&lt;TEntity&gt; GetAll()
  {
      return DbSet.ToList();
  }
  
  public IEnumerable&lt;TEntity&gt; Find(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
  {
      return DbSet.Where(predicate);
  }
  
  public TEntity SingleOrDefault(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
  {
      return DbSet.SingleOrDefault(predicate);
  }
  
  public TEntity Add(TEntity entity)
  {
      TEntity record = DbSet.Add(entity);
  
      return record;
  }
  
  public IEnumerable&lt;TEntity&gt; AddRange(IEnumerable&lt;TEntity&gt; entities)
  {
      IEnumerable&lt;TEntity&gt; records = DbSet.AddRange(entities);
  
      return records;
  }
  
  public void Remove(TEntity entity)
  {
      DbSet.Remove(entity);
  }
  
  public void RemoveRange(IEnumerable&lt;TEntity&gt; entities)
  {
      DbSet.RemoveRange(entities);
  }
  </code></pre>
  
  <p>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</p>
  
  <pre><code>using(var con = new UnitOfWork())
  {
      var task = con.Tasks.With(x =&gt; x.Owner).GetAll();
  }
  </code></pre>
  
  <p>Here in my Unit of work class</p>
  
  <pre><code>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 &amp;&amp; Context != null &amp;&amp; disposing)
          {
              Context.Dispose();
          }
  
          Disposed = true;
      }
  }  
  </code></pre>
  
  <p>Here is my task model</p>
  
  <pre><code>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; }
  }
  </code></pre>
  
  <p>How can I add a way to allow me to include relations to different models?</p>