Skip to content

Instantly share code, notes, and snippets.

@mizrael
Last active July 17, 2020 12:14
Show Gist options
  • Save mizrael/47b601b0a09d4b6f03f6 to your computer and use it in GitHub Desktop.
Save mizrael/47b601b0a09d4b6f03f6 to your computer and use it in GitHub Desktop.
simple mongodb repository implementation
public class Repository<TEntity> : IRepository<TEntity>
{
private readonly IMongoCollection<TEntity> _collection;
public Repository(IMongoCollection<TEntity> collection)
{
if (null == collection)
throw new ArgumentNullException("collection");
_collection = collection;
this.CollectionName = collection.CollectionNamespace.CollectionName;
}
public string CollectionName { get; private set; }
public Task<long> CountAsync(FilterDefinition<TEntity> filter)
{
return _collection.CountAsync(filter);
}
public IFindFluent<TEntity, TEntity> Find(FilterDefinition<TEntity> filter)
{
return _collection.Find(filter);
}
public IFindFluent<TEntity, TEntity> Find(Expression<Func<TEntity, bool>> filter)
{
return _collection.Find(filter);
}
public Task<TEntity> FindOneAndReplaceAsync(FilterDefinition<TEntity> filter, TEntity replacement)
{
return _collection.FindOneAndReplaceAsync(filter, replacement);
}
public Task<TEntity> FindOneAndReplaceAsync(Expression<Func<TEntity, bool>> filter, TEntity replacement)
{
return _collection.FindOneAndReplaceAsync(filter, replacement);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment