Making a Case for Code Reusability
Reusability is good. It's a simple statement but one that doesn't always register with some developers. You want to build things in such a way as to make highly repeatable processes available from multiple sources. Here's a good example. I needed to build a feedback form for a section of a corporate portal. It sends a formated email to a specific mailbox when submitted. I decided to make the email submission portion a Web service so we could use it again, if needed. The code is below. Sure enough, a few weeks later I was building an InfoPath form (same environment) that required email-based approval routing. The challenge was that this company is not using Outlook so InfoPath's native email capabilities don't work. Bang, I plugged in my Web service behind the Submit button and we were on our way. I'll write another post on this Web services connection process later. Remember, REUSABILITY = ACCELERATION.
[WebMethod]
public string SendEmail(string EmailFrom, string EmailTo, string EmailSubject, string EmailBody)
{
//set up email object and send message
MailMessage objEmail = new MailMessage();
objEmail.To = EmailTo.ToString();
objEmail.From = EmailFrom.ToString();
objEmail.Subject = EmailSubject.ToString();
objEmail.Body = EmailBody.ToString();
SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["SMTPServer"];
try
{
SmtpMail.Send(objEmail);
return "Success";
}
catch (Exception ex)
{
return "Failure";
}
}