Use a simple connection string to improve your code

By: Dan

When writing code, anything that a class needs to perform its duties should be passed (injected) into it. There should be no hidden references to outside resources buried inside the code. Why does this matter? Read on for a simple example.

Consider the following code snippets. The first code snippet shows a sample Data class that follows a pattern I’ve seen in many projects. Every data method reads the configuration file to get the database connection string.

public class Data
{
public Data()
{
}
public List GetData(int someId)
{
string conString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
}
}

The second code snippet is a more efficient variation on the first. The configuration file is read in the constructor and the connection string is saved in a local variable. This is a small improvement over the first example.

public Class Data
{
private string conString = "";
public Data()
{
conString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
}
public List GetData(int someId)
{
}
}

The next snippet shows a better way. The connection string is passed (injected) as a parameter in the class’s constructor method. This removes the responsibility of getting the connection string from the class and places where it should be, on the consumer of the class.

public Class Data
{
private string _conString = "";
public Data(string conString)
{
_conString = conString;
}
public List GetData(int someId)
{
}
}

Why does this matter? In the first two examples, you have no idea that the class is reading the connection string from the configuration file unless you are familiar with the code. You could use the class in your project, unaware you need the connection string in your project configuration file until you test your code. It gets even more complicated if this class is part of a shared API library where you don’t have easy access to the source code.

In the last example, you know immediately that you need to provide a connection string to the Data class. It is required to instantiate an instance of the class. If you forget, the compiler will tell you. It also removes the class’s dependency on the configuration file.


About Dan
Dan Cash was a Senior Director of Technology at alligatortek, where he encouraged and championed the development team to explore new development technologies like Appcelerator, Xamarin, and AngularJS.

alligatortek is a Chicago custom software and application development firm that began partnering with clients 25 years ago to develop game-changing applications for its clients. alligatortek is recognized as a best-in-class partner, innovator and employer. The firm is a two-time winner of the Chicago Innovation Awards and a 2015 Microsoft Partner of the Year. alligatortek is a four-time winner of the 101 Best & Brightest Companies to Work For in the Nation.

Copyright © 2018 alligatortek. All rights reserved.