We have a very big application written in C++ using the MFC framework (millions of code lines). No need to say that it's a legacy system, but it's also heavily maintained and updated. This application lacks a lot of architecture. For example, we have a lot of classes that do queries directly to the database, which creates a lot of difficulty for unit testing.

I have read the book Working Effectively With Legacy Code, and one of the most important things to do first is to create some kind of architecture so we can separate the GUI from the logic and from the database.

My idea to go towards this is to create something like a database facade. There will be 2 goals for this:

  1. Encapsulate all the calls to the database behind this facade.
  2. Be able to test the facade in batch to detect errors in the SQL very fast.

The database facade itself will be implementing an IDatabaseXXXX interface so it could be easily mocked in the unit tests. The second goal will be to be able to run all the functions in the facade in batch to test if there are errors in the SQL queries. I don't count them as unit tests and would be running them only once per day.

My main concern about doing this is that I don't know if there is an existing framework or library to take care of this abstraction. As far as I know, there are some on other platforms like .NET, but it seems that for C++ or MFC, there are not a lot of options. Like I said, the application is very big and the change will be done slowly, mostly when new queries are added to the software.

I think it is a good thing to add that we also have a LOT of recordsets (one for almost each object). For the moment, I don't know how to deal with them, so I kept them on the side and decided to deal with the regular SQL queries. But if you have suggestions to deal with recordsets, I will be glad to hear them.

So my main question is: Is this whole idea of database facade worth trying? Is there any library of framework to help me in this adventure? If there are C++/MFC programmers out there, how do you deal with the abstraction of the query to the database?



Some people on the chat suggest looking up at repository pattern which I knew from the world of MVC 3. Any suggestion or tutorial about the repository pattern in C++? Most of what I found is in the .NET world.

  • do you mean MFC == MS foundation classes or MVC == Model View Controller? MFC isn't really a framework, afaik, which is part of the challenge I suspect.
    – user53019
    Apr 9, 2013 at 14:44
  • 2
    I mean Micorosft Foundation Class. I know MFC is considered a library but since almost all our code is based on it, I see it as an application framework. Apr 9, 2013 at 15:08
  • Is the code well-tested? May 31, 2013 at 14:27
  • Hi Luc! No it isn't! It's a 1 or 2 millions or code line from 20 years of development with different teams. May 31, 2013 at 15:57

2 Answers 2


As your update suggets, the repository pattern would be able to help seperate the database calls from your UI. And replacing the database calls with calls to an IDatabase interface is certainly an improvement. The repository pattern will allow you to further abstract the the data retrieval into something meaningful to the application. UserRepository, ThingRepository, etc.


As an approach to implementing this architecture. When I need to make code testable to refactor the first test is always the hardest. Find a well understood piece of code that you want to test and make that class/method testable. Once this is in place it is persistence and patience that will allow you to complete the task of introducing the new architecture into the legacy system.


I have read the book Working Effectively With Legacy Code, and one of the most important things to do first is to create some kind of architecture so we can separate the GUI from the logic and from the database.

That would be how to read the legacy code and then rewrite the system in a new platform. But you can't honestly expect to actually do this in the legacy code itself. Adding architecture to legacy code after the fact...that just doesn't sound feasible in the real world.

  • This is not an answer. What the OP wants to do is certainly possible, but it will not be easy.
    – Seth M.
    Jun 11, 2013 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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