laget.Db.Mongo 1.2.29

Prefix Reserved
Additional Details

This package has at least one vulnerability with high severity. It may lead to specific problems in your project. Try updating the package version.

https://github.com/advisories/GHSA-7j9m-j397-g4wx

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

// Install laget.Db.Mongo as a Cake Tool
#tool nuget:?package=laget.Db.Mongo&version=1.2.29                

laget.Db.Mongo

A repository pattern implementation for MongoDB, a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.

Nuget Nuget

Configuration

This example is shown using Autofac since this is the go-to IoC for us.

public class DatabaseModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.Register(c => new MongoDefaultProvider(c.Resolve<IConfiguration>().GetConnectionString("MongoConnectionString"))).As<IMongoDefaultProvider>().SingleInstance();
    }
}
public class DatabaseModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.Register(c => new MongoDefaultProvider(c.Resolve<IConfiguration>().GetConnectionString("MongoConnectionString"), new MongoDatabaseSettings
            {
                ReadConcern = ReadConcern.Default,
                ReadPreference = ReadPreference.SecondaryPreferred,
                WriteConcern = WriteConcern.W3
            })).As<IMongoDefaultProvider>().SingleInstance();
    }
}
public class DatabaseModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.Register(c => new MongoDefaultProvider(c.Resolve<IConfiguration>().GetConnectionString("MongoConnectionString"), new MemoryCacheOptions
            {
                CompactionPercentage = 0.25,
                ExpirationScanFrequency = TimeSpan.FromMinutes(5),
                SizeLimit = 1024
            })).As<IMongoDefaultProvider>().SingleInstance();
    }
}
public class DatabaseModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.Register(c => new MongoDefaultProvider(c.Resolve<IConfiguration>().GetConnectionString("MongoConnectionString"), new MongoDatabaseSettings
            {
                ReadConcern = ReadConcern.Default,
                ReadPreference = ReadPreference.SecondaryPreferred,
                WriteConcern = WriteConcern.W3
            }, new MemoryCacheOptions
            {
                CompactionPercentage = 0.25,
                ExpirationScanFrequency = TimeSpan.FromMinutes(5),
                SizeLimit = 1024
            })).As<IMongoDefaultProvider>().SingleInstance();
    }
}

Usage

Built-in methods
public interface IRepository<TEntity> where TEntity : Entity
{
    IEnumerable<TEntity> Find(FilterDefinition<TEntity> filter);
    Task<IEnumerable<TEntity>> FindAsync(FilterDefinition<TEntity> filter);

    TEntity Get(string id);
    Task<TEntity> GetAsync(string id);
    TEntity Get(FilterDefinition<TEntity> filter);
    Task<TEntity> GetAsync(FilterDefinition<TEntity> filter);

    void Insert(IEnumerable<TEntity> entities);
    Task InsertAsync(IEnumerable<TEntity> entities);
    void Insert(TEntity entity);
    Task InsertAsync(TEntity entity);

    void Update(FilterDefinition<TEntity> filter, UpdateDefinition<TEntity> update, UpdateOptions options);
    Task UpdateAsync(FilterDefinition<TEntity> filter, UpdateDefinition<TEntity> update, UpdateOptions options);

    void Upsert(IEnumerable<TEntity> entities);
    Task UpsertAsync(IEnumerable<TEntity> entities);
    void Upsert(TEntity entity);
    Task UpsertAsync(TEntity entity);
    void Upsert(FilterDefinition<TEntity> filter, TEntity entity);
    Task UpsertAsync(FilterDefinition<TEntity> filter, TEntity entity);

    void Delete(IEnumerable<TEntity> entities);
    Task DeleteAsync(IEnumerable<TEntity> entities);
    void Delete(TEntity entity);
    Task DeleteAsync(TEntity entity);
    void Delete(FilterDefinition<TEntity> filter);
    Task DeleteAsync(FilterDefinition<TEntity> filter);
}

Generic

public interface IUserRepository : IRepository<Models.User>
{
}

public class UserRepository : Repository<Models.User>, IUserRepository
{
    public UserRepository(string connectionString)
        : base(connectionString)
    {
    }
}

Caching

public interface IUserRepository : IRepository<Models.User>
{
}

public class UserRepository : Repository<Models.User>, IUserRepository
{
    public UserRepository(string connectionString)
        : base(connectionString)
    {
    }

