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

标签 c# asp.net 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 类非常简单,您可以看到以 IQueryable 属性公开的 Heros 集合。

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 方法给了我编译器级别的错误。我需要帮助来查找和查找所有方法。我正在使用泛型,因为我需要我的存储库类来存储英雄、解决方案、项目、用户和类别等实体。

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

最佳答案

如果您对类似于 Rob Connery 和 NBlog 存储代码但使用 mongodb csharp 驱动程序 2.0(即异步)的实现感兴趣,可以查看:

https://github.com/alexandre-spieser/mongodb-generic-repository

然后您可以编写一个继承自 BaseMongoRepository 的自定义存储库。

public interface ITestRepository : IBaseMongoRepository
{
    void DropTestCollection<TDocument>();
    void DropTestCollection<TDocument>(string partitionKey);
}

public class TestRepository : BaseMongoRepository, ITestRepository
{
    public TestRepository(string connectionString, string databaseName) : base(connectionString, databaseName)
    {
    }

    public void DropTestCollection<TDocument>()
    {
        MongoDbContext.DropCollection<TDocument>();
    }

    public void DropTestCollection<TDocument>(string partitionKey)
    {
        MongoDbContext.DropCollection<TDocument>(partitionKey);
    }
}

更新:它现在可以作为自己的 nuget 包使用:

Install-Package MongoDbGenericRepository

关于c# - 如何针对 mongodb 实现通用存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17189590/

相关文章:

c# - LINQ to Lookup 具有不同的项目和计数

c# - 瀑布结束问题。它提示回到其他对话框而不是结束

asp.net - 固定标题和顶部边框的 ASP GridView 滚动

asp.net - ASP.NET 中的 Socket.IO 服务器替代方案

asp.net - 在 web.config 中设置信任级别

ruby-on-rails - 在rails中获取带有符号的哈希作为mongo的键

C# 解析Excel自定义单元格格式

c# - .Net 中的 LDAP 目录条目 - 不适用于 OU=Users

c# - 带驱动程序 C# 的 GroupBy Mongodb

javascript - 我想使用 mongodb 的 _id 检索在特定日期插入的值