Question

I am wondering what is the best way to properly create a Development and Production environment for my C# winforms project. The unfortunate thing is that there is no development environment. Rather, I must specify the publish path each time i wish to create either the Production or Development build. Also since each prod or dev build uses a different connection string I must then go into the code and change that as well.

EDIT Another thing i would like to add is the fact that the 'testers' so to speak will be running the program from a local .exe which will look at the source files and detect if an update needs to be made. The only reason i am mentioning this is because the testers will not be running the code in 'DEBUG' mode.

Any ideas?

Thanks in advance!

Was it helpful?

Solution

UPDATE

Whew, 2009 sure was a long time ago ;) I'm going to leave the original answer for posterity, but, as Eric J. already stated in an updated version of his post in 2012, Slowcheetah is a much more flexible approach to this problem as it let's you create multiple config file per configuration.

Original answer

I can't answer for the publish part as I'm not sure it can be done. However, for the connection string part, you could make a "Connection String Provider Class" which would return the development connection string if you are in development, or the production connection string if you are in production.

I don't know if you are using Debug and Release compilation mode in the configuration manager, but it could be possible to use that with a pre-compiler directive:

public class ConnectionStringProvider
{
  public static string GetConnectionString()
  {
    #if (DEBUG)
      return DevConnectionString;
    #else
      return ProdConnectionString;
    #endif
  }
}

If you are using the VS2005 Settings pane (in the project settings), you can set 2 connections strings in there and use that instead of a constant value. If you are using any of the .Net DataSource, you could be always using the DEV connection string to create your object.

Also, the connection string provider could use a Singleton pattern, but I'm not sure there would be a real benefit.

OTHER TIPS

I'm not sure what you mean by

The unfortunate thing is that there is no development environment.

However, I would point out that you can handle things like

Also since each prod or dev build uses a different connection string I must then go into the code and change that as well.

with conditional compilation flags. For example you could so something like:

#if DEBUG
connString = "ABC";
#else
connString = "DEF";
#endif

DEBUG may not be the appropriate flag for your environment. You can define your own flags such as PROD or DEV and specify whether to define them for a particular build type, or from the command line if building outside of the IDE.

UPDATE

Since originally posting this answer, SlowCheetah has become available. It allows for XML transformations for your app.config/web.config (and for any other XML file such as NLog configuration files) per build configuration. So, if you have say a QA build configuration, you can have an XML transformation for QA that sets e.g. an appropriate connection string for the QA environment.

This is much preferred to placing configuration strings and similar in source code.

You could read the connection string from a configuration file.

You would then use the same program file in PROD and DEV, just a different configuration file.

What about using Virtual PC to create a separate environment ? That's now a best-practice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top