    public override Models.User Get(string id)
    {
        var cacheKey = "_Id_" + id;
        var item = CacheGet<Models.User>(cacheKey);

        if (item != null)
            return item;

        var result = Get(id);

        CacheAdd(cacheKey, result);

        return result;
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.30.39 7 11/4/2024
1.3.37 109 9/3/2024
1.3.36 484 3/4/2024
1.3.35 349 1/20/2024
1.3.33 293 12/21/2023
1.2.32 307 11/22/2023 1.2.32 is deprecated because it has critical bugs.
1.2.31 673 7/3/2023 1.2.31 is deprecated because it has critical bugs.
1.2.29 259 3/10/2023 1.2.29 is deprecated because it has critical bugs.
1.1.27 261 3/6/2023 1.1.27 is deprecated because it has critical bugs.
1.1.25 322 2/6/2023 1.1.25 is deprecated because it has critical bugs.
1.1.22 325 12/5/2022 1.1.22 is deprecated because it has critical bugs.
1.1.21 388 10/24/2022 1.1.21 is deprecated because it has critical bugs.
1.1.17 417 8/22/2022 1.1.17 is deprecated because it has critical bugs.
1.1.16 478 7/4/2022 1.1.16 is deprecated because it has critical bugs.
1.1.15 434 6/7/2022 1.1.15 is deprecated because it has critical bugs.
1.1.14 460 4/4/2022 1.1.14 is deprecated because it has critical bugs.
1.1.13 424 4/4/2022 1.1.13 is deprecated because it has critical bugs.
1.1.12 445 3/10/2022 1.1.12 is deprecated because it has critical bugs.
1.1.11 473 2/7/2022 1.1.11 is deprecated because it has critical bugs.
1.1.10 294 12/21/2021 1.1.10 is deprecated because it has critical bugs.
1.1.9 313 12/6/2021 1.1.9 is deprecated because it has critical bugs.
1.1.8 284 11/28/2021 1.1.8 is deprecated because it has critical bugs.
1.1.7 369 11/1/2021 1.1.7 is deprecated because it has critical bugs.
1.1.6 416 11/1/2021 1.1.6 is deprecated because it has critical bugs.
1.1.5 393 10/4/2021 1.1.5 is deprecated because it has critical bugs.
1.1.4 445 8/16/2021 1.1.4 is deprecated because it has critical bugs.
1.1.3 390 8/9/2021 1.1.3 is deprecated because it has critical bugs.
1.1.2 493 6/7/2021 1.1.2 is deprecated because it has critical bugs.
1.1.1 519 5/21/2021 1.1.1 is deprecated because it has critical bugs.
1.0.25 439 5/5/2021 1.0.25 is deprecated because it has critical bugs.
1.0.24 429 5/3/2021 1.0.24 is deprecated because it has critical bugs.
1.0.23 476 4/6/2021 1.0.23 is deprecated because it has critical bugs.
1.0.22 452 3/5/2021 1.0.22 is deprecated because it has critical bugs.
1.0.21 436 3/5/2021 1.0.21 is deprecated because it has critical bugs.
1.0.20 452 2/11/2021 1.0.20 is deprecated because it has critical bugs.
1.0.19 390 2/1/2021 1.0.19 is deprecated because it has critical bugs.
1.0.18 638 1/8/2021 1.0.18 is deprecated because it has critical bugs.
1.0.17 481 1/1/2021 1.0.17 is deprecated because it has critical bugs.
1.0.16 437 12/23/2020 1.0.16 is deprecated because it has critical bugs.
1.0.15 462 12/22/2020 1.0.15 is deprecated because it has critical bugs.
1.0.14 582 12/22/2020 1.0.14 is deprecated because it has critical bugs.
1.0.13 672 12/16/2020 1.0.13 is deprecated because it has critical bugs.
1.0.12 541 12/9/2020 1.0.12 is deprecated because it has critical bugs.
1.0.11 429 12/9/2020 1.0.11 is deprecated because it has critical bugs.
1.0.10 440 12/9/2020 1.0.10 is deprecated because it has critical bugs.
1.0.9 508 12/8/2020 1.0.9 is deprecated because it has critical bugs.
1.0.8 515 12/8/2020 1.0.8 is deprecated because it has critical bugs.
1.0.7 514 12/8/2020 1.0.7 is deprecated because it has critical bugs.
1.0.2 516 12/8/2020 1.0.2 is deprecated because it has critical bugs.