Project Description

Framework for managing error message strings in resource files and exceptions using enuemrations for message access.

Contents

Project Description Usage

Summary

This framework is a result of the way in which I tend to manage my error strings across my projects. The framework has several goals:
  • To provide a standard method of accessing error message strings in resources through enumerated fields (the default method in C# projects is to use a Resources class with methods corresponding to each string in a resource file)
    • Enumeration fields also store (track) error number information, which makes it impossible to assign the same error number to two enumeration fields within the same enumeration (but this is possible if two enumerations are used)
  • To reduce code clutter and readability
  • Allow standard method of displaying error message information that is common to all messages without having to include that information in every resource string (for instance, if I want all my messages to be prefixed by "CS1234: ", replacing 1234 with the error code assigned to the error, I should not have to put "CS1234: " into every single one of my resource strings, the framework ought to be able to handle that for me)
  • Allow strings to be shared across enumeration field defintions by sharing the same resource file and field name
    • Allow enuemration fields with duplicate names to have separate string definitions within the same resource file if desired

Example

namespace CodePlex.ErrorManagement
{
    enum ErrorCode
    {
        MissingStringDefinition = 1000,
    }

    public class foo
    {
        // This is throwing an exception the hard way
        public void Bar_Old()
        {
            string message = string.Format(CultureInfo.CurrentCulture,
                Properties.Resources.ResourceManager.GetString(ErrorCode.MissingStringDefinition.ToString(), CultureInfo.CurrentCulture),
                obj1, obj2);
            string errorMessage = string.Format("CS{0}: {1}", (int)ErrorCode.MissingStringDefinition, message);
            throw new NotSupportedException(errorMessage);
        }

        // This is thowing an exception using the assembly extension method
        public void Bar_New()
        {
            throw new NotSupportedException(
                ErrorCode.MissingStringDefinition.FormatErrorString(obj1, obj2));
        }

        // Or even more simply...
        public void Bar_Newer()
        {
            throw ErrorCode.MissingStringDefinition.Exception(obj1, obj2);
        }
    }
}

Formatting of error message strings is done through a single extension method and accessed through an enumeration (no need to keep const strings around).

Future

As a future exercise I hope to work on the following items for this project:
  • Performance improvements
  • Additional support for throwing exceptions
  • FxCop rules for validating usage of error messages through the framework
  • A custom resource editor for maintaining error strings and code generation of enumerations

Last edited Aug 28, 2009 at 9:17 PM by tulebast, version 10