Skip to content
This repository was archived by the owner on Jul 30, 2018. It is now read-only.
Necroskillz edited this page Apr 19, 2011 · 11 revisions

There are a lot of utilities to generally help with development, prevent reinventing the wheel, and provide better testability.

Guard

Very basic utility everyone should be used to. Contains some checks against nulls. If something is null, this throws appropriate exception.

Guard.IsNotNull(obj, "obj")
Guard.IsNotNullOrEmpty(str, "str")

404 handling

this will be removed with MVC 3, which will provide built-in way of handling this

Few steps are necessary to make this work. First create a view called 404.aspx in Views/Shared directory (you can actually name it whatever you want, but this is the simplest way).

Then decorate all controllers that will be using this feature with [Handle404] attribute (alternatively you could specify view name, if it’s not 404.aspx[Handle404(View = "Your404View")]).

Finally, when you want to redirect someone to you 404 error page, just throw an instance of Http404Exception. Because you usually want to do this when something is not found, I added method for this to Guard class.

var product = _productRepository.Get(p => p.Id == id);
Guard.IsNot404(product); // if product is null, redirect to 404 page

Data providers

Local data

Very useful class for storing data needed within one request globally. This may be user object you get from membership or other object with request scope lifetime. I originally got code for this from this article.

Use it like a Hashtable

Local.Data["Key"] = value;
int value = (int)Local.Data["Key"];

I also outfitted this class with ChangeContext() method that allows you to supply your own implementation of ILocalDataProvider interface. You can use this in unit tests (simply implement ILocalDataProvider that doesn’t work with HttpContext, but stores data to local dictionary).

Session data

Same API as Local data, only stores data in Session object.

Cookies data

Same API as Local data, except is reads data from inbound cookies, and writes to outbound cookies.

Slug generator

There’s just one method called Generate() on this class. All it does is strip all symbols from input string, convert it to lowercase, and replace spaces with dashes. Basically it does this conversion:

Post about some VERY important stuff! -> post-about-some-very-important-stuff

This is useful for putting important data (like title of a blog post, or name of a product) in URLs.

NoCache attribute

Simple attribute that is put on action method tells that action method to add headers for no caching to the response.

Clone this wiki locally