Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
 
 
Programming Blogs - Blog Catalog Blog Directory
 
 
 

Links

Social

Refactoring: extract and override factory method

I’m sure you have seen classes that initialize a lot of objects in their constructor. These classes may be hard to test because of those object creations I mentioned. To get around this problem we use Extract and override factory method refactoring so we can extend these classes and override some factory methods.

Let’s see the following code.


public class XmlPaymentManager

{

    private PaymentTranformer _transformer;

 

    public XmlPaymentManager()

    {

        var readerPath = ConfigurationSettings.AppSettings["ReaderPath"];

        var writerPath = ConfigurationSettings.AppSettings["WriterPath"];

 

        var reader = new StreamReader(readerPath);

        var writer = new StreamWriter(writerPath);

 

        _transformer = new PaymentTranformer(reader, writer);

    }

 

    // ...

}


We can see here initialization of StreamReader and StreamWriter. We cannot use these classes for unit testing purposes because we don’t want to read and write files. If we do then we have integration tests as we may face file system issues. As a first step let’s move creation of PaymentTransformer class to separate method.


public class XmlPaymentManager

{

    private PaymentTranformer _transformer;

 

    public XmlPaymentManager()

    {

        _transformer = GetTransformer();

    }

 

    protected PaymentTranformer GetTransformer()

    {

        var readerPath = ConfigurationSettings.AppSettings["ReaderPath"];

        var writerPath = ConfigurationSettings.AppSettings["WriterPath"];

 

        var reader = new StreamReader(readerPath);

        var writer = new StreamWriter(writerPath);

 

        return new PaymentTranformer(reader, writer);

    }

 

    // ...

}


Now we can create extended class that overrider GetTransformer() method. For testing purposes we use FakePaymentTransformer.


public class TestXmlPaymentManager : XmlPaymentManager

{       

    protected PaymentTranformer GetTransformer()

    {

        return new FakeXmlPaymentTransformer();

    }

}


Extract and override factory method helps you if you can extend the class you want to test. You may save a lot of time when you find larger hard to test classes and you can make them testable using this refactoring method.

kick it on DotNetKicks.com pimp it Shout it

Comments

Gunnar Peipman's ASP.NET blog said:

Here you can find links to my postings that introduce source code refactoring methods . If I have done

# March 23, 2009 12:16 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# March 24, 2009 6:42 AM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# March 24, 2009 6:45 AM

Refactoring: extract and override factory method - Gunnar Peipman's ASP.NET blog - DotNetBurner said:

DotNetBurner.com - news and articles about .net DotNetBurner

# March 24, 2009 6:47 AM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# March 25, 2009 2:10 PM

RaiulBaztepo said:

Hello!

Very Interesting post! Thank you for such interesting resource!

PS: Sorry for my bad english, I'v just started to learn this language ;)

See you!

Your, Raiul Baztepo

# March 31, 2009 5:17 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)