TK.MongoDB.GridFS.Repository
2.1.1
See the version list below for details.
dotnet add package TK.MongoDB.GridFS.Repository --version 2.1.1
NuGet\Install-Package TK.MongoDB.GridFS.Repository -Version 2.1.1
<PackageReference Include="TK.MongoDB.GridFS.Repository" Version="2.1.1" />
paket add TK.MongoDB.GridFS.Repository --version 2.1.1
#r "nuget: TK.MongoDB.GridFS.Repository, 2.1.1"
// 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
TK.MongoDB.GridFS.Repository
Repository pattern implementation of MongoDB GridFS in .NET Framework
Usage
Settings
Default
ConnectionStringSettingName
is set to "MongoDocConnection", but you can configure this by calling a static method as below:Settings.ConnectionStringSettingName = "MongoDocConnection";
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);
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;
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
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}"); }
Get (by Filename)
IEnumerable<Image> files = imgRepository.Get("Omega1.png");
Get (by Lamda Expression)
IEnumerable<Image> files = imgRepository.Get(x => x.Filename.Contains("Omega") && x.UploadDateTime < DateTime.UtcNow.AddDays(-1));
Insert
byte[] fileContent = File.ReadAllBytes("../../Files/Omega.png"); Image img = new Image() { Filename = "Omega.png", Content = fileContent, isDisplay = false }; string id = imgRepository.Insert(img);
Rename
imgRepository.Rename(new ObjectId("5e37cdcf98d2c12ba0231fbb"), "Omega-new.png");
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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
- MongoDB.Driver.GridFS (>= 2.10.1)
- SharpCompress (>= 0.24.0)
- System.Buffers (>= 4.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Various improvements