Book Review: Anathem by Neal Stephenson

Have you ever been at a party and two drunk and/or stoned guys are sitting in the corner discussing the meaning of life or the nature of existence?  Would you have any interest in reading a transcript of that conversation?  I didn’t think so.  Now imagine that the two guys are Albert Einstein and Stephen Hawking, and the discussion is about the nature of pan-dimensional existence.  Okay so now you’re more interested in the transcript, right?  The only problem is that now you can’t understand a word of it.  That’s kind of what reading Anathem is like.

I wanted to like it, I really did, but to be honest it takes up twice the space it ought to.  There’s little enough actual plot in this novel that I could retell it in less space than this review takes, but the narrative is so tangled up in its own contemplative navel-gazing that I had to continuously stop myself from yelling “Get to the friggin’ point, would you?”  At the same time, standard plot elements and devices are given only the most cursory treatment. Should the main character have a love interest?  No, we’ll just let him decide “I think I like Ala” before yanking her away so that he can spend the rest of the novel pining over a girl that he had previously never even gave a second thought to, nor made it to second base with.

My main irritation with Anathem is the author’s heavy-handed use of invented language to immerse the reader in the world in which the non-action takes place.  Plenty of authors have used invented language to gently remind the reader that they are no longer in Kansas, but Anathem spent so much time positively pummeling me with its Oz-club that it became a distraction.  It’s okay to make up a few new names and/or terms here and there, but when the reader spends most of the novel saying “Now what the hell was a ‘mobe’ again?” you’ve overdone it.

Fortunately the made up language becomes more sparse throughout the middle of the story but at the same time, that’s when everything stops happening, and it degenerates into the literary equivalent of a particularly uneventful road-movie.  It’s like the whole Tom Bombadil section of Lord Of The Rings, but ten times as long.  Chapter after chapter of absolutely nothing of any interest happening, but described in excruciating detail.  I would have given up on it, but by this point finishing the novel had become personal, although I found myself "fast-forwarding” through some of the dinner conversation “messals” in which page after page is spent discussing many different wordings and explanations for what could easily have been explained just once, and more succinctly.

I’ll always appreciate Neal Stephenson for writing Snow Crash and In The Beginning… Was The Command Line, but loyalty only stretches so far, and mine is now soap-bubble-thin.  The ideas were fascinating, but so much time was spent on philosophical dead-ends that the whole thing cries out for editing.  Did I need to witness a conversation about the most mathematically pure way to divide a cake into eight equal pieces?  No, I really didn’t.  (To be fair, this conversation was an appendix, but you can’t just drop footnotes and not expect me to read them.)  The intricacies of how the concent’s clock is wound, and how it works the gates genuinely added to the sense of life at Saunt Edhar’s, but plenty of other orthogonal offshoots could have been left by the wayside, resulting in a tighter, more focused work.

Posted in Uncategorized | Leave a comment

Joel Spoelsky just became irrelevant

I was listening to the Stack Overflow podcast #39 last night while waiting for my daughter’s basketball game to start, and I’m pretty sure Joel just flipped the bozo bit in my mind.  He and Jeff Atwood were discussing test driven development, and Joel’s example had to do with a button in Copilot that would change the jpeg compression level, I suppose to make screen text momentarily more legible at the expense of bandwidth.  Joel then went on to complain how the resulting unit tests would dwarf the actual code.
 
The actual code would have been about 10 lines by Joel’s estimation since all it had to do was change one of the parameters to the jpeg compression library they use on this product.  The test code, however would be monstrous, requiring pre-made "after" versions of an image at varying compression levels with which to compare the results with the button in various states.  He also mentioned the problem of someone else having to test these things, and the difficulties they would encounter if their jpeg library happened to be different than what Joel was using.
 
This exchange pointed out a couple fundamental misunderstandings about how you write unit tests.  Apparantly, Joel only understands full, top-down integration tests.  The idea that I would write unit tests for something like Word, for example, by checking that the individual bits of a .doc file are affected by clicking the bold button on the toolbar illustrates a complete misunderstanding of what "unit" testing means.  You don’t test the launch systems of our nuclear arsenal by blowing something up.  See my previous post for my new favorite definition of unit testing.  Bascially, I said:
"Assuming everyone else does their job, does this code do what it should?"
That’s what Joel doesn’t understand.  In reality, all Joel’s test needed to cover was that pushing the button changes the input parameter to the compression library.  You don’t worry about whether the compression library works, that’s not your code.  Those tests belong to the component vendor who makes the compression library.  If, for some insane reason, Fog Creek felt it necessary to write their own jpeg compression routines, then they are violating a couple of other closely-held principles of mine, and have thus flipped their collective bozo bits en masse.  I certainly hope this isn’t the case.
 
