The beauty that are anonymous methods

I recently came to see code, that used anonymous methods in a very elegant fashion. As very simplified example:

using System;
using System.Collections.Generic;
 
namespace ConsoleApplication2 {
  class Program {
    static void Main ( string[] args ) {
      new List<string> { "a", "b", "c", "d" }.ForEach( print => Console.WriteLine( print ) );
    }
  }
}

Create Elegant Code With Anonymous Methods, Iterators, And Partial Classes is an article on MSDN which is a good starting point. Used wisely on a chosen place, it makes the code short and readable. Going crazy makes it essentially unmaintainable. As for me, I think I should get more familiar with that along with LINQ and the ideas behind.

Windows 8 app hero cake

Windows 8 app hero cake

We received a nice cake from Microsoft today for our participation on Windows 8 launch. We created a first, early release for a photo book application on Windows 8 together with FUJIFILM.

Photobook app tile

Visit http://www.syzygy.de (German) to see what we do more.

EF: Using HierarchyId with EntityFramework

While evaluating EntityFramework again for a possible use in a new project I decided to tackle HierarchyId support first. The sad news first: there is no built-in way to work with HierarchyIds. The same goes for spatial data, at least for the time being. Both data types are user defined data types, which makes it hard to nearly impossible for a ORM to be database agnostic and supporting that type. Julie Lerman (she has written the well known Programming Entity Framework book) has published an article together with Jason Follas) how to read spatial data (note: the Entity Framework June 2011 CTP introduced a much more streamlined, integrated way of doing this). Essentially this should be possible with HierarchyId, too.

To setup my testing I created a EmployeeData table with data in a new SQL Server 2008 R2 database (I don’t like fiddling in existing databases, so I create and drop test databases frequently). I used the sample code from here, as I’m as lazy as a developer should be. Note: there is a download link at the end of the page. It will save you some time, as the double colons between hierarchyid and GetRoot() aren’t visible on the page and you’ll have to fill them in and add a primary key to the table. Latter is important, as EF doesn’t work on key-less tables.

Data setup, now we need to make EF happy. According to the aforementioned blog entry you need to cast to a binary column, this is best done with a view.

The view’s code is straightforward, once you know which type you should use:

CREATE VIEW [dbo].[EmployeeDemoView] AS 
    SELECT
        CONVERT( [varbinary](MAX), OrgNode, 0) OrgNode, 
        EmployeeID, 
        LoginID, 
        Title, 
        HireDate
    FROM
        dbo.EmployeeDemo

Now I created a simple console application and added a reference to EF using NuGet (if you don’t know this, do yourself a favor and learn it – it’ll save you tons of time). Add a new model and connect it to the database. I’ve omitted the table, just went for the view.

All columns were there, moved on to coding. A bit of googling and looking into the SQL Server types unveiled the Read method of SqlHierarchyId. What I had to learn, was to successfulle read the stream, the variable has to be initialized as a Null SqlHierarchyId. So my testing looked like this:

using ( ConsoleApplication1.MyDataContextEntities ctx = new MyDataContextEntities() ) {
  var employees = ctx.EmployeeDemoView.ToList();
  foreach ( var item in employees ) {
    var hierarchyID = Microsoft.SqlServer.Types.SqlHierarchyId.Null;
    var loginID = item.LoginID;
    using ( var stream = new System.IO.MemoryStream( item.OrgNode ) ) {
      using ( var rdr = new System.IO.BinaryReader( stream ) ) {
        hierarchyID.Read( rdr );
      }
    }
    Console.WriteLine( "{0},{1}", loginID, hierarchyID );
  }
}

There is no way to query for an HierarchyId, but at least you can use it.

Visual Studio Addons

I’m using Visual Studio and event though it’s forcing me to restart it from time to time, I’m still the opinion it’s one the best IDE available currently. One reason for that, is it’s flexibility to have more functionality added by plugins and extensions. I’m going to list the one’s I’m using here and in upcoming posts. For me as a checlist when setting up my PC sometime in the future, for others as a way to find new ones.

I’m going to start with those on my home computer:

 

  • DevExpress CodeRush and DevExpress Refactor! Pro
  • NuGet
  • Power Productivity Tools

Plugins adding functionality to DX:

  • CR_Documentor
  • DX_StackNav
  • DX_InterfaceNav
  • DX_GenerateProxy
  • CR_StringFormatter
  • CR_BlockPainterPlus

Windows Phone Marketplace Test Kit

When you’re creating an app for Windows Phone, it’s best to have it right up front when submitting to the marketplace. The Windows Phone Marketplace Test Kit is one tool that can help you. Once set up correctly (by providing graphical assets like app store icon), it enables you to test automatically and provides a list of things to consider. Really helpful and I encourage everybody to use such a help.

This is what I have done today. The past days I’ve actually used my application and some data was stored on my device. Well, after starting the tests on my phone (the emulator doesn’t make sense and even didn’t worked on my machine) my collected data was lost. Nothing mission critical, nevertheless to a big surprise. You’ve been warned ;)

Theme resources

You can find information about theme resources when programming for WP7 here:

http://msdn.microsoft.com/en-us/library/ff769552%28v=vs.92%29.aspx