Skip to content

MongoDriver.Facade makes it easy for you to work with MongoDb through an interface named MongoDbContext.

License

Notifications You must be signed in to change notification settings

amirhosseinsaloot/mongo-driver-facade

Repository files navigation

mongo-driver-facade

.NET

What is the MongoDriver.Facade?

In short MongoDriver.Facade implements facade pattern on the MongoDb C# driver.

Actually MongoDriver.Facade makes it easy for you to work with MongoDb through an interface named MongoDbContext. MongoDbContext manages the client connection to database. Also, it automatically creates the database and collections (with their configuration) based on your models.

Getting Started

1. Install Package:

Install the standard Nuget package into your ASP.NET Core application.

Via package manager:

Install-Package MongoDriver.Facade -Version 1.0.3

Via .NET CLI:

dotnet add package MongoDriver.Facade --version 1.0.3

Via Paket CLI:

paket add MongoDriver.Facade --version 1.0.3

2. appsettings.json

Add MongoSettings section to appsettings.json:

{
  "MongoSettings": {
    "ConnectionString": "mongodb://localhost:27017",
    "Database": "TestDb"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

3. Models and configurations

You should create your own models like this:

using MongoDB.Driver;
using MongoDriver.Facade;

namespace Test
{
    // Your collection
    public class Book : MongoDbCollection
    {
        public string Name { get; set; }

        public decimal Price { get; set; }

        public string Category { get; set; }

        public string Author { get; set; }
    }

    // Optional
    // Applying configuration for the Book collection type.
    // If you don't create this, default collection configuration will be applied.
    public class BookConfigration : ICollectionConfiguration<Book>
    {
        public CreateCollectionOptions Configure()
        {
            return new CreateCollectionOptions { NoPadding = true };
        }
    }
}

4. MongoDbContext

You can use DbContext like this:

using MongoDB.Driver;
using MongoDriver.Facade;
using MongoDriver.Facade.TestApi.Models;
using System.Collections.Generic;

namespace TestApi.Services
{
    public class BookService
    {
        private readonly MongoDbContext _dbContext;

        public BookService(MongoDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public List<Book> Get()
        {
            return _dbContext.Set<Book>().Find(book => true).ToList();
        }

        public Book Get(string id)
        {
            return _dbContext.Set<Book>().Find(book => book.Id == id).FirstOrDefault();
        }

        public Book Create(Book book)
        {
            _dbContext.Set<Book>().InsertOne(book);
            return book;
        }

        public void Update(string id, Book bookIn)
        {
            _dbContext.Set<Book>().ReplaceOne(book => book.Id == id, bookIn);
        }

        public void Remove(Book bookIn)
        {
            _dbContext.Set<Book>().DeleteOne(book => book.Id == bookIn.Id);
        }

        public void Remove(string id)
        {
            _dbContext.Set<Book>().DeleteOne(book => book.Id == id);
        }
    }
}

Customize DbContext

Also, you can create a custom class that inherited from MongoDbContext then add your customized members:

 public class ApplicationDbContext : MongoDbContext
    {
        public ApplicationDbContext(IConfiguration configuration) : base(configuration)
        {
        }

        public async Task DropDatabase(string database)
        {
            await Client.DropDatabaseAsync(database);
        }
    }

Register MongoDriver.Facade

using MongoDriver.Facade;
services.AddMongoDbContext();

If there is customized DbContext :

services.AddMongoDbContext<ApplicationDbContext>();

Give a Star! ⭐

If you liked the repo or if it helped you, a star would be appreciated.

About

MongoDriver.Facade makes it easy for you to work with MongoDb through an interface named MongoDbContext.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages