My new blog

Posted by ben 13. October 2011 11:07

Hello readers!

Just to let you know I've launched a new blog (on fabrik) where I'll be posting from now on. I'll be leaving this blog online as it still gets a lot of traffic and cross-posting (and updating) some of my more popular posts.

BlogEngine has served me well up to now but since I built fabrik, it has lots of features that I prefer such as Markdown support and better code formatting.

The new blog is at http://ben.onfabrik.com. Enjoy!

Tags:
Categories

fabrik goes live

Posted by ben 10. August 2011 07:19

Yesterday the beta of fabrik went live. It’s taken us a little longer than expected, but we think it was well worth the wait. From the official announcement:

Fabrik is an online portfolio and blogging service, built on the real-world requirements of creatives. Our goal is to make it as easy as possible for you to showcase your work online. We take care of the technical stuff and you take care of the content. Simple.

We created fabrik because we felt that there was a genuine need for it. Over the past few years we've created a number of standalone portfolio sites (you may have seen a few of them). As we worked with more and more creatives we were able to get a real understanding of what you wanted from a web site. The product of this is fabrik.

In addition to providing you with a comprehensive portfolio and blog, we also host your site for you. In less than a minute you can have your work online - no complicated set up, no messing around trying to find web hosting, no hassle.

You can see a demo of fabrik running at http://demo.onfabrik.com. If you’ve subscribed to the beta you will find loads of resources such as how-to guides at http://www.onfabrik.com.

If you’re interested in taking part in the beta, just go onto our twitter page and PM us with your email. We can then send you a beta access code.

From a technical perspective fabrik has been a real experience for me. It’s the first production application that we’ve launched on the Windows Azure platform. In fact, I’m pretty sure it’s the first portfolio and blogging service running on Azure full stop.

Over the next few weeks I’ll be blogging about the experience as well as releasing a few open source libraries that we created along the way to make “cloud” development easier.

It does give me great piece of mind knowing that fabrik is running on such a solid infrastructure, and that I can scale my resources up almost infinitely, should I need to.

There are a few learning curves you have to go through, but overall it’s quite a nice end-to-end experience for developers. My biggest negative at this point is deployment, but I’ll be discussing this in a future post, and how it can be improved.

For more information about fabrik you can email support@getfabrik.com.

Tags:
Categories fabrik | Azure

Testing data annotations and asp.net mvc controller actions

Posted by ben 5. July 2011 19:10

If you have developed asp.net mvc applications you will probably be familiar with the following code:

	[HttpPost]
	public ActionResult DoSomething(SomeModel model)
	{
		if (ModelState.IsValid)
		{
			// do something then redirect
			return RedirectToAction("Index");
		}

		return View(model)
	}

If our input model is valid then we typically perform some action with it, then redirect (following the GET, POST, GET principle).

If it’s not valid, then we return to the current view passing the posted model.

Since most of my logic is abstracted into other services, I don’t tend to test my controllers that often. However, in case you do, you would want to test the type of action result based on the input model.

Typically we use the MvcContrib.TestHelper library for this. However just creating an “invalid” model and passing it to your controller action isn’t enough if you are using DataAnnotation based validation. This is because in a normal request, the validation is invoked by the appropriate ModelBinder. If you are calling your actions directly in your tests, then no Model Binding occurs, and therefore your ModelState will always be valid.

As the assertion I want to make is quite simple, I found the simplest approach is to first validate my model, then manually set ModelState before testing my result (much easier than mocking ModelBindingContext).

To test the DataAnnotations I wrote the following helper (you’ll need a reference to System.ComponentModel.DataAnnotations):

	public static bool ValidateModel(object model) {
		var validationResults = new List<ValidationResult>();
		var ctx = new ValidationContext(model, null, null);
		return Validator.TryValidateObject(model, ctx, validationResults, true);
	}

We can then test our controller actions like so:

	[Test]
	public void Register_with_valid_post_returns_redirect()
	{
		var model = new AccountRegisterModel { Email = "a@b.com", Password = "foobar", Url = "ben.onfabrik.com" };
		TestHelper.ValidateModel(model).ShouldBeTrue();

		accountController.Register(model).AssertActionRedirect();
	}

	[Test]
	public void Register_with_invalid_post_returns_view()
	{
		var model = new AccountRegisterModel();
		TestHelper.ValidateModel(model).ShouldBeFalse();

		// invalidate modelstate
		accountController.ModelState.AddModelError("", "");

		accountController.Register(model)
			.AssertViewRendered().ViewData.ModelState.IsValid.ShouldBeFalse();
	}

I’m not advocating testing every controller action, in fact, one could argue that if you feel the need to test your controllers then perhaps you have too much logic within them. Definitely check out “Put your controllers on a diet” by Jimmy Bogard - http://tekpub.com/conferences/mvcconf.

In fact after writing the above code, I abstracted most of my logic into handlers (see Jimmy’s video) and ditched the tests, but knowledge is power and sharing is caring so there you go.

Tags: ,
Categories ASP.NET | C#