This basic misunderstanding of who owns what is illustrated by the second part of Joel’s comment, where he talks about the testing difficulties introduced if customers jpeg libraries produce different results than whatever Fog Creek was using.  Excuse me?  The customer has no more business running your unit tests than you do running the tests for the jpeg library that, God willing, you didn’t write in-house.  Why would your customers even have your tests.  Why would you have shipped them the test assemblies in the first place.  They aren’t going to need it (TAGNI?).
 
I’ve listened to Stack Overflow for a while, and I’ve found it very interesting and enlightening in most cases, but this one episode reduced my personal opinion of the hosts by several orders of magnitude.  If you don’t understand a subject, then either don’t talk about it, or have someone on who does understand it to explain it to you.  There are inexperienced developers out there who think that just because you have a podcast, then you automatically know what you’re talking about.  It’s your responsibility as a host to not do irreperable damage to your audience by convincing them that things aren’t important just because you don’t understand them.  This one episode has just armed a small army of developer luddites with a "get out of testing free" card that they will play to their pointy haired bosses, thus avoiding that whole nasty "career advancement" business.
 
Hopefully in the coming weeks some more guests will appear to set Jeff and Joel straight.  If not, then I’ll probably just stop listening.  The bozo bit is, however, very hard to un-set.  Years ago something Rockford Lhotka said flipped his bozo bit in my mind, and I still have a hard time taking him seriously today.  The thing is, I can’t even remember what it was that he said now.  Yeah, that’s how sticky the bozo bit is.
Posted in Computers and Internet | 1 Comment

Shuffle meme

It’s been a while since we’ve done the "shuffle" meme, so I’ll start it off today.  The rules are simple: Put your Zune/Ipod/Media Player/Whatever in shuffle mode and write down the first 10 things it plays at you.  It’s okay to bail on a song if it sucks, but you still have to put it on the list.
 
Today, my Zune is having some pretty heavy mood swings.  It’s either very mellow, or very angry from one minute to the next.
  1. Thomas Dolby – I Love You, Goodbye
  2. Cubanate – Oxyacetylene
  3. Enormous Richard – The Chemistry Song
  4. The Police – Don’t Stand So Close To Me ’86
  5. Scorpions – You’re Lovin’ Me To Death
  6. Concrete Blonde – Everybody Knows
  7. Hooverphonic – Every Time We Live Together We Die A Bit More
  8. Combichrist – All Your Bass Belongs To Us
    (Yes, he totally screwed up the meme on that one)
  9. VAST – Thrown Away
  10. Lonnie Mack – Wham

And one bonus track: Meat Beat Manifesto – Edge Of No Control Pt. 2

Posted in Music | Leave a comment

On Unit Testing, Mocks, and Isolation

This isn’t going to be news to most developers, but I just thought I’d write this down anyway.  I was talking with a colleague yesterday about unit testing, and about RhinoMocks in particular, and came up with what I thought was my best explanation to date about why Mocks are important.  To me, it all came down to the following sentence.
"Assuming everyone else does their job, does this code do what it should?"
It’s that "assuming … job" part that’s important.  That’s what the mocks or stubs are for.  They’re the tool that lets us focus a test on a single piece of code, excluding as much of the outside world as possible.  That’s what makes unit testing work.  We’re not saying that the code "over there" won’t be tested as well.  It just won’t be tested "here".  Having a separate test for "here" and "there" rather than one overall test for both means that if one of the two pieces break in the future, then we’ll know which one it was.  We’ve cut our number of suspects in half by testing them separately.
 
Talking about this reminded me of a problem a theatre director once explained to me.  It’s all about vocabulary.  The problem isn’t usually that people can’t understand what you’re trying to get them to do, it’s that you haven’t put it in the right terms for them.  You can give the same instructions to three different actors, and each one will do something slightly different.  Getting people to really grok what you’re saying relies entirely on finding the words that mean the same thing to them as the concept you’re explaining means to you.
 
Like I said, if you "get" unit testing, then this post isn’t anything you don’t already know.  But maybe if you’re just getting into the idea, or if you’re the type who argues for integration testing because it’s closer to "real world" conditions, then maybe my explanation might change your mind about how you think about testing.
Posted in Computers and Internet | 1 Comment

Framework Design Guidelines (Second Edition)

I meant to write about this book a while back, but got caught up in a bunch of projects and didn’t get around to it.  If you haven’t bought this book yet, you really should.  The first edition has been an invaluable asset to me on a number of past projects, and the second edition is even better with sections on newer language and framework features such as Linq and extension methods.

I’ve seen, read, and even written a few standards documents in my time as a professional programmer, and I think this book is the last one I’ll be needing.  The format of the book is one I always enjoyed, with the guidance interspersed with comments from the "peanut gallery" of Microsoft architects.  These asides give you a lot of insight into the "why"s, something which a lot of standards documents are missing (I’m talking about YOU, IDesign).  It’s one thing to be told to do something in a particular way, but it’s a lot better when you are taught why.  Simple coding patterns that I wouldn’t have given a second thought to have turned out to have a great impact on other aspect of my code once they were explained.

