{"id":49079707,"url":"https://github.com/fmisrl/mongo-dbcontext","last_synced_at":"2026-04-20T12:19:15.140Z","repository":{"id":284412596,"uuid":"954113386","full_name":"fmisrl/mongo-dbcontext","owner":"fmisrl","description":"This package simplifies MongoDB integration by providing a unified context for database access, eliminating the need to manage table names, schema configurations, or connection strings manually.","archived":false,"fork":false,"pushed_at":"2025-11-27T13:24:39.000Z","size":102,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-30T06:17:21.594Z","etag":null,"topics":["csharp","dbcontext","dotnet","mongodb","mongodb-driver"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fmisrl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-03-24T15:25:31.000Z","updated_at":"2025-11-27T13:24:43.000Z","dependencies_parsed_at":"2025-07-07T14:43:55.429Z","dependency_job_id":"f548f6e1-d138-4322-8761-b67d7b54237d","html_url":"https://github.com/fmisrl/mongo-dbcontext","commit_stats":null,"previous_names":["fmisrl/mongo-dbcontext"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fmisrl/mongo-dbcontext","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmisrl%2Fmongo-dbcontext","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmisrl%2Fmongo-dbcontext/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmisrl%2Fmongo-dbcontext/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmisrl%2Fmongo-dbcontext/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fmisrl","download_url":"https://codeload.github.com/fmisrl/mongo-dbcontext/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmisrl%2Fmongo-dbcontext/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32046737,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T11:35:06.609Z","status":"ssl_error","status_checked_at":"2026-04-20T11:34:48.899Z","response_time":94,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dbcontext","dotnet","mongodb","mongodb-driver"],"created_at":"2026-04-20T12:19:14.177Z","updated_at":"2026-04-20T12:19:15.127Z","avatar_url":"https://github.com/fmisrl.png","language":"C#","readme":"﻿# MongoDB Context\n\nThis package fills a gap in the MongoDB driver by providing a convenient way to create a unified context for database access. It allows you to register a single object within your application, making it easier to interact with the database without worrying about table names, schema configurations, connection strings, and other low-level details.\n\n**⚠️ This project is currently used in production across several internal applications without issues. However, please use it with caution — we are planning a refactor to enhance its resilience and expand its functionality.** \n\n## Compatibilities and driver version\n\n| Version | .NET SDK       | MongoDB* | MongoDB Driver |\n|---------|----------------|----------|----------------|\n| \u003e= 1.9  | netstandard2.0 | 7, 8     | 3              |\n\n*this version is included in our integration tests. You may try connecting to other compatible driver versions, but stability and compatibility are not guaranteed. \n\n## Examples\n\nA running example of the usage is [this test project](https://github.com/fmisrl/mongo-dbcontext/tree/master/tests/FmiSrl.MongoDbContext.SourceGenerator.Tests) inside the test folder.\n\n## Installation\n\nYou need to reference two projects:\n\n```\ndotnet add package FmiSrl.MongoDbContext.Abstractions\ndotnet add package FmiSrl.MongoDbContext.SourceGenerator\n```\n\nThe `FmiSrl.MongoDbContext.Abstractions` project contains all the abstraction layer and base functionalities for the db context.\nThe `FmiSrl.MongoDbContext.SourceGenerator` project contains the source generator for generating the specialized code for your db context.\n\n\n## Usage\n\nAs usual, create your database layer models, keep in mind, that if the model is a root class with an id and not a nested object you should inherit from `IAggregateRoot` (in the next versions this requirement will be removed). \n\n```csharp\npublic class TestObject : IAggregateRoot\n{\n    [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]\n    public string? Id { get; set; } = null!;\n    \n    public string Name { get; set; } = null!;\n}\n```\n\n---\n\nCreate an interface that inherits from `IDbContext` with all your collections like that.\n\n```csharp\npublic interface ITestDbContext : IDbContext\n{\n    [Collection(\"Test_Objects\")]\n    IMongoCollection\u003cTestObject\u003e TestObjects { get; }\n    \n    IMongoCollection\u003cTestGenericObject\u003cstring\u003e\u003e TestGenericObjects { get; }\n    \n    string GetEmptyString { get; }\n\n    string GetHelloString();\n}\n```\n\nAs you can see, every collection is registered with type `IMongoCollection\u003cT\u003e` where `T` is the type of your object. If you do not provide collection's name with the `[Collection(\"\u003cname\u003e\")]` attribute, the name of the property was used.\n\nYou can place your own methods and properties in the interface to implement it later, they are ignored by the generator.\n\n---\n\nCreate a partial class that implements the interface.\n\n```csharp\npublic partial class TestDbContext : ITestDbContext\n{\n    public string GetEmptyString =\u003e string.Empty;\n\n    public string GetHelloString()\n    {\n        return \"Say Hello!\";\n    }\n}\n```\n\nYou must implement yourself all the methods and properties that are not generated by the generator.\nThe code behind the collection properties will be generated by the source generator.\n\n--- \n\nNow you can create a connection to your database and use it directly in your application.\n\n```csharp\nconst string connectionString = \"mongodb://mongoadmin:mysupersecretpassword@localhost:27017\";\nconst string databaseName = \"test\";\n\nvar dbContext = new TestDbContext(); \ndbContext.OpenConnection(connectionString, databaseName);\n```\n\nor share it using the dependency injection.\n\n```csharp\nbuilder.Services.AddSingleton\u003cITestDbContext, TestDbContext\u003e(dbContext);\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffmisrl%2Fmongo-dbcontext","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffmisrl%2Fmongo-dbcontext","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffmisrl%2Fmongo-dbcontext/lists"}