How to prevent an application from communicating with the outside world during debug/testing?

StackOverflow https://stackoverflow.com/questions/9879908

Pergunta

I have two separate applications (both part of the same system) that share a common database. One application is a "gateway" that is responsible for communicating with the outside world and the other is a desktop client that local users on the LAN use to read/modify/update data in the local database.

At specific intervals, the gateway application will download new data to the local database from various web services. Likewise, the gateway application will scan for changes to the local database (made by users via the desktop client) and upload those changes to the appropriate web services.

Two databases are in play, one for production use and one for test/debug use. The gateway switches between the two by changing the connection string in app.config.

When testing the gateway application, I would like for it to act in a read-only manner; that is, it would be able to download new data to the (test/debug) database, but it would not be able to upload any changes back to the web service.

The way I accomplish this now is to use the #IF DEBUG directive in places where the application would upload data like so:

public void DetectAndUploadChanges()
{
    Uploader Up = new Uploader();
    Up.DetectChanges();

    #IF !DEBUG
        Up.UploadChanges();
    #END IF
}

This method relies on me remembering to use the debug directive everywhere the code can upload some type of change.

Is there a better/alternative way to do this type of thing?

Foi útil?

Solução

You can use DI (Dependency injection) where you inject at runtime instance.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top