ExportXMLWordPrintableJSON

    • Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      ASP.NET Core DI functions via IServiceCollection dependency registration.
      While MongoClient can be registered with a general approach, like:

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      services.AddSingleton<IMongoClient>(_ => new MongoClient(connectionString));
      

      There's a trend among prominent dotnet libraries in providing specialized extension methods for dependency registration, like:

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      services.AddMongoClient(connectionString);
      

      This could be achieved by creating a separate nuget package (e.g. MongoDB.Extensions.DependencyInjection) with an extension implementation. The basic one is:

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      public static class ServiceCollectionExtensions
      {
      	public static IServiceCollection AddMongoClient(this IServiceCollection services, string connectionString)
      	{
      		services.AddSingleton<IMongoClient>(_ => new MongoClient(connectionString));
      		return services;
      	}
      }
      

      The major rational for this improvement is directing driver users towards using MongoClient as singleton by default, since not everyone notice the advised singleton lifetime scope in documentation, and per-instance usage sometimes leads to issues with the resource management.

            Assignee:
            Arthur Vickers
            Reporter:
            Mikalai Mazurenka (Inactive)
            None
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: