Extension methods to improve readability
A common reason to take advantage of extension methods is to enhance readability (think fluent interfaces). My team uses the specification pattern regularly and in case a requirement says something like “if the player has reached level 10 a message should be displayed” they would implement it as
if (new HasReachedLevel(10).IsSatisfiedBy(player))
{
view.DisplayMessage("Congratulations! You have reached level 10.");
}
Pretty good but did you notice that they changed the order of player and level in their (code) story? With the aid of an extension method we can express this requirement as
if (player.Satisfies(new HasReachedLevel(10)))
{
view.DisplayMessage("Congratulations! You have reached level 10.");
}
Here is the extension method that allows you to express the requirement in this way
public static bool Satisfies<t>(this T candidate, ISpecification<t> specification)
{
return specification.IsSatisfiedBy(candidate);
}