YDotNet.NoSqlRepository 2.0.0

dotnet add package YDotNet.NoSqlRepository --version 2.0.0
                    
NuGet\Install-Package YDotNet.NoSqlRepository -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="YDotNet.NoSqlRepository" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="YDotNet.NoSqlRepository" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="YDotNet.NoSqlRepository" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add YDotNet.NoSqlRepository --version 2.0.0
                    
#r "nuget: YDotNet.NoSqlRepository, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package YDotNet.NoSqlRepository@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=YDotNet.NoSqlRepository&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=YDotNet.NoSqlRepository&version=2.0.0
                    
Install as a Cake Tool

πŸŽ‰ NoSql Generic Repository Pattern (MongoDB)

πŸš€ NoSqlRepository is a super flexible and easy-to-use generic repository for MongoDB in .NET! It offers all the CRUD operations, smooth pagination, and even comes with an extension method to make your MongoDB configuration a breeze. 😎

🌟 Features

  • CRUD Operations: πŸ“ Create, Read, Update, and Delete documents with ease.
  • Pagination: πŸ§‘β€πŸ’» Seamlessly paginate your read operations to fetch data in chunks.
  • MongoDB Configuration Extension: πŸ”§ Simplify your MongoDB setup with our extension method.
  • Super Flexible: πŸ’ͺ Works with any type of entity, just like magic.

πŸ› οΈ Installation

To install NoSqlRepository, simply run the command below in your Package Manager Console:

Install-Package YDotNet.NoSqlRepository

Or, if you prefer using the .NET CLI (for that command line power πŸ’₯):

dotnet add package YDotNet.NoSqlRepository

βš™οΈ Setup

1. Add MongoDB Configuration

Add your MongoDB connection settings in appsettings.json (or appsettings.Development.json for development purposes):

{
  "MongoDbSettings": {
    "ConnectionString": "mongodb://localhost:27017",
    "DatabaseName": "YourDatabaseName"
  }
}

2. Register MongoDB Services in Program.cs

In Program.cs (or Startup.cs for the legacy folks), register MongoDB services:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using NoSqlRepository;
using MongoDB.Driver;

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        // Register MongoDb services with the extension method
        builder.Services.AddMongoDbExtensions(builder.Configuration);

        // Register the repository
        builder.Services.AddScoped(typeof(NoSqlRepository<>));

        var app = builder.Build();
        app.Run();
    }
}

πŸ”§ Methods

Here’s a list of all the cool things you can do with NoSqlRepository! 😍

GetAllAsync()

Fetches all documents from the MongoDB collection.

public async Task<IEnumerable<T>> GetAllAsync()

FindAsync(FilterDefinition<T>? filter, int skip = 0, int limit = 10)

Fetches documents based on a filter, with pagination support! πŸ“œ

public async Task<IEnumerable<T>> FindAsync(FilterDefinition<T>? filter, int skip = 0, int limit = 10)

GetByIdAsync(string id)

Fetch a document by its ID. πŸ•΅οΈβ€β™‚οΈ

public async Task<T?> GetByIdAsync(string id)

AddAsync(T entity)

Add a brand-new document into your collection! πŸŽ‰

public async Task AddAsync(T entity)

UpdateAsync(string id, T entity)

Update an existing document by its ID. πŸ”„

public async Task UpdateAsync(string id, T entity)

UpdateAsync(string id, UpdateDefinition<T> update)

Apply a partial update to a document. πŸ› οΈ

public async Task<UpdateResult> UpdateAsync(string id, UpdateDefinition<T> update)

DeleteAsync(string id)

Goodbye, document! πŸ‘‹

public async Task DeleteAsync(string id)

🌱 Example Usage

🏷️ Example Entity

Let's create a simple entity for fun!

public class YourEntity
{
    public ObjectId Id { get; set; }
    public string Name { get; set; }
}

πŸ—οΈ Creating the Repository

Make your custom repository extend the abstract NoSqlRepository<T> class. Here's how you can build your own repository for the entity:

