Category Archives: Uncategorized

Introducing Palmer (a Retry logic library for .NET)

Over the last week I’ve been slowly writing a little utility library to make it easier to express retry logic within application code. As software becomes more complex and more interconnected the concept of gracefully recovering from transient conditions becomes more important. Your average cloud or enterprise application is going to have to deal with network time outs, database deadlocks and temporary glitches caused by underlying systems recovering from hardware failures.

Palmer is a library that I’ve created to help solve this problem. You can read more about it over at GitHub. If you just want to download it and have a play, you can install the package from NuGet.

Install-Package Palmer

Using the API is easy with its fluent syntax:

Retry.On<SqlException>().For(5).With(context =>
{
  // Some code that might throw a transient SQL exception.
});

This is a 0.1 release so I am looking for feedback and ideas on how to improve the library. I’ve already got a couple of ideas such as:

  • Extend the On/AndOn methods to allow passing in a typed exception to make accessing specific exception fields easier.
  • Add event hooks to support logging exceptions as they occur.
  • Create an extension library for common usage scenarios such as SQL, network issues.
  • Support capturing multiple exceptions with one termination condition.

I’ve also added some issues around improving the documentation and turning the library back into a portable library after getting my build issues sorted out.

CA2214 & Entity Framework

I’ve just submitted a comment to the CA2214 code analysis guidance. Posting it here just in case others come across this and wonder what they should do.

There are some circumstances where, using Microsoft’s supplied frameworks this is unavoidable. For example, lets say that I am using Entity Framework:

public class Client
{
public Client(string name, User ownedBy)
{
Name = name;
OwnedBy = ownedBy;
}
protected Client()
{
}
public string Name { get; protected set; }
public virtual User OwnedBy { get; protected set; }
}

In order for Entity Framework to lazy load the OwnedBy property it needs to be marked as virtual. However we might want to stop developers constructing the object without providing the minimum mandatory information (in this case a name string, and a User instance who owns the object). When doing this the CA2214 code analysis warning is fired. The simple solution (in theory) is to mark the Client class as sealed, however Entity framework needs a default constructor to call. The way it works is by inheriting from the Client class with a default constructor which points to the protected constructor, thus allowing it to inject the code to traverse lazy loaded properties/collections etc.

I believe that whilst the guidance provided by CA2214 is valuable, it is inappropriate to say “Do not suppress a warning from this rule” because in some cases you might need to do it by design. Given the proliferation of ORM tools which use this approach for lazy loading of properties the guidance should be updated to outline some scenarios where you might want to call a virtual member.

C# 6.0 Suggestions: Quick Review

I’ve just posted four quick posts on three specific enhancements that I would like to see in the C# language.

Obviously I would like to see all of the above features added to the language (after they have been thought through more fully by the language designers). However I do wonder how much further investment there is going to be in C# as a language. If you look at C# 5.0 we basically got async/await (and associated plumbing) and caller info. There was some suggestion that we might get automatic property initializers but that appeared to be scrapped.

Still – the “Roslyn” project does hold some promise in terms of opening up the compiler to allow software developers to transform the syntax of the code prior to compilation thereby supporting the addition of new language features.

Time will tell whether that is a good idea in terms of source code stability because in essence those language tweaks need to be part of source tree just like anything else, otherwise you might snooker yourself if you accidentally lost your Roslyn-based source code transformation add-in.

C# 6.0 Suggestion: Backing Field Type Declaration

This is the third post which expands on two that I have already done tonight. The first was on adding an automatic property initializer syntax to C#, and the second was on adding backing field access from within the property accessors without having to separately specify the backing field.

My next suggested improvement is really just here for the purposes of completeness. Often you want to delay the creation of a backing filed instance until it is accessed. However, if you want to have a backing field which varies in type to the declared property then you need a way to specify the type of the backing field without actually instantiating it. Here is an example of the proposed syntax:

public class Customer
{
  public IEnumerable<Address> Addresses
  {
    get(field)
    {
      if (field == null) field = new Collection<Address>();
      return field;
    }
  } as Collection<Address>();
}

This code is pretty straight-forward. The backing field for the Addresses property is a Collection<Address>, but it is presented as an IEnumerable<Address>, and construction is deferred until it is accessed for the first time.

Potentially the syntax for optional arguments could be “borrowed” here to lead to a more concise syntax:

public class Customer
{
  public IEnumerable<Address> Addresses { get(field = new Customer<Address>()) }
}

In this case the type of the backing field could be inferred, the difference between the two being that the above syntax defers execution of the backing field initialization until the get accessor is called and the example from my automatic property initializer example is executed upon construction.

C# 6.0 Suggestion: Backing Field Access

I just posted on one feature that I would like to see in C# 6.0, automatic property initializers. Another that I would like to see is some kind of reserved word access to the automatic backing properties that are created. This would actually allow for more complex property initializer scenarios without actually having to create the backing field.

