Softwire Blog


Code Generation in .NET with the Roslyn CTP (Part 3)


12 July 2012, by

previous article in series

Visual Studio custom toolThis post is the third in a series on code generation on the .NET platform. In this post, we will look at how to package a code generator as a Visual Studio extension, and how best to share a single library between multiple extensions.

(more…)

Code Generation in .NET with the Roslyn CTP (Part 2)


5 July 2012, by

previous article in series

Visual Studio custom tool

This post is the second in a series on code generation on the .NET platform. In this post, we will take a closer look at Microsoft’s Community Technology Preview of ‘Roslyn’. In brief, this is a C# compiler implemented in managed code that exposes an API to let you hook into the compilation process.

Recap

In the previous post, I introduced Roslyn as an interesting tool for code generation, in particular when generating source code from other source code. Roslyn is especially appealing for this purpose because it provides a strongly-typed model for working with source code and (unlike most code generation approaches) allows you to use the same model for both input and output.

One of the common drawbacks of most other options for code generation (as discussed in the previous post) is the need to translate from one model to another and how clumsy this can be, particularly when you want to carry across some elements of the input source code without changing them. A detailed example of when you might need to do this is discussed in the appendix at the end of this post. (more…)

Code Generation in .NET with the Roslyn CTP (Part 1)


28 June 2012, by

Visual Studio custom tool

Introduction

This post is the first in a series on code generation on the .NET platform. This is a huge subject area and there are a bewildering number of relevant libraries and tools that are useful for this purpose. I will make an effort to mention the most important third-party options (particularly where they’ve pre-empted Microsoft’s efforts), and may revisit some of them in future posts. However, for this series I will focus on those libraries and tools that are part of the .NET framework itself, or will be included in future versions. In particular, I’ll explore building code generation tools using Microsoft’s in-development compiler-as-a-service project, code-named “Roslyn“.

In this post I’ll briefly discuss what we mean by code generation, before going through some of the current tools and libraries for code generation in .NET, along with their shortcomings. I’ll also introduce Roslyn and explain why it initially caught my interest as a potentially useful library for code generation. Subsequent posts in this series will cover what I learned about using Roslyn and creating Visual Studio extensions, by looking at a specific WCF-related code generation scenario.

(more…)

Softwire Speed Coding Challenge – Question 4 Discussion


28 May 2012, by

previous article in series

In our last coding challenge post, we posed the final question from our coding challenge. Did you manage to finish it?

(more…)

Softwire Speed Coding Challenge – Question 4


21 May 2012, by

previous article in series

We’re back, with the final question of the 2011 Softwire Coding Challenge!

Navigating The Hampton Court Maze

Here is a picture of a familiar-looking maze. This version of the image is slightly cut down from the version used in the competition itself; click it to see the full-size image (which you should use if you’re trying this yourself).

Hampton Court Maze - Click for full size

 

Write a program to draw a magenta line (#FF00FF) from the exact bottom middle of the image to the exact centre of the image in the shortest distance possible. Pixels are considered adjacent if they touch horizontally, vertically or diagonally, and only perfectly white pixels (#FFFFFF) are traversable. The answer should contain the total distance travelled in terms of number of pixels (not spatial distance) and the program you used.

Hint:

The simplest optimal path finding algorithm is called Dijkstra’s algorithm (hope you brushed up on it!), and goes something like this:

  • Assign each pixel a distance from the start, initialise these values to +infinity
  • Assign the start pixel distance 0, add it to the back of a FILO queue
  • While your queue is not empty, pop the first pixel from the queue: call this “current pixel”
    • Examine all the pixels adjacent to the current pixel
    • If their recorded distance is greater than the current pixel’s recorded distance + 1, then:
      • Set their distance to be the current pixel’s recorded distance + 1
      • Add the pixel to the back of the queue
  • When you have examined all pixels in this fashion, each pixel will be marked with its minimum possible distance to the start
  • You can then trace the optimal route from the end to the start by placing a pointer at the “end” pixel and moving repeatedly to an adjacent pixel whose distance is 1 less than where you are looking (until you reach distance “0” – the start)

Once again, best of luck with this one; let us know how you get on with this in the comments and we’ll post the answer soon!

next article in series

Softwire Speed Coding Challenge – Question 3 Discussion


14 May 2012, by

previous article in series

In our last coding challenge post, we posted question 3 from our coding challenge. Did you try it yourself? Here’s the question again, as a reminder:

(more…)

Softwire Speed Coding Challenge – Question 2 Discussion


30 April 2012, by

previous article in series

So, in our last speed coding post, we set you the second question in our challenge. Here it is again, just to remind you!

(more…)

JavaScriptSerializer circular reference error when serializing a proxy object


19 April 2012, by

One day, my code suddenly started throwing circular reference errors while trying to serialize objects into JSON. There was no circular reference, and I couldn’t figure out why the couple of extra lines that I last added would affect serialization. What’s more, it only threw an error when I was logged in as certain users, and not as others.

The problem was that the object it was trying to serialize sometimes appeared as a proxy instead of the actual object. This happens if NHibernate doesn’t have enough information to resolve all the dependent objects after the first load – in our case it was dependent on the length of the chain in a parent-child tree. We first attempted to tweak our lazy loading config via .LazyLoad(Laziness.NoProxy) in our Fluent NHibernate mapping, but that laziness mode is only supported on some relationship types and not on the ManyToMany that was the cause of our problem, so this didn’t help us.

There seem to be a lot of people out there asking the same question, and some very complicated solutions being offered (such as this one using Json.Net).

However, in some cases there is a fairly easy workaround you can employ: unproxy the object before you serialize (see here).

Put this in the EntityBase:

public virtual T As<T>() where T : EntityBase
{
  return this as T;
}

And then when you want to serialize it, do something like

var lSerialiser = new JavaScriptSerializer();
unproxiedObject = originalObject.As<User>();
return lSerialiser.Serialize(unproxiedObject);

Note that this will not fix the issue in all cases. But if it works for you, then we’re glad to have been of service!

Softwire Speed Coding Challenge – Question 1 Discussion


16 April 2012, by

previous article in series

Last time out, we set you the first question from our recent Speed Coding Challenge, giving you the opportunity to answer it yourselves and compare your efforts to our winning answer. Here’s question 1 again, just to remind you what it was!

(more…)

Memoization explained part 4: multiple parameters


13 April 2012, by

Last time we saw the implementation of simple memoizers and I promised that this time, we’d take a look into the more advanced overloads – the ones with more than one parameter type.

(more…)