Search code examples
c#asp.netasp.net-mvc

Can you run the IConfiguration Build in a 3rd party library


If I put this code in a 3rd party library I'm building.

public class AsyncDb2Read
{
    private readonly DB2Connection connection;

    public AsyncDb2Read()
    {
        var assembly = System.Reflection.Assembly.GetEntryAssembly();
        var c = new ConfigurationBuilder()
        .AddUserSecrets(assembly, true)
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .Build();
        string connectionString = c.GetConnectionString("DB2ConnectString")!;
        connection = new DB2Connection(connectionString);
        connection.SystemNaming = true;
    }

My question is: will it introduce any strange behavior in the consuming application since we are running IConfiruation's .Build() against the configuration root more than once?

If so, is there a way to consume the parent projects settings from within a 3rd party library without jeopardizing the configuration root?


Solution

  • It's hardly a good idea to take such a tight dependency on a particular configuration system. It bears similarities with the old Provider (anti)pattern.

    It looks as though the AsyncDb2Read class needs a DB2Connection. If so, pass it via the constructor:

    public class AsyncDb2Read
    {
        private readonly DB2Connection connection;
    
        public AsyncDb2Read(DB2Connection connection)
        {
            this.connection = connection;
        }
    

    The class doesn't need an IConfiguration object. It only needs DB2Connection, so pass only that object. This follows from Nikola Malovic’s 4th law of IoC:

    "Every constructor of a class being resolved should not have any implementation other then accepting a set of its own dependencies."

    - Inversion Of Control, Single Responsibility Principle and Nikola’s laws of dependency injection, Nikola Malovic, 2009

    You can wire up the DB2Connection object in your application's Composition Root. This gives you (or another client developer) the flexibility to use IConfiguration, if that's what you want, or another configuration system if such a need should arise.

    Since you're creating a 3rd-party library, you can't (or shouldn't) make any assumptions about the context in which client developers will be using the library.

    You can read more in DI-Friendly Library.