I have a question about MongoDB extension methods -
I want to import both MongoDB.Driver.Linq
and System.Linq
in the same namespace, where I am creating a Repository access layer, which will be used by a service layer in my API.
This allows me to do something like:
private readonly IMongoCollection _collection;
public Repository(IMongoDatabase database)
{
_collection = database.GetCollection("SomeCollection");
}
// Method 1
public IQueryable<TEntity> GetByCondition(Expression<Func<TEntity, bool>> expression)
{
return _collection.AsQueryable().Where(expression);
}
// Method 2
public async Task<List<T>> GetByConditionAsync(Expression<Func<T, bool>> expression)
{
return await _collection.AsQueryable().Where(expression).ToListAsync();
}
The problem is that I need System.Linq
to allow Method 1 to return an IQueryable
object (therefore my service layer doesn't know the MongoDB implementation detail). Conversely, in Method 2 , I need MongoDB.Driver.Linq
to use the .Where()
method to return an IMongoQueryable
object and therefore use the .ToListAsync
extension method.
Is having both of these in the namespace likely to cause issues? I have done a bit of reading that suggests it might!
No, because the lambda signature between both methods are differents. There aren't any conflict between those extensions methods.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.