I need some design advice.
I have an application that will be reading data from various sources and doing checks and comparisons. My intention was to have all dependencies needed for data access (System.Data.SqlClient
etc) contained within one set of Data Access classes (SqlImporter
, JSONImporter
etc.). Other classes that need data would then just use these classes to do the work. Perhaps they would pass a connection string or other information.
However, I needed to make unit tests for these Data Access classes. To make them testable, I made my Data Access classes rely on abstractions/interfaces and then pass in concrete implementations to allow me to pass in Mock objects from a unit test, similar to Mocking SqlConnection, SqlCommand and SqlReader in C# using MsTest.
The result is something like this:
using System.Data.SqlClient;
public class SqlImport {
private IDbConnection conn;
public SqlImport(IDbConnection conn){
this.conn = conn;
}
}
My problem is that, all classes that use these Data Access classes will now also need to rely on Data Access dependencies (System.Data.SqlClient
). Does this somewhat defeat the purpose of having these classes? I mean, it is good for cohesion but...
I now have a class like this:
using System.Data.SqlClient;
using Importers;
public class Mapping {
public Mapping (){
}
public void LoadMappingFromDatabase(string connString){
SqlImport import = new SqlImport(new SqlConnection(connString));
// Do Stuff including using import to query db
}
}
Is this good design? Or am I better off just querying the DB inside LoadMappingFromDatabase()
directly since the required dependencies are already in the class?