MongoDB C# 使用 Find 实现 GetById

5
public abstract class GenericRepository<T> : IDisposable, IGenericRepository<T> where T : class
{
    protected SphereTripMongoDbContext SphereTripMongoDbContext;
    public IMongoCollection<T> MongoCollection { get; set; }
    protected GenericRepository(SphereTripMongoDbContext sphereTripMongoDbContext)
    {
        SphereTripMongoDbContext = sphereTripMongoDbContext;
        MongoCollection =
            SphereTripMongoDbContext.MongoDatabase.GetCollection<T>(typeof(T).Name);
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public T GetById(string id)
    {
        var entity = MongoCollection**.Find(t => t.Id == id)**;
        return entity;
    }
}

我正在尝试编写一个MongoDb的通用抽象存储库类。由于在基类中使用了泛型类型,当我使用Find方法查找文档时,“Id”不可见。不确定如何解决这个问题。
任何帮助都将不胜感激。
2个回答

11
您可以使用 Builders 而无需使用带有类型化的 lambda 表达式的 Find
 var item = await collection
    .Find(Builders<ItemClass>.Filter.Eq("_id", id))
    .FirstOrDefaultAsync();

然而,更为稳健的解决方案是使用一些接口来获取所需的内容(即ID),并确保GenericRepository仅适用于这些类型:

interface IIdentifiable
{
    string Id { get; }
}

class GenericRepository <T> : ... where T : IIdentifiable
{
    // ...
}

1
@i3amon 不清楚如何将强大的解决方案应用于我的情况? - Joshua
2
@Joshua 如果泛型参数实现了该接口,则它具有一个ID属性,您可以使用它来构建过滤器。 - i3arnon

0
我建立了一个像这样的方法:
public ValueTask<T> GetAsync<T>(IQueryable<T> source, object[] keyValues, CancellationToken cancellationToken = default)
    where T : class
{
    if (source == null)
    {
        throw new ArgumentNullException(nameof(source));
    }

    if (keyValues == default)
    {
        throw new ArgumentNullException(nameof(keyValues));
    }

    if (keyValues.Length != 1)
    {
        throw new ArgumentException("Key values must contain exactly one key value", nameof(keyValues));
    }

    var type = typeof(T);
    var classMap = BsonClassMap.LookupClassMap(type);
    if (classMap == default)
    {
        throw new InvalidOperationException($"Class map not found for '{type.Name}'");
    }

    var id = classMap.IdMemberMap;
    if (id == default)
    {
        throw new InvalidOperationException($"Id member not found for '{type.Name}'");
    }

    var filter = Builders<T>.Filter.Eq(id.ElementName, keyValues[0]);
    var collection = Database.GetCollection<T>(type.Name);

    async ValueTask<T> GetAsync()
    {
        var cursor = await collection.FindAsync<T>(filter, default, cancellationToken).ConfigureAwait(false);
        return await cursor.SingleOrDefaultAsync(cancellationToken).ConfigureAwait(false);
    }

    return GetAsync();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接