By estherschindler
via codemanship.co.uk
Published: Jan 14 2013 / 08:07
When refactoring legacy code that relies on static methods to access external systems - for example, for data access - our first goal is usually to make the code unit-testable. Therefore, we seek to invert that dependency on a static method to make it substitutible. I demonstrated in previous blog posts how we do this in fairly simple situations. The dance is pretty straightfoward: you turn the static method into an instance method and then do a "Find and Replace" to swap references to the class that method is on into with "new ClassName().", so the target of invocation is now an instance. We then give client code a way to do the old polymorphic switcheroo by injecting that instance into, say, the constructor of the class where it's being used. As easy as cake. What often comes up though is a more complex, and less than ideal situation.
Tweet
SaveShareSend
Tags: methodology
Add your comment