public class YourEntityRepository : NoSqlRepository<YourEntity>
{
    public YourEntityRepository(MongoClient client, IOptions<MongoDbSettings> settings, string? collectionName = null) 
        : base(client, settings, collectionName) { }
}

πŸ› οΈ Using the Repository

Now let’s see how you can inject and use the repository in your service:

public class YourService
{
    private readonly YourEntityRepository _repository;

    public YourService(YourEntityRepository repository)
    {
        _repository = repository;
    }

    // Fetch all entities from the MongoDB collection
    public async Task GetAllEntitiesAsync()
    {
        var entities = await _repository.GetAllAsync();
        // Do something with the entities, like logging them
        Console.WriteLine(entities);
    }

    // Create a new entity and add it to the collection
    public async Task CreateNewEntityAsync()
    {
        var newEntity = new YourEntity { Name = "New Entity" };
        await _repository.AddAsync(newEntity);
    }
}

Here's the updated README with an additional usage section for the RestaurantRepository example, demonstrating how to access MongoDB collections and perform more extensive operations using the Collection instance:


🍽️ Custom Repository Example

πŸš€ Example: RestaurantRepository

If you want to do some custom queries or more advanced operations, you can easily extend NoSqlRepository and use the protected Collection instance for direct access to the MongoDB collection. Here's how you can create a custom repository for a Restaurant entity!

public class RestaurantRepository : NoSqlRepository<Restaurant>
{
    public RestaurantRepository(MongoClient client, IOptions<MongoDbSettings> settings, string? collectionName = null) 
        : base(client, settings, collectionName)
    { }

    // Custom method to fetch restaurant names with pagination
    public async Task<IEnumerable<string>> GetRestaurantsAsync()
    {
        // Define filter (Empty means no filter, so we get all restaurants)
        var filter = Builders<Restaurant>.Filter.Empty;

        // Define projection to only return restaurant names
        var projection = Builders<Restaurant>.Projection.Expression(r => r.Name);

        // Perform query with pagination: skip 0, limit to 10 results
        var restaurantNames = await Collection
            .Find(filter)
            .Project(projection)  // Apply projection to get only the 'Name' field
            .Skip(0)               // Skip the first 0 records (pagination starting point)
            .Limit(10)             // Limit to 10 records
            .ToListAsync();        // Execute the query asynchronously

        // Return the list of restaurant names
        return restaurantNames;
    }
}

πŸ”‘ How It Works

  • Custom Queries: Use the Collection property (inherited from NoSqlRepository) for any custom query operations like filtering, projections, and sorting. In this case, the GetRestaurantsAsync() method retrieves the restaurant names with pagination support.

  • Projection: The method applies a projection to only retrieve the Name field from the Restaurant documents, keeping your queries efficient by avoiding unnecessary fields.

  • Pagination: We use .Skip(0) and .Limit(10) to implement basic pagination. You can adjust the skip and limit values dynamically for more advanced pagination logic.

πŸ§‘β€πŸ³ Example Entity: Restaurant

For this example, the Restaurant entity might look like this:

public class Restaurant
{
    public ObjectId Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Cuisine { get; set; }
    public bool IsOpen { get; set; }
}

πŸ› οΈ Usage

Once your repository is ready, you can use it in your service class or anywhere you need it. Here’s how you can inject and use your custom RestaurantRepository:

public class RestaurantService
{
    private readonly RestaurantRepository _restaurantRepository;

    public RestaurantService(RestaurantRepository restaurantRepository)
    {
        _restaurantRepository = restaurantRepository;
    }

    // Example method to fetch restaurant names
    public async Task GetRestaurantNamesAsync()
    {
        var restaurantNames = await _restaurantRepository.GetRestaurantsAsync();
        // Do something with the restaurant names, like logging or processing
        Console.WriteLine(string.Join(", ", restaurantNames));
    }
}

πŸ› οΈ Configuration Options

  • ConnectionString: The URL to your MongoDB instance (e.g., mongodb://localhost:27017).
  • DatabaseName: The name of the MongoDB database you want to use. πŸ“‚

πŸ“œ License

This project is licensed under the MIT License. Enjoy and have fun with MongoDB! πŸŽ‰

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 99 4/27/2025