0

I have seen different implementations of connection strings in controllers.

Some are just as simple as:

public class tableNameController : Controller
{
    private ConnectionStringName db = new ConnectionStringName();
    ...

Others are:

public class tableNameController : Controller
{
    private ConnectionStringName db;

    public tableNameController()
    {
        db = new ConnectionStringName();
    }

Is there a benefit of putting the connection string in a constructor or just leave it as a private variable?

Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • 1
    Primarily opinion based. Poor design. Keep controller lean. inject service keep context out of controller. (disclaimer: I did **not** down vote) – Nkosi Jun 23 '17 at 17:10
  • @Nkosi do you know of any good tutorials that show dependency injection? – Grizzly Jun 23 '17 at 17:14
  • Which version of mvc are you using? 5.* or core? there is alot of documentation on MS official site specifically about DI – Nkosi Jun 23 '17 at 17:15
  • @BviLLe_Kid this course helped me, to understand how dependency injection and some other stuff like this works: https://www.pluralsight.com/courses/csharp-design-strategies – Sean Stayns Jun 23 '17 at 17:17

3 Answers3

6

The .NET runtime actually handles these cases exactly the same. It generates the same IL, so it is whatever looks better syntactically to you.

Now, there are tons of other considerations to make this testable and clean. I would do some research on Dependency Injection and Unit Testing.

Chris Kooken
  • 32,730
  • 15
  • 85
  • 123
0

There are no differences, but if you look at this answer: https://stackoverflow.com/a/24558/5056173

There are good rules from kokos, to decide whether you should use initializing in constructor or not:

  1. Don't initialize with the default values in declaration (null, false, 0, 0.0…).
  2. Prefer initialization in declaration if you don't have a constructor parameter that changes the value of the field.
  3. If the value of the field changes because of a constructor parameter put the initialization in the constructors.
  4. Be consistent in your practice (the most important rule).
Sean Stayns
  • 4,082
  • 5
  • 25
  • 35
0

From best practices point of view, both approaches are poor designed. In both cases this code will be hard to cover with tests. Consider next approach:

public class TableNameController : Controller
{
    private readonly ConnectionStringName _db;

    public TableNameController(ConnectionStringName db)
    {
        _db = db;
    }
}

Keep in mind that I have also added readonly construction, it will help you make sure that no one except constructor can set the value to a field. Also, in that approach - in unit tests it will be easy to pass you mocked ConnectionStringName instance into constructor.

If you want to know more about best practices, just google for SOLID, DRY, KISS, IoC. It is the required list of approaches/practices that will help you to write easy to read, well structured and maintainable code.

BTW, don't be afraid to start investigation, developers sometimes like to talk about simple things in a very complicated way.

Maris
  • 4,608
  • 6
  • 39
  • 68