c# – 如何针对mongodb实现通用存储库?

我是mongodb的新手,我正在为一个新项目开发mvc4 web应用程序.
我想使用将与mongodb上下文进行数据库级通信的存储库模式.
我与Entity Framework 4.0一起使用的简单界面如下.发现成员对我来说是个问题.我不知道如何使用mongodb上下文继续它们.
public interface IRepository<T> where T : class
{
void Add(T entity);
void Remove(T entity);
IQueryable<T> Find(Expression<Func<T, bool>> predicate);
//IQueryable<T> FindAll();
}
我有一个非常简单的模型叫做Hero,它的驱动程序来自ImongoEntity,它提供了一个名为accessId的成员.
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;}
}
Context类很简单,你可以看到Heros的集合被公开为IQueryable属性.
public class MongoDataConetext: MongoDB.Kennedy.ConcurrentDataContext
{
public MongoDataConetext(string databaseName, string serverName="localhost") : base(databaseName, serverName)
{
}
public IQueryable<Hero> Heros {
get {
return base.GetCollection<Hero>().AsQueryable();
}
}
}
现在在我的存储库中,大多数简单的方法都是添加和删除它们成功地与mongodb上下文对话并从mongodb中添加或删除实体.然而,Find方法给出了编译器级错误.我的发现需要帮助,找到所有方法.我正在使用genrics,因为我需要我的repositry类,用于英雄,解决方案,项目,用户和类别.
public class Repository<T> : IRepository<T> 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<T> Find(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
_ctx.Heros.Where(predicate);
//throw new NotImplementedException();
}
//public IQueryable<T> FindAll()
//{
// throw new NotImplementedException();
/