Use a simple connection string to improve your code

By: Dan
DOWNLOAD AS PDF

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.

latest news

01

5 signs your company needs an ERP

An ERP system captures information from all departments, and it may be time for you to implement it within your organization. Here are 5 signs that make it clear your business will benefit from an ERP system.

02

7 Benefits of an ERP System For Your Business

ERPs manage the core processes of an organization – from HR to sales to finance – and house all the data. Many ERPs also offer increased visibility, analytics, and efficiency across the company. Let’s explore some of the benefits of running your business on an ERP system

03

8 Benefits to Adopting DevOps into your Organization

Under DevOps, a “product-first” approach, companies align development projects with the business goals of operations so teams can jointly build, test, release, and maintain new digital applications more frequently and more efficiently. What benefits does DevOps offer?

Copyright© 2018 alligatortek. All rights reserved.