The basics are covered, such as naming and formatting standards, but the book goes much further with sections about when and how to use certain interfaces, and provides some brief explanations of common design patterns as they relate to the .net framework.  I’m not talking about "Visitor" or "Model View Presenter" here, I’m talking about "IDisposable"… muuuch lower level stuff.

Basically, this book isn’t just about what you ought to be doing, it’s about explaining why Microsoft did what they did in the .net framework.  It’s refreshing at times in the book to find a discussion about how something was a bad choice in retrospect, or how the framework designers wished they had done something differently knowing then what they know now.  I feel a lot better about my own changes of mind, and less like an amateur for not having seen the eventual solution in the beginning.  After reading it, I’m more comfortable that I’ve made the right career decision to stick with this programming stuff.

Another great asset included with this book is the DVD.  It’s full of training sessions and example API specifications.  One of the first things I did with the previous edition was to convert all the videos to play on my Zune, and spent the next few weeks watching through them whenever I got the chance.  I probably won’t watch them all over again, since I think they’re the same videos, but they’re definitely worth the watching.

Posted in Uncategorized | Leave a comment

Use DateTimeContext to put your code in a time warp

On 12/31/08 all the 30 gb Zunes in the whole world died en masse.  This was attributed to a bug having to do with 2008 being a leap year.  Because the 31st was the 366th day of the year, something in the clock code for the Zune freaked out, locked up, and earned Microsoft a lot of scorn from the Apple fanboys of the world.

This started me thinking.  It should be easier to test what your code will do at a particular time.  Thinking aloud over Twitter, I settled on a design similar to the way TransactionScope works.  You put your code inside a using statement, and it thinks it’s whatever time you want.  It’s not quite that easy since we can’t just replace the DateTime class, or at least not that I know of.  If someone has some Ninja tricks that would allow us to do this, I’d be glad to hear them.

In the meantime you’ll have to replace all the calls to DateTime.Now and DateTime.Today with calls to similar properties on the new DateTimeContext class.  If you’re not IN a DateTimeContext at the time, then these methods will return the real date or time, just like you’d expect.  If you wrap some code in a using DateTimeContext block, then they’ll return whatever you’ve told them to.  I dislike changing code solely for the purpose of testing, but for purposes of testaBILITY is okay.

Here’s a unit test I wrote that illustrates how the DateTimeContext class is used.

[TestMethod]
public void DateTimeContext_returns_expected_values()
{
    DateTime date1 = new DateTime(2008, 12, 31, 23, 59, 59);
    DateTime date2 = new DateTime(2009, 1, 1, 0, 0, 0); 

    Assert.AreEqual(DateTime.Today, DateTimeContext.Today);
    using (new DateTimeContext(date1))
    {
        Assert.AreEqual(date1, DateTimeContext.Now);
        using (new DateTimeContext(date2))
        {
            Assert.AreEqual(date2, DateTimeContext.Now);
        }
        Assert.AreEqual(date1, DateTimeContext.Now);
    }
    Assert.AreEqual(DateTime.Today, DateTimeContext.Today);
}

Notice that the Asserts that are outside of any context are comparing against Today, and the ones inside are using Now.  This is just to get around the problem where the time could have changed between two retrievals.  I could have asserted that the difference between the times was some negligibly small amount, but I wanted the excuse to hit both the Now and Today properties anyway.

Now on to how it works.  The DateTimeContext class uses Thread Local Storage to store a reference to the current instance, and provides a static "Current" property to help us get to that instance.  When we ask for Now or Today, the static methods will check to see if there is an instance on the thread, and if so, they’ll return what they’ve been told to return.  If not, then they’ll defer to the real DateTime values.

public static DateTime Now
{
    get
    {
        DateTimeContext current = Current;
        return (context == null) ? DateTime.Now : current.Value;
    }
} 

public static DateTime Today
{
    get { return Now.Date; }
}

Each instance of the class also has a "PreviousContext" property that will remember the instance that was on the thread when it was instantiated.  This lets us nest contexts, and they’ll unwind properly when you’ve finished with them.

private object PreviousContext { get; set; }

There are multiple constructors which mirror the most commonly-used DateTime constructors as a convenience.

public DateTimeContext(long ticks)
    : this(new DateTime(ticks)) {}

public DateTimeContext(int year, int month, int day)
    : this(new DateTime(year, month, day)) {}

public DateTimeContext(int year, int month, int day, int hour, int minute, int second)
    : this(new DateTime(year, month, day, hour, minute, second)) {}

public DateTimeContext(int year, int month, int day, int hour, int minute, int second, int millisecond)
    : this(new DateTime(year, month, day, hour, minute, second, millisecond)) {}

