The Annotated Turing

Based on Bob Martin’s recommendation in his Clean Coder’s webcast, I picked up The Annotated Turing by Charles Petzold. 


What a fun read!  Petzold really does a good job of breaking down the original paper into manageable chunks – and gives just enough background to place the paper’s ideas in context.  I really had fun digging though my gray cells of the difference among the different kids of numbers (real, imaginary, etc…) .  I am about half way through the book  and I plan to re-read it once I finish it.  If you work in IT for a living, this is worth a read – and re-read….

Town Of Cary ISAB

I was reappointed to a second term on the Town Of Cary’s Information Services Advisory Board (ISAB).  Not only that, I was made chair:


Panzer Redux: WinRT

I was thinking of porting the Windows7 game that I created onto Windwos RT.  Since they are both XAML based, I thought it would be a snap.  I thought wrong.  Part of the problem is that I wrote the original game with poor architecture.  The other part of the problem is that Windows RT makes it hard to port existing code.

I first wanted to separate the game interface from the core game functions.  I created a new project and brought in all of the non-graphical components:



The problem is that every actor seems to have a dependency on the Game class.  For example:



So I need to break the dependencies between these factories and their implementation.  I decided on Property Injection versus constructor injection as a matter of convention because I am not sure on the number of dependencies for each factory and I hate really long constructors.  So my refactoring looks like:

public class ArmyFactory
    public ScenarioFactory ScenarioFactory { get; set; }
    public UnitFactory UnitFactory { get; set; }

    public ArmyInfo CreateArmy(int scenarioId, int sideId)
        ArmyInfo armyInfo = new ArmyInfo();
        armyInfo.SideId = sideId;

        List<ScenarioUnit> scenarioUnits = ScenarioFactory.ScenarioUnitFactory.GetScenarioUnits(scenarioId);
        foreach (ScenarioUnit scenarioUnit in scenarioUnits)
            int unitId = 0;
            if (scenarioUnit.SideId == sideId)
                armyInfo.Units.Add(UnitFactory.CreateUnit(unitId, scenarioUnit));

        return armyInfo;

I then was confronted with the fact that my game engine has dependencies on Windows Phone:


Looking at the original code:


So I need a way of loading from an XML file – or do I?  What if the EquipmentClass data was stored in something other than XML?  Do I really want my factory breaking?  I then looked at ScenarioTile – I had loaded data from either a web service or from an XML file.

I then realized that the factory is a translator –>  In goes XML, SOAP, ADO.NET recordset, whatever, out always pops the instantiated objects.  XML was used because that is the de-facto standard for Win Phones.  I also see that my Lookup data is in XML.  Therefore, what I need is a platform-agnostic XML Stream Reader.  Unfortunately, that is not in the cards: Windows Phone has its implementation, System.IO has its implementation, and Windows 8 RT has its implementation.   I then refactored the factory to take in an XML file of a certain structure and spit out the classes.  All of the implementation that is platform specific I removed.


So where do I implement the actual XML load?  In the calling application.  Windows Phone 7.x uses XElement, WindowsRT does, and I have to assume Windows Phone 8 does too.  I wonder if all platforms shared the same serialization implementation? 

Ugh!  looks like I picked the wrong week to quit drinkin

Web && App sub-config files

One of the cool features of VS2010 and web deploy is the ability to have sub-config files.  that way you don’t have to comment out (and remember) sections of your web .config for each environment.  You get this feature out of the box with web.config files when you create a new web project:



In the master web.config, you pretty much have the same as your normally do:image

In the Sub-Config file, you put in the updated value and some special xsd syntax:


When you publish the website, you only have 1 web.config file. What happens is that the sub-config files have their values moved into the master web.config. For example, if you put a Debug connection String in the Web.Debug.Config, that entry is copied into the master web.config.

When you publish with the Solution Configuration set to Debug:


Your Web.Config gets updated:


Note that if you run the website locally inside Visual Studio (Hitting F5), it does not transform because you are not publishing. You need to publish to implement the change.

A good article is this one and a good webcast is here.

But what about app.configs?  Good question – because this is based on web deploy, you don’t get this feature in an out of the box desktop project.  There are two good posts that explain how to update your projects so they support app configs.  The most concise is this one.  The problem is that it is incomplete, After you follow Phil’s changes to the project file, you also need to follow this posts on how to update you sub-config to use the xdt.  For example:


Once you do that, you get the master app.config updated.  The other cool thing is that you don’t have to use any kind of publishing – hit F5 and the .config in your target environment folder is crated appropriately.

Lambda Expressions and Inner Joins

Consider Northwind database represented by Entity Framework:


Notice that to get an employee’s region, you have to traverse the territory table. In TSQL, you would probably write something like this:

Select * from Employees as e 
inner join EmployeeTerritories as et on e.EmployeeID = et.EmployeeID 
inner join Territories as t on et.TerritoryID = t.TerritoryID 
inner join Region as r on t.RegionID = r.RegionID 
where r.RegionDescription = 'Eastern' 

(Note that EntityFramework does not show the EmployeeTerritory crosswalk table)

Using Lambdas, you have to get to region via territories, but that property is not available from the employee class:


Enter the "Any" keyword – you can chain classes using the "Any" keyword just like you are doing inner joins in TSQL (or join in Linq):

For example, all employees in the eastern region is written like so:

var result = entities.Employees.Where(e => e.Territories.Any(t => t.Region.RegionDescription == "Eastern"));