Myrtle.Abstractions 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Myrtle.Abstractions --version 1.0.1                
NuGet\Install-Package Myrtle.Abstractions -Version 1.0.1                
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="Myrtle.Abstractions" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Myrtle.Abstractions --version 1.0.1                
#r "nuget: Myrtle.Abstractions, 1.0.1"                
#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.
// Install Myrtle.Abstractions as a Cake Addin
#addin nuget:?package=Myrtle.Abstractions&version=1.0.1

// Install Myrtle.Abstractions as a Cake Tool
#tool nuget:?package=Myrtle.Abstractions&version=1.0.1                

Myrtle

Myrtle Logo

Myrtle is a comprehensive collection of useful extensions and configurations for the official MongoDB C# driver. It aims to simplify and enhance the experience of working with MongoDB in .NET applications.

Build Status Coverage Status

Features

  • Enhanced Configuration: Simplified setup for MongoDB with various conventions and serialization options.
  • Dependency Injection: Easy integration with Microsoft.Extensions.DependencyInjection for ASP.NET Core applications.
  • Repository Pattern: Generic repository implementation for streamlined data access.
  • Data Protection: Support for storing ASP.NET Core Data Protection keys in MongoDB.
  • Extensible Architecture: Modular design allowing for easy addition of new features and configurations.

Packages

Package Version Description
Myrtle.Abstractions NuGet Core abstractions and interfaces for Myrtle
Myrtle NuGet Main implementation of Myrtle extensions and configurations
Myrtle.Extensions.MicrosoftDependencyInjection NuGet Integration with Microsoft.Extensions.DependencyInjection
Myrtle.AspNetCore.DataProtection.Keys NuGet Support for storing ASP.NET Core Data Protection keys in MongoDB

Installation

You can install Myrtle packages via NuGet Package Manager or .NET CLI.

dotnet add package Myrtle
dotnet add package Myrtle.Extensions.MicrosoftDependencyInjection
dotnet add package Myrtle.AspNetCore.DataProtection.Keys

Key Interfaces and Abstractions

Myrtle provides several key interfaces and abstractions to simplify working with MongoDB:

  • IMongoConnection: Represents a connection to a MongoDB server.
  • IMongoDatabaseContext: Provides access to a specific MongoDB database.
  • IMongoCollectionContext<TDocument>: Represents a MongoDB collection for a specific document type.
  • IMongoRepository<TDocument, TId>: Defines a generic repository pattern for MongoDB operations.
  • IMongoConfigurationRegistry: Allows registration of custom MongoDB configurations.
  • IMongoConfiguration: Represents a specific MongoDB configuration.

Usage

Basic Setup

  1. Add MongoDB services to your application's service collection:
services.AddMongoDB(options =>
{
    options.ConnectionString = "mongodb://localhost:27017";
    options.DatabaseName = "MyDatabase";
});
  1. Configure MongoDB with custom configurations:
services.AddMongoDBConfigurations(registry =>
{
    registry.AddUtcDateTimeSerialization()
            .AddDecimalSerialization()
            .AddEnumRepresentation()
            .AddIgnoreExtraElements()
            .AddAllConfigurations(); // Adds all available configurations
});

Using the Repository Pattern

  1. Define your aggregate root class:
public class User
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}
  1. Create a custom repository by inheriting from MongoRepository<TDocument, TId>:
public class UserRepository : MongoRepository<User, Guid>, IUserRepository
{
    public UserRepository(IMongoCollectionContext<User> collectionContext) 
        : base(collectionContext)
    {
    }

    public async Task<User> FindByEmailAsync(string email)
    {
        var filter = Builders<User>.Filter.Eq(u => u.Email, email);
        return await Collection.Find(filter).FirstOrDefaultAsync();
    }
}

public interface IUserRepository : IMongoRepository<User, Guid>
{
    Task<User> FindByEmailAsync(string email);
}
  1. Register your custom repository in the dependency injection container:
services.AddScoped<IUserRepository, UserRepository>();
  1. Use the repository in your services:
public class UserService
{
    private readonly IUserRepository _userRepository;

    public UserService(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }

    public async Task<User> GetUserByIdAsync(Guid id)
    {
        return await _userRepository.GetByIdAsync(id);
    }

    public async Task<User> GetUserByEmailAsync(string email)
    {
        return await _userRepository.FindByEmailAsync(email);
    }

    public async Task AddUserAsync(User user)
    {
        await _userRepository.AddAsync(user);
    }
}

Data Protection Key Storage

To configure ASP.NET Core Data Protection to store keys in MongoDB:

services.AddDataProtection()
    .PersistKeysToMongoDb(mongoClient, "MyDatabase", "DataProtectionKeys");

License

Myrtle is licensed under the MIT License. See the LICENSE file for details.

Support

If you encounter any issues or have questions, please open an issue on GitHub.

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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Myrtle.Abstractions:

Package Downloads
Myrtle

Core implementation of Myrtle, providing useful extensions and configurations for the official MongoDB C# driver.

Myrtle.Extensions.MicrosoftDependencyInjection

Provides integration of Myrtle with Microsoft.Extensions.DependencyInjection for easy configuration and usage in ASP.NET Core applications.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 34 7/13/2024
1.0.2 32 7/12/2024
1.0.1 34 7/12/2024
1.0.0 23 7/12/2024

v1.0.1 (2024-07-12)
⦁ Added `IMongoDatabase` and `IMongoClient` to the service collection
⦁ Removed redundant extension method from `MongoConfigurationRegistryExtensions`
⦁ Add the README to nuget packages
v1.0.0 (2024-07-12)
⦁ Added comprehensive MongoDB configuration options
⦁ Introduced IMongoRepository<TDocument, TId> for flexible repository pattern implementation
⦁ Enhanced integration with Microsoft.Extensions.DependencyInjection
⦁ Improved documentation and README
⦁ Added source link support for better debugging experience
⦁ Centralized common properties in Directory.Build.props
v0.2.0
⦁ Myrtle.AspNetCore.DataProtection.Keys
⦁ Add two PersistKeysToMongoDb overloads to support getting mongo collection and mongo database using IServiceProvider
v0.1.0
⦁ Add Myrtle.AspNetCore.DataProtection.Keys