public DateTimeContext(DateTime value)
{
    Value = value;
    var slot = Thread.GetNamedDataSlot(slotName);
    PreviousContext = Thread.GetData(slot);
    Thread.SetData(slot, this);
}

Finally, there are all the fiddly IDisposable bits and destructors that make it all tick.  When a DateTimeContext is disposed, it puts the previous instance back on the thread, allowing us to stack them up all we want (Or until we run out of memory).

Here’s the whole class.  Hopefully it proves useful for someone.  It should make it a lot easier to test what certain code will do on or just after certain transition/edge case days.  This may not be the final version, but I couldn’t stop thinking about it until I wrote it out.

public class DateTimeContext : IDisposable
{
    #region Fields

    public const string slotName = "DateTimeContext";
    private object PreviousContext { get; set; }

    #endregion

    #region Properties

    public DateTime Value { get; set; }

    public static DateTimeContext Current
    {
        get
        {
            var slot = Thread.GetNamedDataSlot(slotName);
            var current = (DateTimeContext)Thread.GetData(slot);
            return current;
        }
    }

    public static DateTime Now
    {
        get
        {
            DateTimeContext current = Current;
            return (current == null) ? DateTime.Now : current.Value;
        }
    }

    public static DateTime Today
    {
        get { return Now.Date; }
    }

    #endregion

    #region Constructors/Destructors

    public DateTimeContext(long ticks)
        : this(new DateTime(ticks)) {}

    public DateTimeContext(int year, int month, int day)
        : this(new DateTime(year, month, day)) {}

    public DateTimeContext(int year, int month, int day, int hour, int minute, int second)
        : this(new DateTime(year, month, day, hour, minute, second)) {}

    public DateTimeContext(int year, int month, int day, int hour, int minute, int second, int millisecond)
        : this(new DateTime(year, month, day, hour, minute, second, millisecond)) {}

    public DateTimeContext(DateTime value)
    {
        Value = value;
        var slot = Thread.GetNamedDataSlot(slotName);
        PreviousContext = Thread.GetData(slot);
        Thread.SetData(slot, this);
    }

    ~DateTimeContext()
    {
        RestoreState();
    }

    #endregion

    #region Methods

    private void RestoreState()
    {
        var slot = Thread.GetNamedDataSlot(slotName);
        Thread.SetData(slot, PreviousContext);
        if(PreviousContext==null)
            Thread.FreeNamedDataSlot(slotName);
    }

    public void Dispose()
    {
        RestoreState();
        GC.SuppressFinalize(this);
    }

    #endregion
}

Update 1/1/09: I decided to call the class DateTimeContext instead of the original DateTimeScope.  It just sounded better to me personally.  Also, comments about the DateTime issue "possibly" being the cause of the global Zunepocalypse were removed since that’s exactly what it turned out to be in the end.

Technorati Tags: ,,,,
Posted in Computers and Internet | 4 Comments

I’m going to CodeMash

Oh yeah, I forgot to post one of these:
 
Best… conference… EVAR!
Posted in Computers and Internet | 2 Comments

Generating DataContext Proxy Classes With T4 Templates

A while back, my friend and coworker Kris Scott posted an article about using a proxy around DataContext classes to add support for isolation/mocking for testing purposes.  I won’t re-hash the basic idea here, as Kris’ blog entry explains it just fine.  In that article he links to a Google Code project which generates these proxy classes for you.  I’ve used it to great success on two separate projects now, but as I prepare to leave my current project, I didn’t feel altogether comfortable leaving behind a dependency on an external tool that none of the other developers were familiar with.

I’d been looking at Damien Guard’s T4 template replacement for generating an entire replacement Linq to Sql DataContext, as well as Oleg Sych’s articles regarding T4 template processing in Visual Studio 2008.  This started me thinking about creating a T4 replacement for the proxy class.  I spent some time this weekend taking a stab at it, and I now have a T4 template that generates the exact same thing as Kris’ original program.  This template adapts a few of the classes from Kris’ generator, using Linq to XML in place of XML DOM code.  The results are the same, but I like Linq a lot, and figured it would be worthwhile making the change as long as I was in the neighborhood.

One of the interesting things about using T4 templates is that they go to work whenever you make a change to the template itself.  You’re can also right-click on the template and "Run Custom Tool" to re-run the generation whenever you like, but in our case we’d like the template to be re-run whenever we make a change to the .dbml file, which is going to change far more often than the template will.  To accomplish this, I’ll again adapt something that was working for Kris’ technique.  On the project he had written it for we were launching Kris’ generator program as a pre-build step of the project containing the .dbml file.  Some searching on the web told me that the T4 template processor is called TextTransform.exe, and can be run as a command line tool.  I changed the pre-build step to something like this.

"C:\Program Files\Common Files\Microsoft Shared\TextTemplating\1.2\TextTransform" "$(ProjectDir)\AbcData.tt"

