TK.MongoDB.GridFS.Repository 2.1.1

There is a newer version of this package available.
See the version list below for details.
Install-Package TK.MongoDB.GridFS.Repository -Version 2.1.1
dotnet add package TK.MongoDB.GridFS.Repository --version 2.1.1
<PackageReference Include="TK.MongoDB.GridFS.Repository" Version="2.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TK.MongoDB.GridFS.Repository --version 2.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TK.MongoDB.GridFS.Repository, 2.1.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install TK.MongoDB.GridFS.Repository as a Cake Addin
#addin nuget:?package=TK.MongoDB.GridFS.Repository&version=2.1.1

// Install TK.MongoDB.GridFS.Repository as a Cake Tool
#tool nuget:?package=TK.MongoDB.GridFS.Repository&version=2.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

TK.MongoDB.GridFS.Repository

Repository pattern implementation of MongoDB GridFS in .NET Framework

Usage

Settings
  1. Default ConnectionStringSettingName is set to "MongoDocConnection", but you can configure this by calling a static method as below:

    Settings.ConnectionStringSettingName = "MongoDocConnection";
    
  2. Default behavior is to always validate file name before inserting against the regex ^[\w\-. ]+$, but this can be changed by setting the following fields:

    Settings.ValidateFileName = false;
    Settings.FileNameRegex = new Regex(@"^[\w\-. ]+$", RegexOptions.IgnoreCase);
    
  3. Default behavior is to always check for file size before inserting with a maximum file size of 5 MB, but this can be changed by setting the following fields:

    Settings.ValidateFileSize = false;
    Settings.MaximumFileSizeInMBs = 5;
    
  4. You can configure chunk size for each bucket by using the method below, default setting will create a bucket with chunk size of 2 MB.

    Settings.Configure<Document>(2);
    
Models

Create a document model by inheriting abstract class BaseFile​ to use in repository. The name of this model will be used as bucket name in MongoDB.

public class Image : BaseFile
{
    public bool isDisplay { get; set; }
}
Repository methods
  1. Get (by Id)

    try
    {
        Image file = imgRepository.Get(new ObjectId("5e36b5a698d2c14fe8b0ecbe"));
        Console.WriteLine($"Output:\n{file.Filename}");
    }
    catch (FileNotFoundException ex)
    {
        Console.WriteLine($"Output:\n{ex.Message}");
    }
    
  2. Get (by Filename)

    IEnumerable<Image> files = imgRepository.Get("Omega1.png");
    
  3. Get (by Lamda Expression)

    IEnumerable<Image> files = imgRepository.Get(x => x.Filename.Contains("Omega") && x.UploadDateTime < DateTime.UtcNow.AddDays(-1));
    
  4. Insert

    byte[] fileContent = File.ReadAllBytes("../../Files/Omega.png");
    
    Image img = new Image()
    {
        Filename = "Omega.png",
        Content = fileContent,
        isDisplay = false
    };
    
    string id = imgRepository.Insert(img);
    
  5. Rename

    imgRepository.Rename(new ObjectId("5e37cdcf98d2c12ba0231fbb"), "Omega-new.png");
    
  6. Delete

    try
    {
        imgRepository.Delete(new ObjectId("5e36b5a698d2c14fe8b0ecbe"));
    }
    catch (FileNotFoundException ex)
    {
        Console.WriteLine($"Output:\n{ex.Message}");
    }
    
Tests

Refer to TK.MongoDB.GridFS.Test project for all Unit Tests.

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.1.6 27 1/17/2022
2.1.5 270 8/3/2021
2.1.5-rc1 104 8/2/2021
2.1.4 283 4/12/2021
2.1.3 878 9/1/2020
2.1.2 323 8/19/2020
2.1.1 231 8/18/2020
2.0.6 842 2/20/2020
2.0.5 288 2/6/2020
2.0.4 280 2/6/2020
2.0.3 264 2/3/2020
2.0.2 250 2/2/2020

Various improvements