Monday, March 11, 2013

Single Action Controller: more improvements

It's been more than a year since the last entry and I've done more improvements to the implementation of Single Action Controller in TEAM.Commons.Web. Most of the improvements have been around removing magic strings and using types instead.

The following gist shows some examples of what you get:
// Create a link to SingleActionController
@Html.ActionLink("Link text", typeof(Type.Of.The.SingleActionController))
// Create a link to SingleActionController with a request and attributes
@Html.ActionLink("Another link text", typeof(Type.Of.The.SingleActionController),
routeValues: new ShowListRequest
{
Property1 = Model.Property1,
AnotherProperty = Model.AnotherProperty
},
htmlAttributes: new { id = "thelink", @class = "btn" })
// Generate a URL
<form action="@Url.Action(typeof(Type.Of.The.SingleActionController))"
// Render an action
@Html.Action(typeof(Type.Of.The.SingleActionController))
view raw gistfile1.cs hosted with ❤ by GitHub


If you like type safety and finding errors at compile time rather than build time, you're going to love this approach, together with enabling view compilation.

On top of that I've also added some methods to help with testing of applications built with this library. This is how you verify that an action returns a View:
// Verify that a Single Action Controller returns a View
var sut = new Some.Controllers.ShowList(with, its, dependencies);
var request = new ShowListRequest();
var result = sut.Execute(request);
result.AssertIsView()
.WithName("_ListPartial")
.WithModel<ListViewModel>(p =>
{
p.Property1.Should().Be(1);
p.SomeList.Should().HaveCount(1);
});
view raw gistfile1.cs hosted with ❤ by GitHub


And this is how you check that an action returns a redirect to another Single Action Controller:
// Verify that a controller returns a redirect to a Single Action Controller
result.AssertIsRedirectToRoute().WithSingleActionController(typeof(ShowList));
view raw gistfile1.txt hosted with ❤ by GitHub

No comments: