Skip to content

Instantly share code, notes, and snippets.

@monoman
Created March 16, 2012 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monoman/2053145 to your computer and use it in GitHub Desktop.
Save monoman/2053145 to your computer and use it in GitHub Desktop.
Some things that could make ASP.NET MVC 4 Web API even better
// this offends my coding sensibility
public Contact GetContact(int id)
{
Contact contact = _repository.Get(id);
if (contact == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return contact;
}
// most of it could become a extension method
public static T VerifiedToExist<T>(this T it)
{
if (it == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return it;
}
// so the action would become
public Contact GetContact(int id)
{
return _repository.Get(id).VerifiedToExist();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment