An open API service indexing awesome lists of open source software.

https://github.com/dumiensky/mongodb.wrapper

A handy CRUD wrapper with soft deletion over MongoDB.Driver.MongoClient
https://github.com/dumiensky/mongodb.wrapper

crud mongodb mongodb-database soft-delete wrapper

Last synced: 11 months ago
JSON representation

A handy CRUD wrapper with soft deletion over MongoDB.Driver.MongoClient

Awesome Lists containing this project

README

          

## MongoDB.Wrapper

A handy CRUD wrapper with soft deletion over MongoDB.Driver.**MongoClient**.

This wrapper is designed with _less-code-is-better_ in mind. It uses code-first aproach, just install the nuget and start working on the database!

[Usage](https://github.com/dumiensky/MongoDB.Wrapper#usage) | [Examples](https://github.com/dumiensky/MongoDB.Wrapper#examples) | [List of all methods](https://github.com/dumiensky/MongoDB.Wrapper#list-of-all-methods-exposed-by-the-wrapper)

## Installation
Install [NuGet package **MongoDB.Wrapper**](https://www.nuget.org/packages/MongoDB.Wrapper/)

#### dotnet cli:
`dotnet add package MongoDB.Wrapper`

#### .csproj edit:
```

```

## Usage

#### Dependency Injection

`MongoDB.Wrapper.MongoDb` requires a `MongoDB.Wrapper.Settings.MongoDbSettings` settings instance to work

>_Examples here use **ASP .Net Core DI container** and **appsettings.json** to store the settings. Wrapper is registered as a singleton, because [that's what the docs recommend](http://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/connecting/#re-use)_

```javascript
// appsettings.json
{
"MongoDbSettings": {
"ConnectionString": "",
"DatabaseName": ""
}
}
```

```csharp
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// get the settings from appsettings.json
var mongoDbSettings = Configuration.GetSection(nameof(MongoDbSettings)).Get();

// register the settings and the wrapper
services.AddSingleton(mongoDbSettings);
services.AddSingleton();

// OR

// register only the instance of the wrapper
services.AddSingleton(new MongoDb(mongoDbSettings));
}
```

#### Normal class instantiation

```csharp
var mongoDbSettings = new MongoDbSettings
{
ConnectionString = "",
DatabaseName = ""
};

var mongoDb = new MongoDb(mongoDbSettings);
```

## Examples

The wrapper will work on any entity that derive from `MongoDB.Wrapper.Entity` or implement `MongoDB.Wrapper.Abstractions.IEntity`
```csharp
public class Person : Entity
{
public string Name { get; set; }
public int Age { get; set; }
}

var person = new Person
{
Name = "Steve",
Age = 21
};
```

Every method exposed by the wrapper interface (except for `Query<>`) is asynchronous, so the _-Async_ suffix is omitted.

#### Add an entity to the database
```csharp
// assigns a new Id to entity and adds it to collection of type TEntity
await mongoDb.Add(person);

// id can be accessed straight from the object
Guid assignedId = person.Id;
```

#### Access the entities
```csharp
// get list of all persons in the database
var persons = await mongoDb.Get();

// get single entity by its' Id
var person = await mongoDb.Get(personId);

// get all entities with age of 10
var persons = await mongoDb.Get(p => p.Age == 10);

// get all entites with name Frank, older than 18, including the deleted ones
var persons = await mongoDb.Get(p => p.Name == "Frank" && p.Age > 18, includeDeleted: true);
```

#### Update the entities
```csharp
person.Name = "Frank";

// replace the entity by its' Id
await mongoDb.Replace(person);
```

#### List of all methods exposed by the wrapper

Returned type | Method | Description
------------- | ------ | -----------
`IMongoQueryable` | **Query<>(includeDeleted)**
`Query(bool includeDeleted = false)` | Returns an open query to operate on
`Task` | **Add(entity)**
`Add(TEntity)` | Assigns a new Id to entity and adds it to collection of type TEntity
`Task` | **Any(includeDeleted)**
`Any(bool includeDeleted = false)` | Returns whether the collection of type TEntity contains entites
`Task` | **Any(predicate, includeDeleted)**
`Any(Expression> predicate, bool includeDeleted = false)` | Returns whether the collection of type TEntity contains entites matching the predicate
`Task` | **Count(includeDeleted)**
`Count(bool includeDeleted = false)` | Returns the count of entites in the collection of type TEntity
`Task` | **Count(predicate, includeDeleted)**
`Count(Expression> predicate, bool includeDeleted = false)` | Returns the count of entites in the collection of type TEntity that match given predicate
`Task` | **Get(id)**
`Get(Guid id)` | Returns entity of type TEntity, which Id equals passed Guid. Default if not found
`Task>` | **Get(includeDeleted)**
`Get(bool includeDeleted = false)` | Returns list of entities from collection of type TEntity
`Task>` | **Get(predicate, includeDeleted)**
`Get(Expression> predicate, bool includeDeleted = false)` | Returns list of entities from collection of type TEntity, that match the predicate
`Task` | **FirstOrDefault(predicate, includeDeleted)**
`FirstOrDefault(Expression> predicate, bool includeDeleted = false)` | Returns the first entity from collection of type TEntity, that matches the predicate
`Task` | **SingleOrDefault(predicate, includeDeleted)**
`SingleOrDefault(Expression> predicate, bool includeDeleted = false)` | Returns the entity from collection of type TEntity, that matches the predicate. If more than one entity matches the predicate, an exception is thrown
`Task` | **Replace(entity)**
`Replace(TEntity entity)` | Replaces the object of type TEntity with passed object, compared by IEntity.Id. If IEntity.Id equals default, entity is added. When entity with given id is not found, an exception is thrown.
`Task` | **Delete(id)**
`Delete(Guid id)` | Soft deletes (sets Deleted flag) on entity with given id. Throws an exception if the entity doesn't exist.
`Task` | **Restore(id)**
`Restore(Guid id)` | Reverts a soft delete (un-sets Deleted flag) of an entity with given id. Throws an exception if the entity doesn't exist.
`Task` | **DeleteHard**
`DeleteHard(Guid id)` | Deleted an entity with given id from the database. This is irreversible. Throws an exception if the entity doesn't exist.

## Roadmap

1. `Update()` method that will update only given properties of an entity

**Feel free to create an Issue or a Pull Request**