I am working on a project where SqlConnection
is created via a static method, lets say it's DatabaseAccess.GetSqlConnection()
. This method reads the .config file to get the connection string and creates a connection.
Is it ok to refactor the code to use an ISqlConnectionCreator
(or ISqlConnectionFactory
) responsible for creating the actual connections just for the sake of testing (as it seems more "by the book" for me) or go easy and just create a .config file in the test project containing test connection strings?
The second solution seems more error-prone and creates an indirect relation between tests and the tested classes.
I want to make sure there is a justification for the extra work of refactoring code to use the injected connection creator.
-
Disclaimer, I work at Typemock. I agree with Doc Brown on use what works best and requires less maintenance. You can use mocking to avoid refactoring altogether from your production code. Just mock the static DatabaseAccess.GetSqlConnection() method.Al.exe– Al.exe2015年03月29日 08:17:50 +00:00Commented Mar 29, 2015 at 8:17
-
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.gnat– gnat2015年03月29日 12:48:10 +00:00Commented Mar 29, 2015 at 12:48
-
Maybe I misunderstood something, but I generally like this idea - it might save me the refactoring bit and I will still be able to control the connection string used... but I don't know if there is a free static-mocking tool, that might be important for my company.user1713059– user17130592015年03月29日 12:55:54 +00:00Commented Mar 29, 2015 at 12:55
-
There are no free static mocking tools.Al.exe– Al.exe2015年03月29日 14:51:45 +00:00Commented Mar 29, 2015 at 14:51
-
In that case you'll have to do some refactoring after all. You can extract only the code that retrieves the connection string from the .config file to a method, then extract an interface from this method, and mock it with a free mocking framework.Al.exe– Al.exe2015年03月29日 15:02:06 +00:00Commented Mar 29, 2015 at 15:02
2 Answers 2
From a pragmatic view I would recommend to use whatever works best, with the least maintenance effort.
In this specific situation, I would probably go with "option 3": separate the part which reads the connection string from the part which creates the actual connection into two different methods. So the connection string becomes an input parameter of method 2, and in your test code, you can omit the config evaluation and provide the test connection string any way you like, for example, as part of the hardcoded test data. As a result, you do neither need a test config file, nor an ISqlConnectionCreator
.
-
I'm not sure if I understand correctly, but i think you propose to add a
string connectionString
parameter to theDatabaseAccess.GetSqlConnection()
. If so, the code will need versy similar amount of work to be refactored (as with the connection factory), don't you think?user1713059– user17130592015年03月29日 12:52:17 +00:00Commented Mar 29, 2015 at 12:52 -
Yeah I think I misunderstood, but anyway you propose to pass connection string to all database methods, right?user1713059– user17130592015年03月29日 14:13:37 +00:00Commented Mar 29, 2015 at 14:13
I'm having a hard time seeing the benefit from this extra code. Code is a liability and unless there is an actual problem I probably wouldn't want to spend time on it.
I would just create the app.config file and call it a day.
-
Never mind good, decoupled design then, right?RubberDuck– RubberDuck2015年10月25日 22:57:24 +00:00Commented Oct 25, 2015 at 22:57
-
never mind kiss and yagni. it is all a balance and an analysis of the current project instead of automatic reactions for more complexity.Esben Skov Pedersen– Esben Skov Pedersen2015年10月26日 04:46:58 +00:00Commented Oct 26, 2015 at 4:46