public IEnumerable<Address> Addresses
{
get(field)
{
return field.Where(x => x.IsActive);
}
set;
} new Collection<Address>();

This example builds on the automatic property initializer that I have proposed previously but adds a parameter to the get accessor which can then be used to operate against the backing field. The backing field type is determined by the initializer.

C# 6.0 Suggestion: Automatic Property Initializers

When automatic properties were introduced into C# as a short-cut way of declaring properties I am sure lots of developers cheered for joy. Automatic properties are a great enhancement but every-time I use them I keep thinking that they are somewhat incomplete.

What I would like to do is have a neat way initializing automatic properties without having to declare them in the constructor. The syntax would be something like as follows:

public class Customer
{
public IEnumerable<Address> Addresses { get; protected set; } new Collection<Address>();
}

There are a few subtle things going on here. First, I might want to expose a list of addresses to calling code, however I may not necessarily want to expose a collection that can be updated. The syntax above exposes a public property as IEnumerable<Address>, however the backing field would be created as a Collection<Address>.

This would remove probably the #1 reason that I have to resort to implementing a property manually.

Much Ado About TypeScript

The web world has been alive with conversation this week about TypeScript, Microsoft’s latest language offering which builds upon JavaScript. Funnily enough I visited Adelaide this weekend to catch up with my friend Darren Neimke and his family. I worked with Darren at Readify and he has more than a decade of experience with the .NET platform but more recently he has been immersing himself in the Google platform – including the Dart language.

We talked a bit about the merits of the language and the approach that the Dart team had taken around designing a language executing within a JavaScript execution environment. My feedback was that I felt that Dart would come into its own when they finished the server execution environment and enabled Dart within Google AppEngine (at the moment it only supports Java, Python and Go).

Then to our joint surprise Microsoft shipped TypeScript earlier this week, a JavaScript superset language which takes a very similar approach to Dart. TypeScript is a language specification, with a  supporting compiler that produces JavaScript. The TypeScript compiler is open source and written in TypeScript – which means that the TypeScript compiler can run in any JavaScript execution environment. Microsoft provides tooling that plugs into Visual Studio but Darren and I successfully got an Ubuntu server up and running, pulled down Node, and got the TypeScript compiler running.

What I do like about the approach that Microsoft is taking here is that they are trying to ensure that they don’t break all of the JavaScript libraries that we know and love. Instead, TypeScript adds a sprinkle of extra syntax to JavaScript, and in some cases are simply a progression along the lines of existing standards efforts.

The truth is that Dart is significant for Google because as a platform vendor they need to have their own server language. Microsoft already have a few languages that they own that have good adoption (existing .NET languages).

If I was Google, I would do what Google has done, and if I was Microsoft, well, I’m not sure that I would have bothered with TypeScript – but now that it is here it might encourage developers to look no only at TypeScript, but also other languages that build upon JavaScript to execute within the browser. TypeScript also adds some extra power to Microsoft’s Node hosting platform within Windows Azure without breaking compatibility with the APIs that come out of the box with the Node engine.

Interesting times indeed.

More Windows Phone 8 sillyness.

I’ve been watching the Windows Phone 8 conversation with great interest. I say conversation, but it has been kind of one sided. That said, Todd Brix who posted the most recent post (12 Sept) on the WP development blog has chimed in on the comments following the post. Here is a copy of what he commented:

I wanted to respond to those of you who are frustrated about the fact that we haven’t publicly released the Windows Phone 8 SDK. This year we’ve taken the approach of unveiling the new features of the OS as close to device availability as possible.  Unfortunately we can’t release the SDK without revealing all of the new features and capabilities.  I recognize this is a departure from how we’ve operated in years past and some of you are questioning our thinking.  That’s ok – let me give you a little more to consider.

Windows Phone 8 was designed to run the apps written with the Windows Phone 7 SDK which is available today.  Right now and for most devs the high-volume app development opportunity remains on the Windows Phone 7 SDK because these apps will run on phones available later this year, regardless of what OS version is on the phone.  If you want to help make sure your Windows Phone 7 app runs well on Windows Phone 8, when it is released, you’ll want to take a look at the Sept 12th post from Andrew Whitechapel . And while I know you want to test your app in our new emulator, the reality is that you will also want to do final testing with Windows Phone 8 devices which are not yet in market.

There are also devs who want to jump on new Windows Phone 8 technologies before anyone has bought a device, which is fantastic.  This is why we’re doing the early Preview Program. For those who don’t get the Windows Phone 8 SDK via the program, we will get the final SDK out in time for you to capitalize on the wave of new devices.

Thanks for your interest in Windows Phone!

Todd makes several good points in his comment:

  1. The biggest opportunity right now for Windows Phone developers is the 7.x generation of devices.
  2. They can’t release the Windows Phone 8 SDK without developers figuring out what the platform has on offer.

