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

This is a prerelease version of TK.MongoDB.GridFS.Repository.NetCore.
There is a newer version of this package available.
See the version list below for details.
dotnet add package TK.MongoDB.GridFS.Repository.NetCore --version 1.0.0-rc1                
NuGet\Install-Package TK.MongoDB.GridFS.Repository.NetCore -Version 1.0.0-rc1                
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="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                
#r "nuget: TK.MongoDB.GridFS.Repository.NetCore, 1.0.0-rc1"                
#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 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                

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.

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 was computed. 
.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
1.0.0 425 11/4/2022
1.0.0-rc2 164 11/1/2022
1.0.0-rc1 195 8/23/2021

Initial