I have an app.config file that contains some connection strings, this is accessed on a project-by-project basis

In my solution, I have the following projects:

  • A
  • B
  • C
  • D

Project A contains the app.config file, and a class called ConnectionStringHelper

public class ConnectionStringHelper
    public string GetConnectionString(string index)
        return ConfigurationManager.ConnectionStrings[index].ConnectionString;

Here is an example of how this is used (in project B):

var connectionString = new ConnectionStringHelper().GetConnectionString("test");

Is there anything wrong with having a public method that returns the connection string?

This feels like a code smell, but I'm unsure how I would get around it?

EDIT: I do use dependency injection across the solution, however I had to write this method to be able to use it in some unit tests, as I can't use DI with them.

  • \$\begingroup\$ I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. The example code that you have posted is not reviewable in this form because it leaves us guessing at your intentions. Unlike Stack Overflow, Code Review needs to look at concrete code in a real context. Please see Why is hypothetical example code off-topic for CR? \$\endgroup\$ – Vogel612 Apr 9 '19 at 10:08
  • 1
    \$\begingroup\$ If you cannot use DI with your tests then your DI is off and is the real issue here, not the connection-string. You loose one of the main advantages of DI by not being able to use DI in tests. \$\endgroup\$ – t3chb0t Apr 9 '19 at 10:15

Browse other questions tagged or ask your own question.