Freigeben über


Writing legacy code on purpose

Earlier this week I was working on a feature where I was using a specific framework and I got frustrated because my usual ways of faking dependencies in my unit tests were being blocked by the framework I was using. All my usual first option approaches just felt wrong and became cumbersome to use. After a discussion with a colleague I realized that the solution was to use my number one option for dependency injection in legacy code; protected virtual methods. I realized that even though I was writing a completely new class, the fact it had to inherit from a class in the framework used made my newly created class into a legacy class. Legacy as in hard to test. As soon as I started to treat my new class as legacy code it was a breeze to make it easy to test and decoupled from the rest of the code using the standard approaches I have when adding unit tests to legacy code.

So even though it makes me feel a little dirty that I write a legacy code class on purpose I think the result is better than the alternatives in this case.