Unfortunately, the TextTransform tool’s directory is not in any search path, so I’ve had to hard-code the entire path to the tool.  This is fine for now, but if a future version of the tool is released, I’ll have to remember to update the pre-build step to call the newer version.  In this example, the dbml file is called AbcData.dbml, so we call the T4 template "AbcData.tt" to match.  All T4 templates have a ".tt" extension, and in our case I’m following Damien Guard’s lead and dynamically "figuring out" the path to the dbml by requiring the T4 template to have a similar name.  The template will create a class called "AbcData.Proxy.cs".  I wanted the file to be called "AbcDataProxy.cs" like Kris’ original program did, but the T4 processor seems to add the extra dot whether I want it to or not.  The proxy class itself is still called "AbcDataProxy", though.  I won’t dissect the template line by line here, but I’ll point out some of the highlights.

At the top of the template file we have a bunch of directives.  You can get information on what they all mean off of MSDN or from Oleg’s posts.  Most of them are essentially "using" statements which specify the .Net libraries that our template’s code will be using.  The second line is where you specify what extension your resulting code should have.  It’s actual filename will be determined by the name of the template itself.  This is where that extra dot comes from.  Whether I specify the extension as ".Proxy.cs" or "Proxy.cs" the T4 processor will helpfully make sure that there’s a dot there… gee, thanks.

<#@ template language="C#v3.5" hostspecific="True" #>
<#@ output extension="Proxy.cs" #>

When we run the template, it will need to be able to write to a file that in all likelihood already exists, and is under source control.  To accomplish this, we’ll need to make the file writeable.  Visual Studio will somehow magically pick up on this and check the file out from source control, which is pretty cool, actually.  We’ll take the name of the template, do some string manipulation to come up with the name of the resulting file, and then remove the ReadOnly attribute from that file before continuing with the code generation.

// Make the output file writable if it already exists
string outputFileName = Host.TemplateFile.Replace(".tt", ".Proxy.cs");
if(File.Exists(outputFileName))
{
    FileAttributes attributes = File.GetAttributes(outputFileName);
    File.SetAttributes(outputFileName, attributes & ~FileAttributes.ReadOnly);
}

After that, we’ll load the .dbml file, which is just an XML document, and begin using Linq to XML’s methods to start dissecting it, looking for the parts we’re interested in.

var dbml = XDocument.Load(Host.TemplateFile.Replace(".tt", ".dbml"));
var root = dbml.Root;
var Tables = (from t in root.Elements(NS + "Table") select new ContextType(NS, t)).ToList();
var Functions = (from t in root.Elements(NS + "Function") select new ContextFunction(NS, t)).ToList();

This is where my versions of Kris’ classes come into play.  The ContextType and ContextFunction classes take an XElement into their constructors, and can generate the interface or proxy class signatures we’ll need later on.  Next, we add some boilerplate stuff to the template output like using statements, the interface header, and some methods that appear in every interface without changing.  After that, we escape out to code similar to classic ASP or CodeSmith templates and include two loops which will use the ContextType and ContextFunction classes we created earlier to emit the per-table and per-function contents of the interface.

namespace <#= ContextNamespace #>
{
    public partial interface <#= InterfaceName #>
    {
        void CreateDatabase();
        bool DatabaseExists();
...
<# 
foreach(var table in Tables)
{
    WriteLine("\t\t" + table.InterfaceSignature());
}

foreach(var function in Functions)
{
    WriteLine("\t\t" + function.InterfaceSignature());
}
#>    }

After generating the interface, similar code will create the proxy itself.  Toward the bottom of the template, you’ll find the declaration of the ContextType and ContextFunction classes themselves.  They appear in a different kind of code block, delimited by "<#+" and "#>".  Any classes your template needs can be declared in this way.  I could have broken these out into a separate file and used directives to include it here, but I prefer having a single template file like this, at least at this scale.

So how do we use all this?

