- Posted by mateid on August 22, 2010
I think dynamic languages have some serious advantages when working with JSON over static languages. Things are just easier and there is a lot less friction. Static languages like C# make things a little cumbersome because you always need to specify a type. That is almost always. :) There is an alternative in C#, provided via anonymous types. You can deserialize to an anonymous type by giving the JSON deserializer and example object. (I’m using the fabulous JSON.net library for these samples) Let’s see what I mean:
public class JSON_to_anonymous_type
{
private string json = @"{ Name: ""James"" }";
[Test]
public void can_deserialize_to_simple_anonymous_type()
{
var example = new { Name = string.Empty };
var person = JsonConvert.DeserializeAnonymousType(json, example);
Assert.AreEqual("James", person.Name);
}
}
The JSON serializer will take your example anonymous object and attempt to use it as template to deserialize the JSON literal. This provides a nice alternative to defining a full blown C# class to deserialize to in some cases.
The example above is rather simplistic, usually the JSON literals we have to deal with in real life are a little more complex than this. Let’s look at something a little closer to a real life scenario. Say the person is married, and we’d like to include the spouse information with our JSON literal. We now need a nested anonymous type. The syntax can get a little crazy but here goes:
[TestFixture]
public class JSON_to_anonymous_type
{
private string json_moderate = @"{ Name: ""James"", Spouse : { Name: ""Sally""} }";
[Test]
public void can_deserialize_to_moderately_complex_anonymous_type()
{
var example = new { Name = string.Empty, Spouse = new { Name = string.Empty } };
var person = JsonConvert.DeserializeAnonymousType(json_moderate, example);
Assert.AreEqual("James", person.Name);
Assert.AreEqual("Sally", person.Spouse.Name);
}
}
Let’s try one more scenario – we would like to parse a list of children from the JSON literal. This is starting to become a little hard to read, but still better than having to define two static classes if all we need to do is say count the couple’s children.
[TestFixture]
public class JSON_to_anonymous_type
{
private string json_complex = @"{ Name: ""James"", Spouse : { Name: ""Sally""}, Children : [ { Name : ""Tom"" }, { Name : ""Sandra"" } ] }";
[Test]
public void can_deserialize_to_complex_anonymous_type()
{
var example = new { Name = string.Empty, Spouse = new { Name = string.Empty }, Children = new[] { new { Name = string.Empty }}};
var person = JsonConvert.DeserializeAnonymousType(json_complex, example);
Assert.AreEqual("James", person.Name);
Assert.AreEqual("Sally", person.Spouse.Name);
Assert.AreEqual(2, person.Children.Length);
}
}
I think that covers a couple of complex scenarios that you might encounter in real life. Deserializing to anonymous types can reduce the friction of declaring one or more static classes for simple scenarios where we may need to simply peak at a JSON literal to extract one or two properties, or perform some simple operation. If things get a little more serious than by all means declare a couple of static types to deserialize to, if only for readability's sake.
- Posted by mateid on August 11, 2010
I think I’ve posted before about build automation but I didn’t really stress how important this is for a dev team. This is probably the one thing that will support your team day in and day out. It will save you lots of tedious, manual labor and a lot of frustration when you integrate with other systems. Once you get your builds going on a build server like TeamCity visibility into issues increases dramatically. You can also expect issues to be resolved dramatically faster than before. One thing to remember is that this new increased visibility is not to give you ammo against you team mates, but to enable you to help them. The bottom line is that automating your build and deployments is well worth the time you’ll spend to set it up.
This will probably turn into a little series, but I’ll show how to set up a basic build script. I’ll be using MSBuild but the same concepts apply if you happen to be using nant. I used to prefer nant because I find it easier to work with. MSBuild has a somewhat cumbersome, awkward syntax but it seems easier to adopt in Microsoft environments.
I’ll assume a simple solution that consists of a web project, some library projects and at least one test project. The solution is self contained – in other words all the tools and libraries we need are in the solution folder. All we need to do is check out and off we go. This enables us to build without even opening studio. Our goal is to clean the solution, build it and run the unit tests. As you will see this is not very difficult to accomplish.
The basic building block for a MSBuild script is a target. A target is a group of tasks that will be executed sequentially in the order they are defined in your build script. A very basic build script can look like the one below:
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Build">
<MSBuild Projects="solution.sln" />
</Target>
</Project>
This a simple project that consists of one target – Build – which also happens to be the default target for the project. We save this as solution.build for example. Using the .build extension actually gives you intellisense when working with your build file in VS 2010. You can setup a batch file that will call MSBuild and execute the script we just created with the following line:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe solution.build %*
This will execute the default target in your build file – Build. The ‘%*’ means that any parameters that you pass to this batch file will be appended to the command line used to invoke MSBuild.
One thing that we would like to do is ensure a clean build. We need to clean the output folders of all assemblies that might be stale before we build our solution. We can easily do this by setting up a new target called Clean. In here we’ll define a list of files we’d like to delete using and ItemGroup. Once defined, the item group can be passed to the Delete task to clean our output folders. Your new build script will look similar to this:
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Build" DependsOnTargets="Clean">
<MSBuild Projects="solution.sln" />
</Target>
<Target Name="Clean">
<ItemGroup>
<BuildFiles Include="$(MSBuildProjectDirectory)\**\obj\**\*.*" />
<BuildFiles Include="$(MSBuildProjectDirectory)\**\bin\**\*.*" />
</ItemGroup>
<Delete Files="@(BuildFiles)" />
</Target>
</Project>
Here we’ve introduced a new target and leveraged a couple of important MSBuild features. We stated that the Build target depends on the Clean target. MSBuild will now ensure that the Clean target runs before the Build target. Inside the Clean target we used ‘$(MSBuildProjectDirectory)’ – this means we would like to use the value of the property ‘MSBuildProjectDirectory’. In this case it is a build in property but we’ll see shortly how you can define your own properties. We are now at a point where our script can perform a clean build of the solution. Time for a check in! In fact you can now start to leverage this build script on a build server like TeamCity where by defining some artifacts you can have your build output packaged and ready for deployment.
Finally we would like to control the active configuration for our project. We don’t necessarily want to package and ship debug assemblies! We can accomplish this easily by defining a property named Configuration in our build script. This will most often take one of two values - [Debug|Release]. We’ll pass the value of this property to our MSBuild tasks in our Build target. The build script will now look like this:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition="$(Configuration) == ''">Release</Configuration>
</PropertyGroup>
<Target Name="Build" DependsOnTargets="Clean">
<MSBuild Projects="$(ProjectName).sln" Properties="Configuration=$(Configuration)"/>
</Target>
<Target Name="Clean">
<ItemGroup>
<BuildFiles Include="$(MSBuildProjectDirectory)\**\obj\**\*.*" />
<BuildFiles Include="$(MSBuildProjectDirectory)\**\bin\**\*.*" />
</ItemGroup>
<Delete Files="@(BuildFiles)" />
</Target>
</Project>
We can now change the active configuration for our build simply by passing a command line parameter to our batch file.
build.bat /p:Configuration=Release
We now have a very basic build script that we can start using in a build server or from the command line. This pretty much concludes the first part of what is to become a series.
- Posted by mateid on August 9, 2010
As a follow up to my previous posts and the issues we’re dealing with on my current project let’s look at how we’re dealing with blockers.
First of all what are blockers? Things that prevent me from doing my work and moving the ball forward, things that make me waste the clients time and money. As of late there are a couple that have managed to sneak in and we’ve not dealt with them effectively. They weren’t blocking any high priority tasks but it is a somewhat unsettling trend. Perhaps that is why the visibility was so low, they were just flying under the radar.
During the standup we all say our piece followed by “no blockers” or “I am blocked by…” (Stating “no blockers” at the end of your update is a bit of a smell to me, it smells of agile implemented as a control mechanism but that’s a post for another day) So how is it than that we have issues that remain unresolved for extended periods of time? The must be a disconnect somewhere. The problem is that blockers were not assigned an owner. Lack of ownership is an issue we learned to deal with quickly.
Soon enough when a team member reported a blocker we started noting it on a sticky, and placed it in a column on the wall dedicated to blockers. We found that an approach that works reasonably well is having a team member own the issue. Just ask for volunteers during the standup! An expected resolution date associated with the item also helps drive the resolution or further escalation. At the end of the standup when everyone has given their update we go through the blockers and deal with them on a case by case basis. Obviously if discussions are taking a long time you can take it off line with a couple of teammates. We found that the increased visibility and daily attention helped us deal with various issues effectively and keep the dev machine from grinding to a halt.
We found that when dealing with blockers what works best is increasing visibility and ownership of the issue. Taking control of your blockers really helps to keep the project moving and it gives an tremendous opportunity to deal with tough issues early and effectively.
- Posted by mateid on August 8, 2010
I’ve been away on vacation for the past week and now I’m back to work. It’s amazing how refreshing a few days off can be, and I’m pretty pumped to be back on my project. Some time off also helps put things in perspective. I can think clearer and see some beginnings of anti-patterns that are making their way into our team’s daily activities. Some of them are mostly related to attitude rather than technical issue and they need to be stopped as soon as possible. I was lucky enough to be on a very difficult project recently where I’ve learned a lot, and I want to talk about how we used to deal with similar challenges. One sentence sums it up: don’t be dogmatic, be pragmatic.
We’re looking at a fairly small project, with lots of front end development using MVC2 and jQuery. Lately we started having some discussions (some of them very heated) about our test coverage, pairing, technical debt and so on. We are starting to fall into the dogmatic trap and I don’t think our project will benefit much from it. Some members of our team expect 100% test coverage, pairing all of the time and zero technical debt. Heh, one can dream! But remember there is a price to pay for these things, they don’t come free.
Take the test coverage for example – the team is somewhat split on this issue: some think that 80% is good enough, others insist that we must be close to 100%. Personally I find myself in the 80% camp. I think to expect 100% test coverage is unrealistic. Test coverage is just another tool to tell me if my tests are covering the code base adequately. If we’re hovering around 80% and I can refractor easily and the test suite is highlighting issues consistently and effectively I am happy. I’d rather spend my time delivering business value than refining my test suite. If on the other hand we’re having issues that go undetected until late in the QA cycle or worse, than yes I wholeheartedly agree we need to work on our test suite and perhaps improve the coverage in troublesome areas. But asking for 100% test coverage is dogmatic and not very helpful to the client. Ask yourself if setting unrealistic expectations for coverage is helping you deliver business value more effectively. When will the client benefit from the activities you’re engaged in and how much value are they going to get in the end? I think this is pretty much the ultimate measuring stick when working on an agile project.
Another good example is technical debt. Say we have about 10% of our code that is somewhat sketchy. It doesn’t really hurt us on a day to day basis but it definitely is high up on the refactoring list. What do you do? Spend the next three days refactoring it or pick up a story and deliver a couple more points? I know it can be a tough choice - the code going into disarray or delivering a few ore points. Here the team is again split. Some think the code is fine, others cry bloody murder, this abomination needs fixing now! Remember this particular piece of code is not hurting us right now. To me the smart and agile thing to do is take note the offending area and deliver business value. I’m not saying sweep it under the carpet! Just deal with the issue pragmatically. If you’re having a hard time justifying refactoring it now than make a low priority tech task and deal with it later. There will be ups and downs in the iterations and an opportunity to fix it will definitely open up.
Don’t be dogmatic, be pragmatic. There will probably always be those that ask for 100% test coverage, zero technical debt etc. Just remember that every project is different and pragmatism will serve you much better than a dogmatic attitude.
- Posted by mateid on July 20, 2010
I've broken this up into three parts:
The last feature that I want to add to my BeforeFilter is an exclusion list that specifies controller actions for which not to execute the filter methods. Assume that you have Edit/Create/Display views; the Edit and Create views could – for example - require additional data to render drop down lists. If we populate this data with a before filter we should have the ability to exclude the Display view from the filter where this additional data is not necessary.
Attribute parameters can only be constant expressions so we have that restriction to contend with. The usage should be friendly and easy to use – a little syntactic sugar doesn’t hurt. I was contemplating something similar to the snippet below:
[BeforeFilter("Initialize", Except = new [] {"Index", "Details"} )]
public class ItemController : Controller
{
...
}
This looks pretty straight forward and the implementation should not be too difficult.
How are we going to know the action that is about to be executed? The OnActionExecuting method is passed an ActionExecutingContext. It contains an ActionDescriptor object that has various interesting properties including the ActionName.
Naturally we begin with a test. Our test setup will changes slightly so that we can mock the ActionDescriptor and create the context with the mocked out version. The test will include a setup for the ActionName property and verify that the filter methods are not invoked for the actions specified in our list. The setup and the test code is below:
[SetUp]
public void Before()
{
controller = new Mock<TestController>();
descriptor = new Mock<ActionDescriptor>();
context = new ActionExecutingContext
{
Controller = controller.Object,
ActionDescriptor = descriptor.Object
};
}
[Test]
public void should_not_call_specified_controller_method_given_an_action_is_an_exception()
{
descriptor.SetupGet(x => x.ActionName).Returns("Display");
var attribute = new BeforeFilterAttribute("initialize", "logaction")
{
Except = new [] {"Display"}
};
attribute.OnActionExecuting(context);
controller.Verify(x => x.initialize(), Times.Never());
controller.Verify(x => x.logaction(), Times.Never());
}
The implementation for OnActionExecuting changes very little, we simply check if the ActionName is on the exception list before invoking the filter methods:
if(!Except.Contains(filterContext.ActionDescriptor.ActionName))
controllerMethod.Invoke((object)controller, new object[] { });
And that’s that, we now have a fairly complete BeforeFilterAttribute we can use to keep controllers clean.