January 2012 Blog Posts
Wednesday, January 25, 2012 2:02 PM | Comments

If you drink from the fire hose read Ayende’s blog you would notice a lot of Anti-Repository talk over the past couple years – which I fully agree with. Back in 2009 he declared repository is the new singleton, stating: My current approach for data access now is: When using a database, use NHibernate’s ISession directly Encapsulate complex queries into query objects that construct an ICriteria query that I can get and manipulate further When using something other than a database, create a DAO for that, respecting the underlying storage implementation Don’t try to protect developers Naturally he gets a slew of comments asking how he handles certain scenarios without using the repository...

Monday, January 23, 2012 3:58 PM | Comments

By using the the simple SelectorAttribute and EditorTemplate described in this post, you will get rich support the following very common scenarios (and flip between the various modes with ease): Single selection from a Drop Down Single selection from Radio Buttons Multiple selection from Check Boxes Multiple selection from a List Box Read-only mode combines multiple values into a comma-delimited string The Canonical example Say you’re inserting a new Product and need to set the CategoryId property. You make a simple input Model wanting to use EditorFor, but CategoryId gets rendered as a useless textbox. The challenge is: Retrieving the list of categories to present to the user Keeping the selected item(s) in sync with the...

Friday, January 13, 2012 2:40 PM | Comments

Many ASP.NET applications utilize the System.Web.Caching.Cache in some way. While it offers a pretty simple Dictionary-like API that your app can start using immediately, I typically create a combined “tell-don’t-ask” wrapper around it – which has some additional architectural benefits as well. Out of the box concerns A very common usage of the Cache API can be seen below, but there are a few initial problems I have with it: Ugly, non-generic casting Manual null checks Duplicating the string for the key Lots of implementation details sprinkled around No out of box way to scope the cache. For example, cache a unique copy of the item for each User   public ActionResult Bad() { var firstVisit = HttpContext.Cache.Get("FirstVisit") as DateTime?; ...

Tuesday, January 10, 2012 10:12 AM | Comments

Many line-of-business applications contain dozens of forms similar to the following, each field consisting of a few common characteristics: A Label with the name of the field The field editor itself Asterisks and special styling for required fields A tooltip that can be hovered for a detailed description of the field Validation messages if the input is incorrect To achieve this, we can create the following Model public class ProductInput { [HiddenInput(DisplayValue = false)] public int Id { get; set; } [Required] public string Name { get; set; } [Required] [Display(Description = "A brief description of the product")] [DataType(DataType.MultilineText)] public string...