Monday, July 06, 2009

An Opinion on Repository

First, what is a repository?

…Therefore, use a Repository, the purpose of which is to
encapsulate all the logic needed to obtain object references. The
domain objects won’t have to deal with the infrastructure to get
the needed references to other objects of the domain. They will
just get them from the Repository and the model is regaining its
clarity and focus.

Excerpt from Domain Driven Design Quickly

So what the author is saying is that the repository’s (single) responsibility is to encapsulate the logic for obtaining object references. What that means to me is that the class utilizing the repository should be querying it in clear terms, such as:

_repository.GetPendingOrdersFor(customer);

This interface keeps the behavior of the method calling the repository focused on it’s job, and eliminates querying logic. Not only that, but the intention of the code is clear and even human(non-developer) readable.

The alternative is:

_respository.Query<Order>(o => o.CustomerID = customer.CustomerID && o.Status == OrderStatus.Pending);

Here’s how these options would look as an interface.

repository

Just one man’s opinion. What do you think?

Wednesday, July 01, 2009

ASP.Net MVC: Intro To MVCContrib TestHelpers

MVCContrib contains an assembly specifically built to provide help when unit testing ASP.Net MVC controller actions.  This is a very condensed guide to what you can accomplish using each helper method.

Test View Rendering

Test that a view is being rendered from the action:

_controller.List().AssertViewRendered();

…for a particular view

_controller.List().AssertViewRendered().ForView("SomeViewName");

…with a view model of the correct type

_controller.List().AssertViewRendered().ForView("List").WithViewData<SomeType>();

Test PartialView Rendering

Test that a partial view is being rendered from the action:

_controller.ShowAsteroid().AssertPartialViewRendered();

…for a particular view

_controller.ShowAsteroid().AssertPartialViewRendered().ForView("Asteroid");

…with a view model of the correct type

_controller.ShowAsteroid().AssertPartialViewRendered().ForView("Asteroid").WithViewData<AsteroidModel>();

Test Redirects

Assert that the action redirects to the correct action:

_controller.AddFormStar().AssertActionRedirect().ToAction("List");

…for an action on a different controller:

_controller.AddFormStar().AssertActionRedirect().ToController("SomeController").ToAction("List");

Easily Stub HttpContext Variables

The TestHelper library contains a class (TestControllerBuilder) that builds up a complete context for your controller to operate within.  This gives you the ability to easily mock anything that you would normally access from HttpContext.  Here’s an example of usage:

setup

The _builder variable is what contains all of the dictionaries for Session, ViewData, etc. that you can use to set up all of your stubs.  Example:

test

The code in the ActionUnderTest method will now be able to access all of the variables just set up in the test.  Couldn’t be easier!

Wednesday, June 24, 2009

Tip For Keeping ASP.Net MVC Views Easy To Read

ASP.Net MVC views are harder to read when they’re built with a lot of escapes between script and HTML, such as:

escapes

There’s really just no good way to make that easy to read using this approach.

Using the MVC Futures Repeater Control

Server controls and MVC just don’t sound like they’d go together, but if used for good I believe they can really serve a useful purpose. The following code snippet achieves the exact same result as above, but reads much cleaner:

repeater

The MVC Futures repeater control gives me a way to display repeating data with a template. The control is “bound” to the Model.Items collection because I set the name of the repeater to “Items”.

To get a reference to this control you need to include a reference to the MVC Futures assembly in the web.config:

reference

The complete solution with this code can be downloaded here:

Monday, June 22, 2009

Run Your Tests With A Keystroke

Today I was looking for a way to run my unit tests with a keyboard shortcut. I got really tired of grabbing the mouse just to click a button (wow I’m lazy):

testmenu

I knew there had to be a way to kick off a single test using ReSharper – and after a little digging I found the solution.

keyboardsettings

The magic lies in these 4 keyboard mappings. Assign them to something that makes sense to you, and enjoy the benefit of removing just a little more friction from your TDD experience.

Previous ReSharper keyboard shortcut tips:

Part 9 – Help When Yellow Screens Happen
Part 8 – Bird’s Eye View of Class Files
Part 7 – Add New Files Quickly
Part 6 - Move Extracted Interfaces to Their Own File using ReSharper
Part 5 - Find the Next Error Using ReSharper
Part 4 - What To Pass?
Part 3 - Surround Your Code
Part 2 - Find Inheritors
Part 1 - Quick Documentation View

Thursday, June 18, 2009

Resharper Help When Yellow Screens Happen

Ran across a great utility in Resharper to help with code navigation. It comes in particularly handy when you come across one of these:

pwnd

To get your app back to working order, Resharper will analyze the stack trace and then show hyperlinks so you can jump into the code that dealt the problem. Here’s how:

stacktracecopy

Now go back to Visual Studio and launch the Resharper feature (Intelli-J bindings use CTRL+SHIFT+E, Visual Studio bindings use CTRL+E, T).

When you launch the Stack Trace Explorer, your copied stack trace will already be inserted into the window and all of your user code class names/line numbers will be turned into hyperlinks. You can now easily click through to your code to see what’s up:

click

Previous ReSharper keyboard shortcut tips:

Part 8 – Bird’s Eye View of Class Files
Part 7 – Add New Files Quickly
Part 6 - Move Extracted Interfaces to Their Own File using ReSharper
Part 5 - Find the Next Error Using ReSharper
Part 4 - What To Pass?
Part 3 - Surround Your Code
Part 2 - Find Inheritors
Part 1 - Quick Documentation View

Monday, June 15, 2009

Rendering A Modal Dialog with ASP.Net MVC

One of the most requested ‘Web 2.0’ features by users is the ability to pop up a “modal” dialog to the user.  There are a couple ways you could implement this functionality, but I’m going to show you a really easy way to do it with ASP.Net MVC.  (Hat tip to Jon Kruger for sharing this idea with me.)

Start with a very simple controller action:

controlleraction

Nothing spectacular going on here.  The “RandomModal” string points to a ASCX file that is in the views directory, and the second parameter is the model that you want the partial view to render:

ascx

And finally, on the view that is hosting the modal dialog there is a chunk of javascript that makes the call, throws the returned DOM elements into a parent div, and displays the div as a dialog:

javascriptcallingaction

Another place you could implement this pattern is on a grid.  A common request is to click on a row and view more detailed information about the record.  To implement this all you’d need to do is stash a row identifier on the html row, and pass that back to the controller on click.  Something like this:

postforrowdetail

The complete solution with this code can be downloaded here:

Saturday, June 13, 2009

Resharper: Bird’s Eye View Of Class Files

When I’m in a unit testing class file, sometimes I have multiple classes in the same file and the file gets pretty large.  It’s times like these when the file structure navigation shortcut comes in handy.  For users with their keybindings set to the Visual Studio scheme, the shortcut is CTRL+ALT+F.  If you’re using the Intelli-J bindings, it’s CTRL+F11.

FileStructure

Previous ReSharper keyboard shortcut tips:

Part 7 – Add New Files Quickly
Part 6 - Move Extracted Interfaces to Their Own File using ReSharper
Part 5 - Find the Next Error Using ReSharper
Part 4 - What To Pass?
Part 3 - Surround Your Code
Part 2 - Find Inheritors
Part 1 - Quick Documentation View