Skip to content

Repository pattern based on offical MongoDB.Driver for .NET

Notifications You must be signed in to change notification settings

msykutera/MongoDB.Repository

Repository files navigation

MongoDB.Repository

Repository pattern based on offical MongoDB.Driver for .NET

It is published as Nuget package MongoDB.Driver.Repository.

Project MongoDB.Repository.Example contains simple example of how to use this library in .NET Core web projects.

Bascially to get it working:

  1. Add two lines below to ConfigureServices method in Startup.cs
    public void ConfigureServices(IServiceCollection services)
    {
        // ...
        services.AddSingleton(x => Configuration.GetSection("MongoDb").Get<MongoDbSettings>());
        services.AddScoped<IMongoDbContext, MongoDbContext>();
    }
  1. Add section below to main configuration object in appsettings.json
  "MongoDb": {
    "ConnectionString": "mongodb://localhost:27017",
    "DatabaseName": "example"
  },
  1. Then you can register repositories in ConfigureServices method in Startup.cs
    public void ConfigureServices(IServiceCollection services)
    {
        // ...
        services.AddScoped<IRepository<Category>, BaseRepository<Category>>();
    }
  1. Now you can inject repositories into services and controllers
    private readonly IRepository<Category> _categoryRepository;

    public CategoriesController(IRepository<Category> categoryRepository)
    {
        _categoryRepository = categoryRepository;
    }
     // ...

About

Repository pattern based on offical MongoDB.Driver for .NET

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages