title: How implement generic repository against mongodb?
body: <p>I am new to mongodb and I am working on mvc4 web application for a new project.</p>
  
  <p>I want to use repository pattern that will talk to a mongodb context for database level communications.</p>
  
  <p>Simple interface that I have used with Entity Framework 4.0 is following. The find members are the problematic area for me. I don't have a clue how to proceed with them with mongodb context.</p>
  
  <pre><code>public interface IRepository&lt;T&gt; where T : class
  {
      void Add(T entity);
      void Remove(T entity);
      IQueryable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
      //IQueryable&lt;T&gt; FindAll();
  
  }
  </code></pre>
  
  <p>I have a very simple model that is called Hero its drive from ImongoEntity which provides a member called accessId.</p>
  
  <pre><code>public class Hero : MongoDB.Kennedy.IMongoEntity
  {
      public ObjectId _id { get; set; }
      public string Name { get; set; }
      public string Alias { get; set; }
      public string _accessId { get; set;}
  }
  </code></pre>
  
  <p>Context class is very simple you can see the collection of Heros exposted as IQueryable property.</p>
  
  <pre><code>public class MongoDataConetext: MongoDB.Kennedy.ConcurrentDataContext
  {
      public MongoDataConetext(string databaseName, string serverName="localhost") : base(databaseName, serverName)
      {
      }
  
      public IQueryable&lt;Hero&gt; Heros {
          get {
              return base.GetCollection&lt;Hero&gt;().AsQueryable();
          }
      }
  }
  </code></pre>
  
  <p>Now in my repository most simple methods are add and remove they successfully talk to mongodb context and get entities added or deleted from mongodb. Find method however gives me compiler level errors. I need help for my find and find all method. I am using genrics because I need my repositry class for enties like hero, solution, project, user and category.</p>
  
  <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class, IMongoEntity
  {
      private readonly MongoDataConetext _ctx;
      public Repository(MongoDataConetext ctx)
      {
          _ctx = ctx;
      }
      public void Add(T entity)
      {
          _ctx.Save(entity);
  
  
      }
  
      public void Remove(T entity)
      {
          _ctx.Delete(entity);
      }
  
      public IQueryable&lt;T&gt; Find(System.Linq.Expressions.Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
      {
          _ctx.Heros.Where(predicate);
          //throw new NotImplementedException();
      }
  
      //public IQueryable&lt;T&gt; FindAll()
      //{
      //    throw new NotImplementedException();
      //}
  }
  </code></pre>