Stefan Says

Opinions on ASP.NET, ASP, databases etc
posts - 34, comments - 25, trackbacks - 23

The danger of configuration instead of code

I have a strong opinion regarding code vs configuration files  - in short I say things that could be done programmatically instead of by the design time visual controls - should be made programmatically: that idea was the initial reason for my In Practice article serie. 

Same thing kind of applies to config files. Don't get me wrong - what I mean is - yes DO use config files - but just use it for simple settings - you are in trouble if you start relying on framework code and framework behaviours based on the settings you've made.

I am not perfect myself (far from) - but however since life is about learning, I'll give you one example from one of my first ASP.NET 2.0 membership project I created. In my old ASP.NET 1.1 code I use this function for sending mails from within my apps and typically store things like port no, account, password etc in web.config or a databse table or something.  The important thing is - I use my own wrapper SendMail function from all my aspx pages I want to send email from = one single spot to change if I ever need to.

Now, in ASP.NET 2.0. there are a lot of new web.config configuration sections available. One is for email setup, looking like this

 <system.net>
  <mailSettings>
   <smtp from="webmaster@thedomain.com" >
    <network host="mail.domain.com" port="25"
             userName="webmaster@thedomain.com"
          password="whatever"/>
   </smtp>
  </mailSettings>
 </system.net>

That was nice I thought - and when I got into the different controls triggering email sending -  such as asp:PasswordRecovery I was so excited to see the MailDefinition BodyFileName property - allowing me to specify an template file containing the body text - and I didn't even have to write a single line of code! The control automatically used the web.config settings - and the MailDefinition BodyFileName - and suddenly: email was sent! Not a single line of code. Here's my mistake: I thought it is was COOL! - and went on doing the same for CreateUserWizard etc.

Solution was deployed and everything is running smoothly -  until now that is. Now they need to use another SMTP server - one with SSL authentication. And - in ASP.NET 2.0 you  can't specify SSL authentication with just web.config changes... I need to do some code changes.

The thing is - for my ASP.NET 1.1 apps - it just took a minute (example code here). I knew I used my helper function whenever I was sending an email. For my 2.0 apps - the fix will take some time. While it is easy to override the SendingMail event - my trouble is finding all these spots - basically I need to test the whole app from start to end. 

So, in short - don't get too excited about all the settings and "automagic" you can get in ASP.NET 2.0. 100 times of 100 I prefer debugging code instead of trying to search in documentation if a certain config section can be tweaked to support a specific feature. Always wrap functionality you use from multiple places - no matter how simple the call is. You will gain from it someday if you do - that day is today for me - and I'm telling you I didn't gain - for me it was more like pain...

 

Print | posted on Saturday, September 30, 2006 7:45 PM