- Generic Repository Pattern: Implements a generic repository using the MongoDB.Driver to simplify data access patterns in .NET applications. - Seamless Dependency Injection: Integrates effortlessly with Microsoft.Extensions.DependencyInjection for automatic service provisioning. - Attribute-Based Configuration: Offers custom attributes for straightforward management of databases and collections, tailored to each connection.


Keywords
mongo, mongodb, nosql, repository
License
MIT
Install
Install-Package MongoGogo -Version 5.1.1

Documentation

MongoGogo

Quickly integrate MongoDB into your .NET projects with MongoGogo, a lightweight and powerful ORM designed to elevate your data layer with minimal effort.

Why MongoGogo?

  • Simplify MongoDB Interactions: Utilize generic repository patterns to interact with MongoDB collections using .NET-friendly abstractions.

  • Seamless Integration: Built to fit naturally into the ASP.NET Core framework, supporting dependency injection out of the box.

  • Efficient Data Management: Perform synchronous and asynchronous CRUD operations with ease, thanks to a clear and fluent API.

  • Lambda Expressions: Use the elegance of lambda expressions for more readable and maintainable update and delete operations.

  • Flexible Querying: Leverage the power of LINQ to query MongoDB documents directly in C#.

  • Bulk and Transaction Support: Manage large datasets with bulk operations and maintain data integrity with transaction support.

Quick Start

Implement MongoGogo in your .NET project to enhance your MongoDB operations with minimal configuration.

Installation

dotnet add package MongoGogo

Setup

  1. Define your POCO data models. Use attributes to link them to MongoDB collections.
// Example of a user-defined POCO linked to a MongoDB collection
[MongoCollection(fromDatabase: typeof(MyContext.StudentDb), collectionName: "students")]
public class Student
{
    [BsonId]
    public string Id { get; set; }
    
    public string Name { get; set; }
    public int Age { get; set; }
    // Additional properties can be added as required
}

MyContext is a placeholder for your custom context class, which manages database configurations like connection strings and database names. StudentDb represents the specific configuration for the Student collection within that context.

  1. Implement your custom context class for managing database configurations.
// Define your custom context class inheriting from GoContext
public class MyContext : GoContext<MyContext>
{
    [MongoDatabase]
    public class StudentDb { }

    public MyContext(string connectionString) : base(connectionString)
    {
    }
}

MyContext manages database configurations like connection strings and database names. StudentDb represents the specific configuration for the Student collection within that context.

  1. Configure MongoGogo in your Program.cs:
var builder = WebApplication.CreateBuilder(args);

// Register your custom context class with MongoGogo
builder.Services.AddMongoContext(new MyContext("your-mongodb-connection-string"));

// Continue setting up your application...

Replace MyContext with the name of your context class and "your-mongodb-connection-string" with your actual MongoDB connection string.

  1. For more details on model mapping and attribute configuration, see the setup your models section in our full documentation.

Dependency Injection Setup

Once MongoGogo is configured in your Program.cs, you can inject IGoCollection<T> instances into your classes.

For example, in an ASP.NET Core controller:

public class StudentsController : ControllerBase
{
    private readonly IGoCollection<Student> _studentCollections;

    public StudentsController(IGoCollection<Student> studentCollections)
    {
        _studentCollections = studentCollections;
    }

    // ... CRUD operations using _students
}

Here, StudentsController is an example of a user-created ASP.NET Core controller.

Basic CRUD Operations

MongoGogo simplifies CRUD operations with IGoCollection<T>:

Transactional Support and Bulk Operations

MongoGogo provides robust support for more advanced database operations:

  • Transactional Support: Process multiple operations as a single unit of work to maintain data integrity.
  • Bulk Operations: Handle large numbers of operations in batches for efficiency.

Learn more about bulk operations and transactions section.

Support

For detailed guidelines on configuration and mapping, review our full documentation.

For assistance, reach out at r05221017@gmail.com.