Refactoring: parameterize constructor

In legacy code you may find classes with constructor where some objects are created and initialized. These classes are not easily testable because they depend on other classes and therefore when you unit test these classes they may also throw exceptions of classes they depend on. In these cases we can use parameterize constructor refactoring.

Let’s look at the following class.

public class MassMailer
private Mailer

        _mailer =
new Mailer

// other methods follow here

We can see that in the constructor of this class the Mailer object is created and initialized. If we want to unit test this class we have to remove this dependency. It is possible that we cannot go to interfaces but at least we can use “safe” copy of Mailer class for testing purposes. Maybe we can extend this class and override some methods with our dummy methods so mailer doesn’t affect our tests.

Let’s add new constructor to our class. New constructor accepts one parameter – the mailer object.

public MassMailer(Mailer mailer)
    _mailer = mailer;

Why don’t we change current constructor? The point simple – we may break current working code this way. It is good to have only one point where class is initialized, so let’s modify current constructor so it works safe.

public MassMailer()
new Mailer());

Now we have made MassMailer class independent of Mailer class if we use our new constructor. Of course we still have dependency but this dependency is for backward compatibility. In other means our class is now testable.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    2 thoughts on “Refactoring: parameterize constructor

    • June 8, 2009 at 6:50 pm

      I like doing things like this with constructor chaining…

      (Mailer implements IMailer)

      public class MassMailer
      private IMailer mailer;
      public MassMailer():this(new Mailer()){}
      public MassMailer(IMailer mailer)
      this.mailer = mailer;

    • June 9, 2009 at 5:00 am

      Congratulations…you’ve discovered Constructors!

    Leave a Reply

    Your email address will not be published. Required fields are marked *