Class MongoDBRepository<T>
Abstract base class for MongoDB repositories. GlitchedPolygons.RepositoryPattern.IRepository<T1, T2>
Inheritance
Implements
Inherited Members
Namespace: GlitchedPolygons.RepositoryPattern.MongoDB
Assembly: GlitchedPolygons.RepositoryPattern.MongoDB.dll
Syntax
public abstract class MongoDBRepository<T> : IRepository<T, ObjectId> where T : IEntity<ObjectId>
Type Parameters
Name | Description |
---|---|
T | The type of entity that this repository will store. |
Constructors
| Improve this Doc View SourceMongoDBRepository(IMongoDatabase, String)
Creates a new MongoDB repository.
Declaration
protected MongoDBRepository(IMongoDatabase db, string collectionName = null)
Parameters
Type | Name | Description |
---|---|---|
MongoDB.Driver.IMongoDatabase | db | The Mongo database of which you want to create a repository. |
System.String | collectionName | Optional custom name for the underlying MongoDB collection. If left out, the entity's name is used. |
Fields
| Improve this Doc View Sourcecollection
The underlying MongoDB collection.
Declaration
protected readonly IMongoCollection<T> collection
Field Value
Type | Description |
---|---|
MongoDB.Driver.IMongoCollection<T> |
db
The underlying Mongo database.
Declaration
protected readonly IMongoDatabase db
Field Value
Type | Description |
---|---|
MongoDB.Driver.IMongoDatabase |
Properties
| Improve this Doc View SourceCollectionName
The name of the repository's underlying MongoDB collection.
Declaration
public string CollectionName { get; }
Property Value
Type | Description |
---|---|
System.String |
Item[ObjectId]
Synchronously gets an entity by its unique identifier.
Declaration
public T this[ObjectId id] { get; }
Parameters
Type | Name | Description |
---|---|---|
MongoDB.Bson.ObjectId | id | The entity's unique identifier. |
Property Value
Type | Description |
---|---|
T | The found entity; |
Methods
| Improve this Doc View SourceAdd(T)
Adds the specified entity to the data repository.
Declaration
public Task<bool> Add(T entity)
Parameters
Type | Name | Description |
---|---|---|
T | entity | The entity to add. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Boolean> | Whether the entity could be added successfully or not. |
AddRange(IEnumerable<T>)
Adds multiple entities at once.
Declaration
public Task<bool> AddRange(IEnumerable<T> entities)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | entities | The entities to add. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Boolean> | Whether the entities were added successfully or not. |
Find(Expression<Func<T, Boolean>>)
Finds all entities according to the specified predicate System.Linq.Expressions.Expression.
Declaration
public Task<IEnumerable<T>> Find(Expression<Func<T, bool>> predicate)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> | predicate | The search predicate (all entities that match the provided conditions will be added to the query's result). |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<T>> | The found entities (System.Collections.Generic.IEnumerable<T>). |
Get(ObjectId)
Gets an entity by its unique identifier.
Declaration
public Task<T> Get(ObjectId id)
Parameters
Type | Name | Description |
---|---|---|
MongoDB.Bson.ObjectId | id | The entity's unique identifier. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<T> | The first found GlitchedPolygons.RepositoryPattern.IEntity`1; |
GetAll()
Gets all entities from the repository.
Declaration
public Task<IEnumerable<T>> GetAll()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<T>> | All entities inside the repo. |
Remove(T)
Removes the specified entity.
Declaration
public Task<bool> Remove(T entity)
Parameters
Type | Name | Description |
---|---|---|
T | entity | The entity to remove. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Boolean> | Whether the entity could be removed successfully or not. |
Remove(ObjectId)
Removes the specified entity.
Declaration
public Task<bool> Remove(ObjectId id)
Parameters
Type | Name | Description |
---|---|---|
MongoDB.Bson.ObjectId | id | The unique id of the entity to remove. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Boolean> | Whether the entity could be removed successfully or not. |
RemoveAll()
Removes all entities at once from the repository.
Declaration
public Task<bool> RemoveAll()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Boolean> | Whether the entities were removed successfully or not. If the repository was already empty, |
Exceptions
Type | Condition |
---|---|
System.NotImplementedException |
RemoveRange(IEnumerable<T>)
Removes the range of entities from the repository.
Declaration
public Task<bool> RemoveRange(IEnumerable<T> entities)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | entities | The entities to remove. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Boolean> | Whether the entities were removed successfully or not. |
RemoveRange(IEnumerable<ObjectId>)
Removes the range of entities from the repository.
Declaration
public Task<bool> RemoveRange(IEnumerable<ObjectId> ids)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<MongoDB.Bson.ObjectId> | ids | The unique ids of the entities to remove. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Boolean> | Whether all entities were removed successfully or not. |
RemoveRange(Expression<Func<T, Boolean>>)
Removes all entities that match the specified conditions (via the predicate System.Linq.Expressions.Expression parameter).
Declaration
public Task<bool> RemoveRange(Expression<Func<T, bool>> predicate)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> | predicate | The predicate System.Linq.Expressions.Expression that defines which entities should be removed. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Boolean> | Whether the entities were removed successfully or not. |
SingleOrDefault(Expression<Func<T, Boolean>>)
Gets a single entity from the repo according to the specified predicate condition.
If 0 or >1 entities are found, null
is returned.
Declaration
public Task<T> SingleOrDefault(Expression<Func<T, bool>> predicate)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> | predicate | The search predicate. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<T> | Single found entity; |
Update(T)
Updates the specified entity.
Declaration
public abstract Task<bool> Update(T entity)
Parameters
Type | Name | Description |
---|---|---|
T | entity | The entity to update. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Boolean> | Whether the entity could be updated successfully or not. |