{ independency injection }

I am a recent fanboy of AOP (aspect oriented) design patterns, so maybe this isn't anything new to you, but MAN it's changed my life. Staying hard-and-fast on the "DRY" principle (don't repeat yourself) is an awesome concept, but it isn't always easy. Take for instance the "Try/Catch/Finally" block. When accessing external resources, you sometimes need to wrap each call in a this block to manage the resource in the event of a failure. Something like, the following:

publicvoid DoSomething()

{

try

{

// Perform the action

_service.Do();

}

catch (Exception exc)

{

// Log exception, etc.

Log(exc);

_service.Abort();

}

finally

{

// Clean up resources

_service.Close();

}

}

Now it isn't terribly obvious how you can get around this (at least it wasn't for me). If only we could have every call to this service wrapped in the same try/catch/finally block... Enter the Action<T> delegate. With this guy, you can create a method within this class (or a base blass) that provides this block as a wrapper for your action. For instance:

privatevoid Invoke(Action<IService> action)

{

try

{

// Perform the action

func(_service);

}

catch (Exception exc)

{

// Log exception, etc.

Log(exc);

}

finally

{

// Clean up resources

_service.Close();

}

}

Then, you could make your calls like through the "Invoke" method and everything would be wrapped in the common Try/Catch/Finally block. Check it out:

publicvoid DoSomething()

{

Invoke(s=>

s.Do()

);

}

It's that easy! Now, if you are going to be returning values, then you may want to create an overload using the Func<> delegate, like this:

When dealing with Enums (parsing, getting values, etc.) there is entirely too much casting and Types being passed around. For instance, to simply parse a string to it’s Enum value, one would do this:

TestEnum testEnum = (TestEnum)Enum.Parse(typeof(TestEnum), "Super");

We had to define the enum type THREE (count them) THREE times, in a single row. Blech! With this helper, this statement becomes much cleaner:

var testEnum = Enum<TestEnum>.Parse("Super");

Only ONE declaration of the Enum type. Nice! (Okay, so I cheated and used the 'var' keyword, but you get the point)

Also, there are some really handy helpers built-in, like:TryParse - four overloads, two returning a bool with an out parameter and two that just returns NULL if it fails to parse.ToEnumerable - returns a typed-list of all the Enum options

Example:

// Fails parsing, enum value = null

var enumTest = Enum<TestEnum>.TryParse("WrongValue");

// Get an IEnumerable of the Enums

var list = Enum<TestEnum>.ToEnumerable();

Plus more goodies -- feel free to take a look.

Now, I am certain that I am not the first to create a generic helper to make parsing Enums easier (article one, article two to name a few) but here is a complete Enum<T> helper class for all your Enum needs. Note, I even recreated all the standard static methods available in Enum, so that there will be no need to pass the “typeof(X)” parameter.