MongoGogo 1.0.3
See the version list below for details.
dotnet add package MongoGogo --version 1.0.3
NuGet\Install-Package MongoGogo -Version 1.0.3
<PackageReference Include="MongoGogo" Version="1.0.3" />
paket add MongoGogo --version 1.0.3
#r "nuget: MongoGogo, 1.0.3"
// Install MongoGogo as a Cake Addin #addin nuget:?package=MongoGogo&version=1.0.3 // Install MongoGogo as a Cake Tool #tool nuget:?package=MongoGogo&version=1.0.3
MongoGogo Guide
- A generic repository implementation using MongoDB.Driver
- Auto implement the dependency injection for Microsoft.Extensions.DependencyInjection.
- Provides attributes used for manage
databases
andcollections
for eachconnection
.
Install via .NET CLI
dotnet add package MongoGogo
Example
After few steps of configuration
and class building
, you can easily get any Collection
, Database
and context
in abstract through the dependency resolution system.
public class MyController : ControllerBase
{
private readonly IGoContext<MyMongoDBContext> MyContext;
private readonly IGoDatabase<MyMongoDBContext.City> CityDatabase;
private readonly IGoCollection<Hospital> HospitalCollection;
public MyController(IGoContext<MyMongoDBContext> myContext,
IGoDatabase<MyMongoDBContext.City> cityDatabase,
IGoCollection<Hospital> hospitalCollection)
{
this.MyContext = myContext;
this.CityDatabase = cityDatabase;
this.HospitalCollection = hospitalCollection;
}
}
IGoContext<T>
、IGoDatabase<T>
and IGoCollection<T>
:
- is in generic type with very few restriction
- acts like
IMongoClient
、IMongoDatabase
andIMongoCollection<T>
of the well-known package MongoDB.Driver.
using MongoDB.Driver;
public class MyController : ControllerBase
{
[HttpGet("IGoCollection<T>_GetList")]
public async Task<IActionResult> Collection()
{
var hospitals = (await HospitalCollection.FindAsync(_ => true)).ToEnumerable();
return Ok(hospitals);
}
[HttpGet("IGoDatabase<T>_ListAllCollections")]
public IActionResult Database()
{
var collectionNames = CityDatabase.ListCollectionNames().ToList();
return Ok(collectionNames);
}
[HttpGet("IGoContext_DeleteDatabasebyName")]
public IActionResult Context(string databaseName)
{
MyContext.DropDatabase(databaseName);
return Ok($"{databaseName} is successfully droped.");
}
}
Configuration
In the ConfigureSerivces
segment of .net core
, pass an IGoContext
instance into your IServiceCollection
.
public void ConfigureServices(IServiceCollection services)
{
services.AddMongoContext(new MyMongoDBContext("my mongodb connection string"));
}
Or, you can configure mongoContext through implementationFactory
public void ConfigureServices(IServiceCollection services)
{
Func<IServiceProvider, object> implementationFactory; //some custom factory
services.AddMongoContext<MyMongoDBContext>(implementationFactory);
}
Context
IGoContext<TContext>
- Stands for a MongoDB Connection (using package MongoDB.Driver)
- an instance stores
connection string
and is in charge of the structure of anentity
. - generic TContext is used for dependency resolution system.
public class MyMongoDBContext : GoContext<MyMongoDBContext>
{
[MongoDatabase]
public class City { }
[MongoDatabase("Log")]
public class Logger { }
[MongoDatabase]
public class Admin { }
public MyMongoDBContext(string connectionString) : base(connectionString)
{
}
}
public abstract class GoContext<TContext> : IGoContext<TContext>
{
//...
}
Congratulations!! You successful set up the database
in the IGoContext
.
A vaild database
of an entity/context
must be:
- an
inner class
ofIGoContext<TContext>
which ispublic
orinternal
- decorated by
MongoDatabase
attribute
Collections
Provided the database City
has two collection City
and Hospital
.
- decorated by
MongoCollection
attribute, and specify correspondingdatabase
type. - Notice once again, this
database
type must be aninner class
of anIGoContext<TContext>
[MongoCollection(fromDatabase: typeof(MyMongoDBContext.City))]
public class City
{
[BsonId]
public ObjectId _id { get; set; }
public string Name { get; set; }
public int Polulation { get; set; }
}
Besides, keep the old way using attribute of MongoDB.Driver, if you have some restriction on this collection
, field
, or even some Serializer
.
using MongoDB.Bson.Serialization.Attributes;
[MongoCollection(typeof(MyMongoDBContext.City),"Hospital"]
[BsonIgnoreExtraElements]
public class Hospital
{
[BsonId]
public ObjectId _id { get; set; }
[BsonElement("FullName")]
public string Name { get; set; }
[BsonIgnoreIfDefault]
public City City { get; set; }
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. 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 | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- MongoDB.Driver (>= 2.16.0)
-
net5.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- MongoDB.Driver (>= 2.16.0)
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- MongoDB.Driver (>= 2.16.0)
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 |
---|---|---|
6.0.0 | 66 | 5/20/2024 |
5.4.1 | 104 | 2/5/2024 |
5.4.0 | 82 | 2/2/2024 |
5.3.0 | 802 | 11/16/2023 |
5.2.2 | 120 | 11/3/2023 |
5.2.1 | 780 | 8/7/2023 |
5.2.0 | 318 | 7/24/2023 |
5.1.2 | 149 | 7/21/2023 |
5.1.1 | 239 | 7/4/2023 |
5.0.1 | 226 | 6/28/2023 |
4.0.0 | 163 | 6/19/2023 |
3.1.0 | 635 | 3/17/2023 |
2.0.1 | 551 | 11/18/2022 |
2.0.0 | 347 | 11/18/2022 |
1.0.3 | 327 | 11/10/2022 |
1.0.2 | 353 | 11/9/2022 |
1.0.1 | 328 | 11/9/2022 |
1.0.0 | 355 | 11/9/2022 |