Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a large application written in web forms. It was developed under 1.0, and still uses DataGrid, though portions have been updated.

Most of the code resides in either the codebehind, or in controller classes that can't be instantiated outside of a running IIS. (The controller framework we use provides lifetime management tied to session state.) Data access is through a custom DAL, meaning that most of that code also requires a live database with proper data inside it.

I want to decouple the code from the database and the web server, so that I can run it under a test harness. Are there any good strategies for moving from this sort of code to a more testable structure?

share|improve this question
Good luck. Seems like a major rewrite. Try to move as much logic as possible into a domain model and call the newly created model then from the controllers. Also create an interface for the DAL and make an adapter to use the current DAL with it. – Falcon Aug 2 '11 at 14:51

The problem is. that with the information you give, the range of possible answers is theoretically infinite.

If I collect the hard facts I get the following picture:

  • ASP.NET 1.0 Webform
  • DataGrid
  • Tight coupling
  • Dataaccess via custom DAL

Besides that, you didn't say in which direction your refactoring is heading. Simply »cleaning the mess« is a good starting point, but not enough. And as @Wayne poined out: it would end up effectively in a rewrite.

There are at least 2 possibilities:

1) Making a SPA where you have a smart frontend and do most - or at least a good deal - of logic in the frontend

2) Making a conventional web application with WebAPI. Besides, though I actually have not worked with it, but perhaps it is worth a glimpse: NancyFX

Depending on how big your application / codebase is - and on how hip you want to be, you should think about using microservices

Some easy recommendations are:

Data access is through a custom DAL, meaning that most of that code also requires a live database with proper data inside it.

  • This could be mocked or stubbed away

For the rest, it depends:

If the codebase is somehow in shape. Try to guess, what the code does. Write a test. See if your assumption holds. If so, write another test, to veriy another assumption. If the PITA-factor is too high, rewrite the business logic completely. Otherwise repeat and build an iterative refined understanding of the code and evolve a finegrained test suite. Step by step you are knitting a saftey net around the code. If it is tight enough, take the chainsaw and cut the code in handy pieces.

I have to admit, that this is a very general advice, but without further knowledge of your codebase, it is hard to give any concrete advice.

share|improve this answer
Yup. I'm gonna end up rewriting this mess entirely, aren't I? I put a bounty on this because my question seemed like a duplicate, but I may post one with the details filled in. Does that sound like a reasonable idea? – RubberDuck Jul 17 '15 at 22:30
»I'm gonna end up rewriting this mess entirely, aren't I?« Yes. And you are doing you (and your team) a favour. » I may post one with the details filled in. Does that sound like a reasonable idea« I'm okay with that ;) – Thomas Junk Jul 17 '15 at 22:51

The best way would be to refactor out the code-behind to logical layers (services, DAO/Repository, etc). Look at Model View Presenter for ideas how to abstract away the rest, but realistically an endeavor like this is usually better served by rewriting the thing properly; speaking from experience it's very hard to refactor WebForms when it's done terribly, chances are you will have to redo so much that you're basically rewriting the thing anyways.

share|improve this answer
Not what I wanted to hear, but pretty much what I expected.... – RubberDuck Jul 17 '15 at 17:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.