Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Least-bad way to change constructor behaviour

Tags:

c#

massive

I feel like I'm stuck between several bad solutions here and need some advice on how to minimize future agony. We are using Massive ORM, which in its constructor has these lines:

var _providerName = "System.Data.SqlClient";

if (ConfigurationManager.ConnectionStrings[connectionStringName].ProviderName != null)
    _providerName = ConfigurationManager.ConnectionStrings[connectionStringName].ProviderName;

_factory = DbProviderFactories.GetFactory(_providerName);
ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

The important part for me here is that it reads the connection strings from ConfigurationManager. We are trying to centralize configuration, and in doing this we want to keep connection strings out of our web/app.configs (we have somewhere around 150 deployed hosts, so the overhead is becoming significant). However, this breaks down since the config file read is hardcoded here, and the ConnectionStrings collection is read only (there are workarounds, but they are all quite dirty).

One possible way out of this is to extract these lines into a virtual method and then change it with inheritance. This is not so nice when we want to update Massive though, and also calling virtual methods from a constructor is potentially bad.

What other alternatives do I have? Main priority here is to minimize impact when updating.

like image 733
carlpett Avatar asked Nov 04 '22 14:11

carlpett


1 Answers

You can eliminate the object's configuration source dependency by passing in all required fields through the constructor.

See Martin Fowler's article on Dependency Injection. The same concept applies here.

like image 141
apack Avatar answered Nov 15 '22 05:11

apack