TK.MongoDB.GridFS.Repository.NetCore
1.0.0-rc1
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
<PackageReference Include="TK.MongoDB.GridFS.Repository.NetCore" Version="1.0.0-rc1" />
paket add TK.MongoDB.GridFS.Repository.NetCore --version 1.0.0-rc1
#r "nuget: TK.MongoDB.GridFS.Repository.NetCore, 1.0.0-rc1"
// 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
Repository pattern implementation of MongoDB GridFS in .NET Standard 2.0
Usage
Settings
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
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 | 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. |
-
.NETStandard 2.0
- CG.Pluralization (>= 0.3000.12)
- MimeMapping (>= 1.0.1.37)
- MongoDB.Driver.GridFS (>= 2.13.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial