Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC and multiple environments

Tags:

asp.net-mvc

How does ASP.NET MVC, if at all, deal with or provide ways to create your application using multiple environments? For example:

  1. Development environment (local machine, probably run via the built-in web server and talking to a local database)
  2. Testing (runs against a preloaded databse with example data, although this part could be skipped and mocks could be used)
  3. Production database on a real server with real data

Ruby on Rails has the concept of environments and "automagically" can deduce if you're in development or production, so you can specify your connection information (connection string) in a config file and the framework dynamically pulls the appropriate one. Is there a similar way of doing things with .NET MVC? If not then how are professional developers using .NET MVC handling different environments?

The only way I can think of is to manually add an "environment" global method (or use an enum, or something like that, maybe this is a use for something like the State pattern?) and store the different connection strings in the web.config file, and then create a base class which all data access classes derive from which provides a way to obtain the connection string for the current environment; this would then have to be set to production when the time comes to put the application live.

Is there another way? Most of the .NET MVC videos and articles I've seen don't even bother with separate environments but only use a development database and don't indicate how you do it in production.

like image 876
Wayne Molina Avatar asked Oct 23 '25 16:10

Wayne Molina


1 Answers

I'd say this is really a question of your company's internal processes. Since every company is a little bit different it's hard to have a "right" generic way to support dev/test/alpha/production and/or other environments.

One way: Create a setup program that supplies the correct connection string based on the environment chosen during the setup process.

Another way: System Admin edits web.config file to supply correct connection string during install.

Yet ANother Way: Connection strings are stored in the system registry.

Even Another Odd Way: You have all your connection strings for all environments in web.config, then a setting in appSettings the tells you which one to use.

Depending on the client, I've done all of these. There are more but these are the more popular.

(One client wanted to store the connecting string in the data base itself. Really.)

like image 67
No Refunds No Returns Avatar answered Oct 26 '25 06:10

No Refunds No Returns