Archives / 2011 / August
  • Moving Beyond Enums

    I just published an article on ASPAlliance on Moving Beyond Enums, describing when and how to move from enums to classes in your code when you start demanding more from your enums than they were designed to give.  Check it out and let me know what you think.  I also thought I’d post an alternate LINQ-ified version of the DisplayFriendlyNames() method I used in the article. Original, non-LINQ version: public static string DisplayFriendlyNames() { var sb = new StringBuilder(); foreach (Role role in Role.List()) { if (role.IsVisible) { sb.AppendLine(role.Name); } } … more

  • Working with SimpleMembership outside of ASP.NET

    I’m using SimpleMembership, from WebMatrix’s distribution(WebMatrix.WebData), with an ASP.NET MVC 3 application.  You can find the NuGet Package for SimpleMembership.Mvc3 here, and installing it is just a matter of running “Install-Package SimpleMembership.Mvc3” from the Package Manager Console in Visual Studio.  Unlike the built-in Membership and Role providers for ASP.NET, SimpleMembership doesn’t require huge swaths of XML to be added to your web.config file, or much in the way of database initialization using tools like aspnet_regsql.exe.  Instead, it does pretty much all of its work in code, in a static class SimpleMembershipMvc3 that gets placed in your App_Start … more

  • Validating Emails for System.Net.Mail

    If you’ve worked with the System.Net.Mail API to send out messages, you may have run into the fact that when you add an email address to a message, it will sometimes throw an exception if the email doesn’t appear to be valid: var myMessage = new MailMessage();myMessage.To.Add("ssmith@somewhere,com"); // invalid ','// do more stuff Result: FormatException - The specified string is not in the form required for an e-mail address. This is handy since it’s usually better to fail early rather than wait until you actually go to send the message to learn that the email is invalid.  However, if you’re processing more than one email, you likely have code that might look like … more