ASP.NET MVC - How to make code behave differently when running locally vs. in QA vs. in Production

Question: How to make code behave differently when running locally vs. in QA vs. in production?

Example: In an ASP.NET MVC application I have a controller set up to deliver e-mail notifications. When running locally on a development machine I want the e-mails delivered to the developer, when in QA I don't want any e-mail notifications going out and in Production I want the notifications going out to their intended recipients

Thanks

Answers


Have three different web.configs and add an AppSetting which tells you where you are so you can determine if you should send an email.

You could also define constants in your web.config using the CompilerOptions attribute:

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp" extension=".cs"
      type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0,
      Culture=neutral, PublicKeyToken=b77a5c561934e089"
      compilerOptions="/d:Test"/>
  </compilers>
</system.codedom>

And in your code use

#if !Test
    SendMail();
#endif

This sounds like something you should do using an IoC. I typically use StructureMap which would allow me to setup different profiles. Then all I would have to do is have a web.config switch configured to set the appropriate environment profile.

For example, in StructureMap you could do the following:

ObjectFactory.Initialize( x => {
    x.CreateProfile( "Development", p =>
    {
        p.Type<IEmailProvider>().Is.OfConcreteType<DeveloperEmailProvider>();
    } );

    x.CreateProfile( "QA", p =>
    {
        p.Type<IEmailProvider>().Is.OfConcreteType<NullEmailProvider>();
    } );

    x.CreateProfile( "Production", p =>
    {
        p.Type<IEmailProvider>().Is.OfConcreteType<ProductionEmailProvider>();
    } );

} );

ObjectFactory.Profile = ConfigurationSettings.AppSettings["Profile"];

Need Your Help

Quartz string for a schedule which includes execution minute as well

c# quartz-scheduler quartz.net

I am trying to construct a quartz string for below schedule. Can some one help me in getting this done?

ArgumentException when adding many coordinates on the map

c# map windows-phone-8 location

I would like to add some pushpins that can be clickable on the map. First, I would like to display them but when I add them on a map, an ArgumentException is occured and my application crashes.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.