• Repository and Unit of Work for Entity Framework (EF4) for Unit Testing

    Tags: Entity Framework, c#, linq, EF4

    If you are not familiar with the Unit of Work pattern you should firt look at Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application (9 of 10).  It's a great article that demonstrates what the Unit of Work pattern can do.

    Unfortunately, that article uses a different version of Entity Framework.  I am using the default tools that come with Visual Studio and didn't want to modify the project too greatly so for me, the code wasn't usable.  I also wanted to expose the IQueryable interface which as a side effect also simplified my Repository design.

    By using a Repository Pattern you'll find unit testing is now possible and quite easy to write.  This illustration (taken from shows the differences between No Repository and the Repository Pattern.  Notice how there are no Unit Tests in the No Repository section also note the "Alternative Persistence Medium" node (this is where our MemoryRepository comes in).


  • Generic IQueryable Repository for Entity Framework 4 (EF4)

    Tags: c#, linq, Entity Framework, EF4

    Anyone who has tried to create tests with Entity Framework (EF) knows that EF is not very unit test friendly. I needed something simple, reusable and unit testable. this generic repository was the result.

    The goal of this project was high reusability, ease of implementation and to be unit tests friendly.

    There are a lot of Repository Pattern's out there that hide a lot of the underlying functions of your data access layer (in this case LINQ). Well, I really like LINQ to SQL and didn't want to hide any of that. Plus, if you really need encapsulation it should be done in your service layer and not your repository layer.


  • Setting up Visual Studio 2010 and IIS Express

    Why use IIS Express over the Visual Studio Development Server (Cassini)?

    I've seen Cassini run inconsistently on different machines, which resulted in the developers having to setup IIS on their local machines which also didn't end up working because they weren't all running the same OS.

    Cassini doesn't support the <system.webServer /> node in your config file, which can also give inconsistent results when deployed.

    Here are the reasons Scott Guthrie gives:

    • It’s lightweight and easy to install (less than 10Mb download and a super quick install)
    • It does not require an administrator account to run/debug applications from Visual Studio
    • It enables a full web-server feature set – including SSL, URL Rewrite, Media Support, and all other IIS 7.x modules
    • It supports and enables the same extensibility model and web.config file settings that IIS 7.x support
    • It can be installed side-by-side with the full IIS web server as well as the ASP.NET Development Server (they do not conflict at all)
    • It works on Windows XP and higher operating systems – giving you a full IIS 7.x developer feature-set on all OS platforms


  • Data, Context and Interaction (DCI) & Windows Workflow (C#)

    Tags: DCI, Workflow, c#

    Disclaimer: This article assumes you already know the following: C#, WF4 and Extension Methods.

    Lately I have been reading a bunch about Data, Context and Interaction (DCI) and a lot of my current project have involved using Windows Workflow Foundation (WF4).  Recently I've come to the realizaion that a WF4 Activity can also be the perfect DCI context because both are basically use cases.

    I've taken some sample code written by Christian Horsdal about DCI in C# and applied to to a Workflow.  He's has some very good posts about DCI and C#, I'd recommend checking them out if you want to learn more.


  • Unobtrusive Data-Binding for Knockout.js

    Tags: knockout.js, data-binding, javascript

    If you're a web developer who has yet to see Knockout, go take a look. I'll wait... Knockout is an incredible library, I just love it. Though I prefer to keep my presentation layer clean and now it's sprinkled with little bits of code for the data-binding. And that is how this Plugin came to be. It solves that problem with an easy way to Unobtrusively Data-Bind your presentation layer.


  • MiniLinq for Script#

    Tags: script#, c#, javascript, linq

    Script# is my new favorite piece of technology. Write JavaScript with C#? Yes please! But since it's (currently) limited to ISO-2 C#, it can feel like you're stuck back in 2004. Using C# 2 means you're missing a lot of things you use daily... like LINQ. I do miss LINQ. So I read about Script# adding a few LINQ like extensions to the array class and decided to work them together into this MiniLinq class, which I am now sharing with you.


  • Logging Errors with ELMAH in ASP.NET MVC 3 – Part 5 – (JavaScript)

    Tags: ASP.NET, MVC, Logging, Error Handler, Elmah, javascript

    Now we need a way to track those pesky client side error messages. Tracking the server side errors is the easy part, but what about those browser errors that go unnoticed and unfixed?

    First, create the Server Side Handler

    To handle and log JavaScript errors, we'll first need to setup some server side code. The client (browser) will pass these errors to the server, which will hand them off to ELMAH for tracking. So let's start by creating a new Exception type for our JavaScript exceptions. This will allow us to differentiate the server errors from the JavaScript errors more easily. It doesn't need much so this should be fine...


  • Logging Errors with ELMAH in ASP.NET MVC 3 – Part 4 - (HandleErrorAttribute)

    Tags: ASP.NET, MVC, Logging, Error Handler, Elmah

    Creating a custom HandleErrorAttribute for ELMAH (Optional)

    Remember when we commented out that line from our Global.asax.cs in Part 1? Well, building our own HandleErrorAttribute will allow us to put it back or more importantly prevent the HandleErrorAttribute from overriding our ELMAH behavior. Currently the HandleError attribute will prevent any errors from bubbling up to ELMAH and we can't guarantee down the line, someone won't use a HandleError attribute like this...


  • Logging Errors with ELMAH in ASP.NET MVC 3 – Part 3 – (Filtering)

    Tags: ASP.NET, MVC, Logging, Error Handler, Elmah

    Filtering Unwanted Errors

    Ok, the reason I really like receiving an email for the every error message is because it will get annoying really fast. This results in me fixing the errors really fast. But there are just some errors that you just can't (or don't want to) "fix". If you noticed in Part 1, the error page contained some 404 errors for /favicon.ico. Errors like these will get annoying really quickly, and fortunately there's an easy way to filter them out. This chunk of code will filter out all 404 errors with a URL matching /favicon.ico.


  • Logging Errors with ELMAH in ASP.NET MVC 3 – Part 2 – (Notifications)

    Tags: Elmah, ASP.NET, MVC, Logging, Error Handler

    Setup Notifications for ELMAH Errors

    Now that ELMAH is setup, you need a way to be notified when an error occurs. Though you can just subscribe to your ELMAH RSS feed at /elmah.axd/rss, I prefer to receive an email notification as soon as an error happens. To do this simply merge the following changes into your web.config's <configuration> section.


  • Getting ASP.NET MVC 3 working on

    Tags: ASP.NET,, MVC

    Though this article should apply to any IIS7 shared-hosting platform, I can only confirm it works on

    Getting Errors After Deploying your Application?

    After deploying your application, you might see the following error message...

    Could not load file or assembly 'System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

    This is due to the MVC binaries not being installed in the GAC on the server. But do not fret, the GAC is only optional for MVC 3!


  • Akismet .Net 2.0 API (anti-comment-spam)

    Tags: .net, spam, akismet, c#

    It looks like spammers are set on ruining the entire internet. Not only do I get 1000's of unwanted emails every day, but now spammers are starting to pollute blog comments.

    Fortunately the guys over at Akismet have developed an anti-comment-spam web API. There are multiple libraries that have been developed for Akismet, though none for .Net. Since I am going to be implementing this on, I figured I would just wrap it all up into an easy to use library for me and you too!

    Download source project

  • US has a 90% Chance of Economic Doom.

    Dollar / Euro Exchange Rate There have been a lot of economic forecasts floating around the news lately; very few of them seem to have a positive outlook on the future. Morgan Stanley's (investment banking giant) chief economist, Stephen Roach is giving us a 10% chance of getting through this OK, citing our record US trade deficit being the cause.

    From 1992 and 1997, the US trade deficit almost tripled. In 2002 the US trade deficit hit a record $418 billion; only to be outdone in 2003 when we hit another record deficit of $489.4 billion. Thats a 17.1% increase in just one year.

    Stephen Roach argues the dollar will continue to fall as a result of the U.S trade deficit, thus creating inflation. To combat inflation, the Federal Reserve will be forced to raise interest rates, higher and sooner than we or they would like. We've already seen the Feds increase rates 4 times this year.


  • Coercion - why we listen to what "they" say

    Tags: advertisting, coercion, books

    I picked up a new book, Coercion.  So far it's a pretty good book.  It talks about the ways the media, sales, and retail personel manipulate us without us being the wiser.

    In the section I just finished reading, a retired auto salesman is describing to the author a popular way to break through the buyers defenses and make them more responsive to coercion.

       As Miller explained, "Somewhere during that demo drive, while you're making your trial close -- not asking for the sale yet -- you ask him, in these exact words, 'Is this the type of vehicle you would like to own?' It happens.  And anyone will tell you this, the vacuum cleaner salesman, the carpet salesman - the customer has a split second of insanity.  The mind goes blank, the body paralyzes, the eyes get glassy, dilated.  And you'd be surprised how many people have an accident at just that moment!  Ask any car dealer.  We always joke about it."
       How could a single question provoke such an extreme response?  Partly because it relies on disassociation.  The customer is already in a vehicle, being asked to imagine himself owning the same type of vehicle.  It's the same as if I asked you if this is the kind of book you can imagine yourself reading.  Your current situation is reframed in fantasy.  It creates a momentary confusion, or disassociation, from the activity you're involved in.  That's why so many drivers crash.  They are no longer just driving the car but imagining themselves driving the car.  It is a momtary loss of awareness, during which the customer's defense mechanisms and rational processes are disabled.

    So far I think the book is great and would recomment it to everyone. I'll let you know more as I read on.