TK.MongoDB.GridFS.Repository.NetCore 1.0.0-rc1

This is a prerelease version of TK.MongoDB.GridFS.Repository.NetCore.
Install-Package TK.MongoDB.GridFS.Repository.NetCore -Version 1.0.0-rc1
dotnet add package TK.MongoDB.GridFS.Repository.NetCore --version 1.0.0-rc1
<PackageReference Include="TK.MongoDB.GridFS.Repository.NetCore" Version="1.0.0-rc1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TK.MongoDB.GridFS.Repository.NetCore --version 1.0.0-rc1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TK.MongoDB.GridFS.Repository.NetCore, 1.0.0-rc1"
#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.NetCore as a Cake Addin
#addin nuget:?package=TK.MongoDB.GridFS.Repository.NetCore&version=1.0.0-rc1&prerelease

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

TK.MongoDB.GridFS.Repository.NetCore

Nuget Nuget Azure DevOps builds Azure DevOps tests Azure DevOps releases

Repository pattern implementation of MongoDB GridFS in .NET Standard 2.0

Usage

Settings
  1. Provide MongoDB ConnectionString by calling a static method as below:

    Settings.ConnectionString = "(connString)";
    
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; }
}
Bucket Attribute

You can configure the GridFS bucket attributes by decoration the model with Bucket attribute, for example:

[Bucket(PluralizeBucketName = false, MaximumFileSizeInMBs = 1, BucketChunkSizeInMBs = 1)]
public class Document : BaseFile
{
	/*...*/
}

The Bucket attribute has the following properties that you can set:

public class BucketAttribute : Attribute
{
    /// <summary>
    /// Pluralize bucket's mame. Default value is set to True.
    /// </summary>
    public bool PluralizeBucketName { get; set; }

    /// <summary>
    /// Validate file name on insert and update from FileNameRegex field. Default value is set to True.
    /// </summary>
    public bool ValidateFileName { get; set; }

    /// <summary>
    /// Validate file size on insert from MaximumFileSizeInMBs field. Default value is set to True.
    /// </summary>
    public bool ValidateFileSize { get; set; }

    /// <summary>
    /// File name Regex to validate. Default value is set to Regex(@"^[\w\-. ]+$", RegexOptions.IgnoreCase).
    /// </summary>
    public Regex FileNameRegex { get; set; }

    /// <summary>
    /// Maximum file size in MBs. Default value is set to 5.
    /// </summary>
    public int MaximumFileSizeInMBs { get; set; }

    /// <summary>
    /// GridFS bucket chunk size in MBs. Default value is set to 2.
    /// </summary>
    public int BucketChunkSizeInMBs { get; set; }
    
    /// <summary>
    /// Connection String from configuration / appsettings file. Default value is set from <i>Settings.ConnectionString</i>.
    /// </summary>
    public string ConnectionString { 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
1.0.0-rc1 39 8/23/2021

Initial