  1. Get the template and supporting classes (here).
  2. Unzip them into the folder containing the dbml file you want to generate a proxy for.
  3. Rename AbcData.tt to match the name of your dbml file, but with a ".tt" extension.
  4. Include the files in your project.
  5. Add a pre-build step to the project with the dbml file, similar to the following:
    "C:\Program Files\Common Files\Microsoft Shared\TextTemplating\1.2\TextTransform" "$(ProjectDir)\AbcData.tt"
  6. Build your project
  7. Enjoy

UPDATE: For those of you with 64 bit Windows installations
I KNEW the hard-coded path would cause a problem, and I was right.  64 bit windows adds "(x86)" at the end of the 32 bit "Program Files" folder, so the original hard-coded path won’t work.  I’ve used a couple different techniques in the past to solve this, but my current favorite is using a relative path from the project itself. Find the TextTransform.exe file, which is located at "C:\Program Files\Common Files\Microsoft Shared\TextTemplating" (With or without the x86 part, as appropriate). Once you have found it, just copy it into a folder in your solution. In this example, I’ve copied it into a "libs" folder at the root of the solution. Now add a pre-build step to the project containing the .dbml file, and use relative paths to use your local copy of TextTransform.exe. It looks like this:

"$(ProjectDir)..\..\..\libs\TextTemplating\TextTransform" "$(ProjectDir)\AbcData.tt"

Posted in Uncategorized | 3 Comments

Itty Bitty IoC (A C# IoC container in 100 lines)

Since I posted my Teeny Tiny Template parser last week, I figured I’d keep going and post about my Itty Bitty IoC container as well.  I’m not going to attempt to define IoC or Dependency Injection in this blog post, that’s not the point.  There are enough sites out there already that cover this pretty well.  Go read Martin Fowler’s article "Inversion of Control Containers and the Dependency Injection pattern" if you want the full story. 

This blog post is simply to introduce my own "ClassRegistry" class, a C# IoC container that does pretty much everything I’ve ever needed from an IoC container, and does it in 100 lines.  This is another one of those classes I like to use as a "first resort".  No it doesn’t have the power of something like StructureMap, but for a lot of projects that can be overkill anyway, and when a new project is starting up it’s more about establishing the pattern than nailing down specific tools.  You can always switch to a different IoC container later on if you outgrow this one.

I got started on this when I say Oren Eini’s 15 line IoC container (here), and Ken Egozi’s 15 minute response (here).  They got me thinking about just how simple an IoC container could be.  Oren’s is interesting as an experiment, but it’s a little TOO minimal for normal usage.  Ken’s is interesting as well, but it only does transient registrations.  Mine does constructor injection for the following types of dependencies.

  • Instance – Returns a specific provided instance of a particular dependency each time.
  • Singleton – Creates a single instance of a dependency the first time it is requested, and returns it for all requests from then on.
  • Transient – Creates and returns a new instance of the dependency each time it is requested.
  • Delegate – Executes and returns the result of the provided method.

So, on to the code.  first of all, lets get some supporting stuff out of the way.  I’ve got a DependencyInfo class which encapsulates and describes an individual dependency using an enumeration of the supported types.  I’ve also defined an attribute used to mark which constructor to call in the event that the choice isn’t obvious (More on that later).  Finally, I have two dictionaries.  The first will store all the DependencyInfo objects, and the second will store actual instances of the dependencies for the "Instance" and "Singleton" dependency types.

public static class ClassRegistry
{
    [AttributeUsage(AttributeTargets.Constructor)]
    public class InjectionConstructorAttribute : Attribute
    { }

    private enum DependencyType
    {
        None = 0,   // Type is unset
        Delegate,   // A builder function
        Instance,   // A specific instance
        Singleton,  // Dynamically created singleton
        Transient   // Dynamically created transient object
    }

    private class DependencyInfo
    {
        public object Dependency { get; private set; }
        public DependencyType DependencyType { get; private set; }

        public DependencyInfo(DependencyType dependencyType, object dependency)
        {
            DependencyType = dependencyType;
            Dependency = dependency;
        }
    }

    private readonly static IDictionary<Type, DependencyInfo> dependencies = new Dictionary<Type, DependencyInfo>();
    private readonly static IDictionary<Type, object> instances = new Dictionary<Type, object>();

That’s all very interesting, but it doesn’t actually do anything.  In order for the container to return things to us, first we’ll have to "register" the types we want it to handle.  I’ve provided four Register methods, one for each of the dependency types.

public static void Register<TContract>(TContract instance)
{
    dependencies[typeof(TContract)] = new DependencyInfo(DependencyType.Instance, instance);
    instances[typeof(TContract)] = instance;
}

public static void Register<TContract, TImplementation>()
{
    Register<TContract, TImplementation>(false);
}

public static void Register<TContract, TImplementation>(bool isSingleton)
{
    DependencyType dependencyType = isSingleton ? DependencyType.Singleton : DependencyType.Transient;
    dependencies[typeof(TContract)] = new DependencyInfo(dependencyType, typeof(TImplementation));
}

public static void Register<TContract>(Func<TContract> builder)
{
    dependencies[typeof(TContract)] = new DependencyInfo(DependencyType.Delegate, builder);
}

The first Register method takes in a specific instance of a class.  We’re telling the container "If anyone asks for one of these, hand them this one".  The second handles transient registrations "If anyone asks for one of these, make up a new one and give it to them".  The third can be used to handle singletons by passing true as the sole argument "… make up a new one, and give back the same one from then on".  The final Register method is what’ll save your bacon in a lot of cases.  But before I can really give a good example of how, we’ll need to look at the other half of what IoC containers do, which is to hand back or "resolve" the classes you’ve registered.  There are two methods here.  The first is a generic method which is mainly for convenience.  It makes the syntax for using the container much nicer.  The real work is done by the second method which takes a type, and hands back an instance of that type.

public static TContract Resolve<TContract>()
{
    return (TContract)Resolve(typeof(TContract));
}

public static object Resolve(Type contract)
{
    if (!dependencies.ContainsKey(contract))
        throw new InvalidOperationException(string.Format("Unable to resolve type '{0}'.", contract));
    if (instances.ContainsKey(contract))
        return instances[contract];
    var dependency = dependencies[contract];
    if (dependency.DependencyType == DependencyType.Delegate)
        return ((Delegate)dependency.Dependency).DynamicInvoke();

    var constructorInfo = ((Type)dependency.Dependency).GetConstructors()
        .OrderByDescending(o => (o.GetCustomAttributes(typeof(InjectionConstructorAttribute), false).Count()))
        .ThenByDescending(o => (o.GetParameters().Length))
        .First();
    var parameterInfos = constructorInfo.GetParameters();

    object instance;
    if (parameterInfos.Length == 0)
    {
        instance = Activator.CreateInstance((Type)dependency.Dependency);
    }
    else
    {
        var parameters = new List<object>(parameterInfos.Length);
        foreach (ParameterInfo parameterInfo in parameterInfos)
            parameters.Add(Resolve(parameterInfo.ParameterType));
        instance = constructorInfo.Invoke(parameters.ToArray());
    }

    if (dependency.DependencyType == DependencyType.Singleton)
        instances[contract] = instance;

    return instance;
}

I won’t explain every line individually, but would like to explain the basic flow.  If you ask for any unregistered type you get an error.  If an instance has been cached for a type, you get that instance.  This handles both the Instance and Singleton registration cases.  Delegate registrations run the delegate that was originally handed in, and return the result.

Assuming we haven’t already returned, we’ll need to actually build something, so we’ll look at the type that was registered, and try to pick its "greediest" constructor.  This is the constructor that takes the most parameters.  If there’s a tie, we can use the presence of the InjectionConstructorAttribute as a tie-breaker.  If there’s still a tie, then all bets are off.  It won’t explode, but I won’t guarantee which constructor will get called either.  If you ever find yourself in this situation, then you are quite definitely doing something wrong, so you get what you deserve… there, I said it.  Once we’ve picked which constructor to use, we get its parameters, and recursively call Resolve to fill them in.  Finally, in the case of a singleton registration, we remember the new instance for any future requests.

Here are some examples of using the ClassRegistry in different ways.  These come directly from the ClassRegistry unit tests.

[TestMethod]
public void Can_resolve_registered_delegate()
{
    string value = GetRandom.String();
    ClassRegistry.Register<IFoo>(() => new Foo {Value = value});
    IFoo ifoo = ClassRegistry.Resolve<IFoo>();
    Assert.AreEqual(value, ifoo.Value);
}

[TestMethod]
public void Can_resolve_registered_instance()
{
    Bar bar = new Bar();
    ClassRegistry.Register<IBar>(bar);
    IBar ibar = ClassRegistry.Resolve<IBar>();
    Assert.AreSame(ibar, bar);
}

[TestMethod]
public void Can_resolve_transient_type()
{
    ClassRegistry.Register<Foo, Foo>();

    Foo foo1 = ClassRegistry.Resolve<Foo>();
    Foo foo2 = ClassRegistry.Resolve<Foo>();

    Assert.IsInstanceOfType(foo1, typeof(Foo));
    Assert.IsInstanceOfType(foo2, typeof(Foo));
    Assert.IsFalse(ReferenceEquals(foo1, foo2));
}

[TestMethod]
public void Can_resolve_singleton_type()
{
    ClassRegistry.Register<Bar, Bar>(true);

    IBar bar1 = ClassRegistry.Resolve<Bar>();
    IBar bar2 = ClassRegistry.Resolve<Bar>();

    Assert.IsInstanceOfType(bar1, typeof(Bar));
    Assert.IsInstanceOfType(bar2, typeof(Bar));
    Assert.IsTrue(ReferenceEquals(bar1, bar2));
}

Now I can explain why the "Delegate" registration type can be so useful.  It all comes down to that recursive "fill in the dependency’s dependencies" part.  Sometimes you just need to override the normal behavior, and the delegate registration type is what will make that possible. 

Let’s assume that our example ProductRepository class requires a Linq to Sql DataContext class in order to do its work, a NorthwindDataContext specifically.  Ordinarily, I would just register the NorthwindDataContext, and let the container do the rest.  The trouble is that when ClassRegistry goes to look for the "greediest" constructor, it’s going to find the one that takes a connection string.  Since we haven’t registered what to do when someone tries to resolve "string", nor would we want to, we’ll get an exception when we try to resolve NorthwindDataContext.  Since NorthwindDataContext is a generated class, I can’t just go in and mark the correct constructor with an attribute, either.  What I’ll do instead, is to tell ClassRegistry exactly what to do when someone asks for a NorthwindDataContext.

string connectionString = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
ClassRegistry.Register<NorthwindDataContext>(() => new NorthwindDataContext(connectionString));

I could take the call to ConfigurationManager, and put the whole thing in the call to the NorthwindDataContext constructor, but that would make it part of the delegate that gets run each time we try to resolve NorthwindDataContext.  Since this string won’t change over any single "run" of the service projects, that’s pretty wasteful.  Instead, we’ll figure out the connection string once, outside the delegate, and use the result in the registration.

Note: You’ll notice that the ClassRegistry doesn’t have any kind of "Reset" method.  That’s because in real life, you should never need one.  IoC registrations aren’t the sort of thing that are supposed to change while an application is running.  They’re the sort of thing you set up once when the application starts.  The only place this caused me a problem was in unit testing the container itself.  Since it’s a static class, once you’ve registered an instance of something, or resolved a singleton, you can never make it transient again.  I got around this problem by creating two separate "test case" classes.  One for instances and singletons, and one for transients.  But like I said, this isn’t something that ought to come up in the real world.

Posted in Uncategorized | Leave a comment

Teeny Tiny Templates

On my current project, I need to "fill in the blanks" in some text file templates.  It’s nothing we haven’t all done before using a series of string.Replace functions, and they work just fine, but they’re not shiny enough.  Also, when you think about it, each individual Replace operation iterates over the whole document again looking for the next tag to replace.  I decided to make something using regular expressions instead.  Something that would make just ONE pass over the document.  Here’s the result.

/// <summary>Simple, lightweight template parser.</summary>
/// <remarks>Replaces ASP-style tags in the form <%tag%> or <%=tag%> in strings.</remarks>
public class TemplateParser
{
    private const string tagPattern = @"<%=*\s*(?<1>.*?)\s*%>";
    public Dictionary<string, object> TagValues { get; private set; }

    public TemplateParser()
    {
        TagValues = new Dictionary<string, object>();
    }

    public string ParseString(string template)
    {
        return Regex.Replace(template, tagPattern, match =>
        {
            string tag = match.Groups[1].Value;
            return (TagValues.ContainsKey(tag) 
                ? TagValues[tag].ToString() 
                : string.Empty);
        });
    }

    public string ParseFile(string filename)
    {
        return ParseString(File.ReadAllText(filename));
    }
}
Yeah, that’s the whole thing… really.  To use it, you just new up a TemplateParser, add items to the TagValues dictionary, and then call ParseString, handing in the template.  Here’s a simple unit test that shows it in action.
[TestClass]
public class TemplateParserTests
{
    private TemplateParser Target { get; set; }

    [TestInitialize]
    public void TestInitialize()
    {
        Target = new TemplateParser();
        Target.TagValues["A"] = 1;
        Target.TagValues["B"] = "2";
        Target.TagValues["C"] = 3;
    }

    [TestMethod]
    public void ParseString_replaces_tags()
    {
        string template = "A<%A%>B<%=B%>C<% C %>";
        string expected = "A1B2C3";
        string actual = Target.ParseString(template);
        Assert.AreEqual(expected,actual);
    }
}

Now on to the discussion.  There’s really only one interesting part to this whole thing, and that’s the call to Regex.Replace.  You can see I’m passing it a lambda expression here.  This is the MatchEvaluator.  Its whole job is to take each match and do something with it.  It could trim spaces, make it all uppercase, anything really.  In my case, I’m going to pull the tag out of the match, and look it up in the dictionary.  If I find a value I’ll replace the whole match with it, otherwise I’ll replace it with an empty string.

I suppose the regular expression (@"<%=*\s*(?<1>.*?)\s*%>") itself could use a little explaining.  I’ve often described regular expressions as a "write-only" medium.  You can look up the parts you need in a reference, string them together, and make a working RegEx, but woe unto those who try to read it after it’s been created.  So let’s break it down into chunks.

@ – This just says "Yes, I know the following string is ugly, deal with it."
(Not really, but I like saying that)

<% – This is our opening asp-style tag

=* – There may or may not be an equals sign here.  Either way, I don’t care.

\s* – There may or may not be some whitespace here.  Same deal.

(?<1>.*?) – Once you’ve found the opening tag, and ignored the optional stuff, grab the next bit and call it "1".

\s* – Again with the optional whitespace.

%> – This is our closing tag.

So this looks for anything in the format <%ABC%>, and pays close attention to the "ABC" part in particular, giving it a name so that I can talk about it later.

And that’s it, really.  It’s going straight into my core library for future use.  I’m not saying that this will run your enterprise, but for those times when you just need to fill in some blanks, this’ll do the trick.

UPDATE:
If you don’t want the tags to be case-sensitive, you can specify a comparer when instantiating the Dictionary.  Just change the constructor to this:

public TemplateParser()
{
    TagValues = new Dictionary<string, object>(StringComparer.CurrentCultureIgnoreCase);
}
Posted in Uncategorized | Leave a comment