Unfortunately, the comment still completely misses the point. Firstly, even if I am going to target Windows Phone 7.x generation devices, I still want to work within the latest version of Microsoft’s IDE. Developers don’t just write Windows Phone applications, they build the backends which support those applications and when your vendor doesn’t continually bring their SDKs up to a level where you can use a single IDE they are effectively introducing a productivity burden. I want an updated SDK which allows me to use Visual Studio 2012, I’m actually less concerned about the Windows Phone 8 specifics right now.

The other point, which has been raised by others is that by sharing the SDK with some developers, they are effectively sharing the SDK with all developers. We are already hearing rumours about WP8 devices being found in the wild. It is possible that these are just unfounded rumours, but it definitely appears like they have RTM’d the operating system. Sorry guys – you can’t contain this stuff it will leak out. Developers will be pissed if they are the last to know – it is a great way to completely screw an product eco-system and fill it with ill-will.

So where are we after my previous post? Nowhere. I had hoped that Microsoft would quickly switch strategies and open up access to the SDK as soon as it was ready (so no super secret preview program). That is looking less and less likely now. Not only that, but it appears that Microsoft is suggesting that we should be happy and target the mobile platform with the most opportunity (see highlighted second in Todd’s comment). Fair enough – here are some statistics for you to consider (according to the Mobile Mix report):

  • 46% Android
  • 34% iPhone
  • 15% BlackBerry
  • 4% Windows
  • 1% Symbian

Time to leap to action! Go and download the Android SDK and the iOS SDK.

Windows Phone 8 SDK? Srsly?

The Windows Phone 8 SDK Preview program is now open. Except you can’t download the bits, and it isn’t the least bit open. If you are a software developer that has been loyal to the Microsoft platform for years (or decades) you have the right to be pissed.

Many developers have been eagerly waiting for the release of an updated SDK for Windows Phone. Microsoft didn’t see fit to release an update with Visual Studio 2012 so that developers could use the latest IDE to target Windows Phone 7.x, and so an updated SDK was really overdue.

Unfortunately on the 5th of September Microsoft announced that they would only be opening up the Windows Phone 8 SDK in preview mode to a select few, basically cutting off those who had been waiting for an update in the SDK to start work on their projects (whether they are Windows Phone 7.x or Windows Phone 8.x). The overwhelming reaction of developers commenting on that post and today’s update was clear (epic fail).

In order to get access to the SDK you need the following:

  1. Publisher Name
  2. Dev Center Publisher GUID
  3. Registered Country
  4. Application Name
  5. Application GUID

As an MVP I’m used to filling in forms on the Microsoft Connect, but normally it is just basic demographic and organisation information. This is a whole new level and it basically excludes a lot of really passionate developers from getting access. The form of course is not the only blocker, Microsoft then needs to “select you” because it’s not just a case of filling in the form and automatically getting access to the bits.

The Windows Phone 8 SDK preview is not open, its closed. Period.

Sure the marketing folks in charge of this decision are quickly reconsidering their options. Firstly – the requirement to go through this process will detract from developer adoption of their platform. Right now they are getting a strong reaction from a small number of developers passionate enough give a crap.

I’m already feeling cheated having paid good money (USD$99) to get access to the Windows Phone Dev Center, only to have Microsoft completely drop the ball on SDK updates after the RTM of Visual Studio 2012.

Right now Microsoft has sub-10% market share in the mobile operating system space. This strategy of locking loyal developers out of the platform updates (combined with not releasing updates to existing tools) is completely wrong-headed.

Version Numbers

Identifying when, how and whom produced a product is important. It is important with physical goods, hence why we have barcodes, expiry dates, model and serial numbers. The same is also true for software. Embedding a version number in files that ship with a product allow support team members to determine what version of the source code went into produce that version of the product and therefore more easily debug its observed behaviour.

Later this week I was working with a fellow team member on automatically applying version numbers to our product each time that it built (continuously integrated). On the surface it is quite a simple task except when you start to consider the sheer number of places that version numbers should be applied within a relatively complex build process. Here are some common examples:

  • .NET assemblies
  • NuGet packages
  • WiX installers
  • Documentation files

Each one of these target locations requires a different approach for finding and updating version numbers and it end up getting pretty complicated. But before you start you might want to give some consideration to what your version number actually looks like, and what it means.

I take a lot of inspiration from SemVer.org but it doesn’t really work for all technologies. For example, strongly named assemblies in .NET have four elements (major.minor.build.revision) whereas SemVer defines three elements (major.minor.patch). SemVer also defines trailing elements followed by a dash (for example 1.0.9-rc1) which won’t work for the strong name in a .NET assembly. Of course we can use other informational versioning techniques to pass along SemVer like version information.

If we look at SemVer and apply the first part (major.minor.patch) to version numbers within the package then we could probably adopt a manual version increment technique and just take a centrally defined version number (say in version.xml in the root of the solution) and stamp it in various files during the build process.

Anyway – version numbers, they are important!