Jun 9, 2008

Good code I would like to be used...

Hm, I reallly like this code but it seems that they will never use it. I am a PM, they are developers. Saddly...

public class Actions
{
private delegate void ChangeState(License license, NewLicenseState newState, string reason);
private IDictionary actions;

public Actions()
{
actions = new Dictionary();
actions.Add(
ChangeToExpiredBlockedStateException,
delegate(License license, strin reason)
{
license.ChangeState(5, reason);
});

actions.Add(
ChangeToExpiredInUseStateException,
delegate(License license, strin reason)
{
license.ChangeState(4, reason);
});
}

public ChangeState this[LicenseException exception]
{
get
{
if (this.actions.ContainsKey(exception.GetType())
{
return this.actions[exception.GetType()];
}

Assert.ThrowInvalidOperationException(message, params args);
}
}
}


And then use it as follows:

new Actions()[ex.GetType()](license, ex.Message);

Yeah, I wrote this code as a sample. To bad it seems too complicated. Although I like it, they do not.

No comments: