Google Fusion Tables

Posted on October 29 by bjn

During the development of www.miljoparkering.se we found ourselves in the need to visualize all the streets that we currently have data for in Malmö. We had received a large csv file with all the streets in Malmö containing street name and the date when its not allowed to park there. What we needed was a way to see all these streets on a map.

We googled and tried to find a method in the google maps api for adding multiple roads etc on a map. It was possible but not something easily done. All we needed was something that could produce a map quick and easy, not something we actually was going to use on www.miljoparkering.se.

Enter Google Fusion Table which is an awesome addition to google docs. All you do is create a new item in Google Docs called “Table". When you create a table you can chose to import existing data either from a file or from a spread sheet in google docs.

We imported our csv file with all the streets. The table looks pretty much like an ordinary spread sheet but with a few key differences.

table

There are a few columns that special for example the Location. In our case we only had a street name but you can use a GPS position as well. All we did after the import of our file we clicked “Visualize” and then “Map” and Google maps presented us with a pretty nice map.

map

That took us 10 minutes to produce. It can also create heat maps based on your data and other cool stuff. Very useful stuff!

Björn & Christer (from http://www.christer.dk/)


AutoMapper initialization

Posted on December 14 by bjn

AutoMapper is an excellent object-object mapper which uses a fluent configuration API and conventions to perform mapping with ease. Read more about AutoMapper here and my post here.

The Problem

When using AutoMapper you need to create a map between the objects you want to map. Of course, the maps must be created before any mapping is performed by AutoMapper. When I first started using AutoMapper I created a class (MappingConfigurer) to handle the creation of maps for AutoMapper. However, this class grew quite large over time when the system grew larger. After a while there were over 50 types getting mapped by AutoMapper. Having all of these initialized in the same class and method was a bit cumbersome after a while.

The Solution (well, one solution)

To remedy this situation I created an interface IMappingConfiguration. The interface only has one method Configure(), each type implementing this method imagemust create and configure the map its responsible for.

For example, the UserMapper is responsible for mapping from User to UserViewModel. The UserMapper implements the interface IMappingConfiguration. In the method Configure() it creates the map needed:

public void Configure() {
    Mapper.CreateMap<User, UserViewModel>()
   	.ForMember(viewModel => viewModel.Phone, 
		   opt => opt.MapFrom(user => user.Telephone.PhoneNumber)));
}

 

The UserMapper class also has a method Map to do the actual mapping.

public UserViewModel Map(User user) {
    return Mapper.Map<User, UserViewModel>(user);
}

 

imageNow I have a class which is responsible for mapping User. The problem is that instead of having one giant method to create all of these maps I now have quite a lot of small classes.

To execute the Configure() method of all these mapping classes I use reflection to find all the types implementing the interface and executing the method. The first version was just plain type navigation using LINQ and reflection. But after a while I realized I needed this kind of functionality in other projects as well and in other scenarios. What I really wanted was an easy way to find all types implementing an interface and executing a method on the interface. Different ways of doing it. I could have used Castle Windsor for resolving all types implementing the IMappingConfiguration interface but I’m not using Castle Windsor in all of my projects and I don’t want to integrate Windsor just for this reason.

I started by writing down the way I wanted to be expressed:

FindTypes
    .InAssembly(typeof(UserMapper).Assembly)
    .Implementing<IMappingConfiguration>()
    .Execute(mappingConfigurer => mappingConfigurer.Configure());

 

Higher level of abstraction and could potentially be used in other scenarios. For example, instead of executing a method it could return a list of types implementing the interface. Could even extend it to use Windsor. Instead of InAssembly, it could be InContainer(container), and it would use Windsor to resolve all types implementing the interface and still executing the method.

Code

This was the code I came up with (while watching an episode of Red Dwarf), so its at best a proof-of-concept Winking smile

The first part, which all it does it indicates where to look for types.

FindTypes.InAssembly(typeof(UserMapper).Assembly)

 

The code is quite simple, static method which returns an AssemblyNavigator with the supplied assembly as a parameter to the constructor.

public class FindTypes
    {
        public static AssemblyNavigator InAssembly(Assembly assembly)
        {
            return new AssemblyNavigator(assembly);
        }
    }

The next part of the expression, is just filtering out the types implementing the interface.

    .Implementing<IMappingConfiguration>()


The code uses LINQ to select the types in the assembly that is implementing the interface. The most difficult part of this code was figuring out the way to invoke IsAssignableFrom(), very easy to do it the other way around.

public class AssemblyNavigator
    {
        private readonly Assembly _assembly;

        public AssemblyNavigator(Assembly assembly)
        {
            _assembly = assembly;
        }

        public TypeList<T> Implementing<T>() where T : class
        {
            var listOfTypesImplementingInterface = from type in _assembly.GetTypes()
                                      where typeof(T).IsAssignableFrom(type) && type.IsClass
                                      select type;
            
            return new TypeList<T>(listOfTypesImplementingInterface);
        }
    }

Also, note the type.IsClass in the where-clause. You’ll need this or otherwise you might end up with the interface it self. This method returns a TypeList<T> which is used in the last part of the expression.

.Execute(mapping => mapping.Configure());


The Execute() method creates an instance of the type and invokes the method supplied as an Action<T> on the instance.

 public class TypeList<T> where T : class
    {
        private readonly IEnumerable<Type> _listOfTypes;

        public TypeList(IEnumerable<Type> listOfTypes)
        {
            _listOfTypes = listOfTypes;
        }

        public void Execute(Action<T> action)
        {
            foreach (var type in _listOfTypes)
            {
                var instanceOfType = Activator.CreateInstance(type) as T;
                action(instanceOfType);
            }
        }
    }

 

That’s all the code that was needed to get it working. If it’s usable, maybe, it hides the details of the implementation.
At least it was fun writing it.


This book was recommended to me by a friend when I said I was interested in presentation technique and design. The author (Garr Reynolds) explains simple ideas on presentation and design in a very easy to read manner. The book contains a lot of examples with slides “before” and “after”. Makes it very visual and easy to understand what Reynolds means with his ideas. Thanks to all the examples in the book it’s very easy to get ideas and be inspired to do better.

The book starts with the situation today, how PowerPoint is misused and mainly used for displaying bullet after bullet with text.

The next chapter gives ideas on how to prepare. Don’t start with PowerPoint and create

slides. Instead think through what you want your presentation to convey. Reynolds mentions two questions you should ask yourself whenever you do a presentation: “What’s Your Point? Why Does It Matter?” If you can’t answer those two questions, your audience probably wont be able to grasp the core message of your presentation either.

After you’ve worked out your core message and the flow of the presentation Reynolds continues to talk about design. Reynolds is a strong believer in simplicity, amplification through simplification. Keep it clean and simple.

The last part of the book is where most of the example slides are. With both a before and after image, visualizing the difference between the two.

Since the book is only about 200 pages and filled with pictures it’s very easy to read through it. I must say I felt very motivated to start on a new presentation and being able to use these tips n tricks I’ve picked up from the book.

I highly recommend this book!

Link to Amazon


Code Contracts

Posted on December 3 by bjn

Code Contracts is shipped with .NET Framework 4.0 in the namespace System.Diagnostics.Contracts. Code Contracts is a way to do runtime and static checking of pre and post conditions in the code. Like Debug.Assert on steroids. The biggest thumbs up is the static analysis which would generate compile errors if the condition is false. Sounded like an awesome thing so after creating a new project in VS2010 with target 4.0 I wrote the following method.

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Divide(10,0));
        }

        public static double Divide(int numerator, int denominator)
        { 
            Contract.Requires(denominator != 0);
            return numerator / denominator;
        }
    }

Compiled the program with no compile errors. Weird. Executed the program and thought I’d at least would get a runtime exception of some sort that the condition failed. Wrong again, no runtime exception. Well, only an ordinary DivideByZeroException was thrown. After inspecting the code with Reflector I could see that the Contract.Requires line had been removed by the compiler.

Googling a bit and I came to this page. OK, so even if Code Contracts are shipped with .NET FrameworPhoto by Céline Mackowiakk 4.0 the tools are not included and the code (Contract.Requires) will be removed by the compiler by default. The page told me to download the tools from devlabs.

While at that page I realized that the static checking of contracts is only available to PREMIUM and ULTIMATE versions of VS2010. That’s really bad. I totally agree with https://connect.microsoft.com/VisualStudio/feedback/details/481327/make-data-contract-static-checking-available-in-professional-edition?wa=wsignin1.0 and http://connect.microsoft.com/VisualStudio/feedback/details/106484/code-quality-tools-should-not-be-limited-to-team-system.

Anyways, to at least get runtime validation running with VS2010 Pro I had to:

1. Download the STANDARD edition of Code Contract tools from this page.

2. Go to to project properties and check the checkbox Perform runtime checking, which is found on the tab Code Contracts.

After that I got the runtime exception ContractException: precondition failed.

Lesson learned, there’s a price for code quality, it just happens to be the same price as for VS2010 Premium or Ultimate…


Just ordered some books

Posted on October 31 by bjn
 
Presentation Zen: Simple Ideas on Presentation Design and Delivery

Presentation designer and internationally acclaimed communications expert Garr Reynolds, creator of the most popular Web site on presentation design and delivery on the net — presentationzen.com — shares his experience in a provocative mix of illumination, inspiration, education, and guidance that will change the way you think about making presentations with PowerPoint or Keynote. Presentation Zen challenges the conventional wisdom of making "slide presentations" in today’s world and encourages you to think differently and more creatively about the preparation, design, and delivery of your presentations. Combining solid principles of design with the tenets of Zen simplicity, this book will help you along the path to simpler, more effective presentations.

The Pragmatic Programmer: From Journeyman to Master

Straight from the programming trenches, The Pragmatic Programmer cuts through the increasing specialization and technicalities of modern software development to examine the core process--taking a requirement and producing working, maintainable code that delights its users. It covers topics ranging from personal responsibility and career development to architectural techniques for keeping your code flexible and easy to adapt and reuse.

Kanban

The Kanban Method will improve your existing development process. This book explains why and shows you how to get started using it right now. Case studies and illustrations make it easy to adopt the improvements you need for your unique situation.
Limiting work in progress and visualizing your work are just the beginning. Optimize your processes to achieve better prioritization, higher quality work and a sustainable pace.


Why Software Sucks...and What You Can Do About It

It’s no secret that software sucks. You know that from personal experience, whether you use computers for work or personal tasks. In this book, programming insider David Platt explains why that’s the case and, more importantly, why it doesn’t have to be that way. And he explains it in plain, jargon-free English that’s a joy to read, using real-world examples with which you’re already familiar. In the end, he suggests what you, as a typical user, without a technical background, can do about this sad state of our software—how you, as an informed consumer, don’t have to take the abuse that bad software dishes out.

The No Asshole Rule: Building a Civilized Workplace and Surviving One That Isn't

We all know them or know of them--the jerks and bullies at work who demean, criticize, and sap the energy of others, usually their underlings. It could be the notorious bad boss or the jealous coworker, but everyone agrees that they make life miserable for their victims and create a hostile and emotionally stifling environment. Fed up with how these creeps treat others and poison the workplace, Sutton declares war and comes out calling them exactly what they are--"certified assholes." Caricatured in sitcoms such as The Office, these brutes are too often tolerated until irreparable damage is done to individuals and the organization as a whole.

Thinkertoys: A Handbook of Creative-Thinking Techniques (2nd Edition)

THINKERTOYS will teach you how to generate new ideas for businesses, markets, sales techniques, and products and product extensions. Packed with fun and practical tools and exercises, it outlines 30 practical linear and intuitive techniques that can be used by individuals or groups to tackle and solve business problems in fresh, creative ways.

Rework

Explores a different reality of business. This book is suitable for hardcore entrepreneurs, small-business owners, people stuck in day jobs who want to get out, and artists who don't want to starve.

With the release of .NET framework 4 the Task Parallel Library (TPL) is now a part of the core framework without the need to install any extensions. The TPL exposes a higher level of abstraction for creating threads. It includes a new way of constructoring For and ForEach loops, and make these run in parallel without manually creating and managing threads.

Under the hood the TPL will create a number of threads. The number of threads will vary depending on the number of cores in the system running the application. Microsoft has completely redone the threadpool for .NET 4 CLR for it to be optimized for computers with a larger number of cores. Check out this excellent explanation of how it works in detail.

The TPL will reduce the complexity of your application code vastly and enables the developers to utilize all cores in the system more efficiently. Instead of having to manually creating threads its now as easy as calling Parallel.ForEach() for executing some operations in parallel.

Lets show an example. This snippets creates a list of 10 dates and then loops over each date and executes the method SimulateLongRunningMethod, which sleeps for one second and then just prints out the argument.

            
List<DateTime> dates = new List<DateTime>();
for (int i = 0; i < 10; i++)
	dates.Add(DateTime.Now.AddDays(i));
Stopwatch watch = new Stopwatch();
watch.Start();
foreach (var date in dates)
{
	SimulateLongRunningMethod(date);
}
watch.Stop();
Console.WriteLine("Took: " + watch.Elapsed);
private void SimulateLongRunningMethod(DateTime date)
{
	Thread.Sleep(1 * 1000);
        Console.WriteLine(date);
}

The output of the above would be (depending on when you run it of course):

2010-10-03 20:08:58
2010-10-04 20:08:58
2010-10-05 20:08:58
2010-10-06 20:08:58
2010-10-07 20:08:58
2010-10-08 20:08:58
2010-10-09 20:08:58
2010-10-10 20:08:58
2010-10-11 20:08:58
2010-10-12 20:08:58
Took: 00:00:10.0730300

All of the dates are in order and it took 10 seconds to do the execution.

Changing the foreach loop to Parallel.ForEach is easy.

Instead of the foreach we saw in the previous example it is now:

Parallel.ForEach(dates, date => SimulateLongRunningMethod(date));
The output is:

2010-10-04 20:13:51
2010-10-08 20:13:51
2010-10-03 20:13:51
2010-10-09 20:13:51
2010-10-05 20:13:51
2010-10-10 20:13:51
2010-10-07 20:13:51
2010-10-12 20:13:51
2010-10-06 20:13:51
2010-10-11 20:13:51
Took: 00:00:03.0930603

This is running on my dual core computer, your result may vary. The result is not in order anymore as each thread is pulling data from the list. The execution time was decreased with approximately 7 seconds.

That’s how easy it is to change your single threaded application into an application that will utilize all the available cores in your machine. Be careful though, concurrency issues might be exposed in your application if it hasn’t been designed for multi-thread execution in the beginning.


Just tried to build Sharp Architecture through hornget:

horn.exe –install:sharp.architecture

It failed with the message “The type or namespace name 'AutoMap' does not exist in the namespace 'FluentNHibernate' (are you missing an assembly reference?)” and 16 errors with the same message. All I needed to do was to open the SharpArch.sln in c:\users\<my username>\.horn\frameworks\sharp.architecture\working\src\sharparch and Resharper immediately told me to add a using statement to FluentNHibernate.Automapping. Seems like they’ve changed namespace in fluent nhibernate from FluentNHibernate.AutoMap to FluentNHibernate.Automapping. After changing this and made sure that I could compile the solution I just ran the horn command line tool again and it built everything successfully. Just remember to revert the changes or wipe the folder clean so there wont be any conflicts the next time…


AutoMapper is a convention-based object-object mapper as described by the AutoMapper project homepage (http://www.codeplex.com/AutoMapper).

Basically it will map properties between two different objects (different types). If the name of the properties are the same it just works. It works with lists and nested types. All you need to do is initiate the mapping and then do the actual mapping. Here’s an example when mapping from an instance of type User to a UserViewModel instance.

   1: Mapper.CreateMap<User, UserViewModel>;
   2: User user = new UserRepository().Get(1);
   3: UserViewModel userViewModel = Mapper.Map<User, UserViewModel>();

If the structure/name isn’t something that AutoMapper can figure out how to map then it’s possible to add manual mappings with lambda expressions. For example if the User has a Telephone type with a property called PhoneNumber and the UserViewModel has a basic string property called Phone:

   1: Mapper.CreateMap<User, UserViewModel>()
   2:     .ForMember(viewModel => viewModel.Phone, opt => opt.MapFrom(user => user.Telephone.PhoneNumber)));
   3:  
   4:  

Very cool stuff indeed. I introduced AutoMapper in one of the projects I’m currently working on and since it was mostly 1-1 mapping that was done, I reduced over 600 lines of code in two hours in the project. Since I just replaced the body of my mapper methods with the call to AutoMapper that meant that all the unit tests I had written could verify that everything worked as before.


Currently I’m building a image gallery component for a website where the user is able to upload images and they are displayed in a gallery using http://pikachoose.com. I have one action on my controller called Download which takes the id of the image that should be downloaded as an argument. It worked fine using this code (in the beginning):

   1: public ActionResult Download(int id)
   2: {
   3:     var image = Repository.GetById(id);
   4:  
   5:     return File(image.Data, image.ContentType, image.Filename);
   6: }

The problem with this code only showed up with some images. The problem is the last parameter to File(), image.Filename. Which is the filename of the original image the user has uploaded. If the filename contains “invalid characters” ASP.NET MVC throws this exception:

[FormatException: An invalid character was found in the mail header.]
   System.Net.Mime.MailBnfHelper.GetTokenOrQuotedString(String data, StringBuilder builder) 
   System.Net.Mime.ContentDisposition.ToString() +270
   System.Web.Mvc.FileResult.ExecuteResult(ControllerContext context) +164

 

The part that confused me was “in the mail header”. Suppose they’re reusing some mail component for mime parsing. To fix it I only had to make sure that the filename didn’t had any invalid characters, I did it by setting it to the filename I use on the server instead of the filename submitted by the user.


When using the automapping feature of Fluent NHibernate you may encounter some problems when storing large text data in the database. Below is a class which gets mapped by the automapper in Fluent NHibernate. Both Title and TextContent are strings. The difference between them is that the TextContent property might store a large chunk of text.

image

The default automapper in Fluent NHibernate will create a table with a nvarchar(255) column for both the Title and TextContent. This is fine for most cases but in this case the TextContent property may contain much more than 255 characters. Trying to save the entity with more than 255 characters stored in TextContent will give throw an exception saying:

System.Data.SqlClient.SqlException: String or binary data would be truncated.
The statement has been terminated.

This is the same problem you might run into when using Castle ActiveRecord which also will do a default nvarchar(255) for a string property. It's quite simple to override the default automapper in Fluent NHibernate to change the column in the database into a text type for example. All you need to do is create a class which implements the interface IAutoMappingOverride<T> and add it to the configuration.

   1: public class HeadlineOverride : IAutoMappingOverride<Headline>
   2: {
   3:         public void Override(AutoMap<Headline> mapping)
   4:         {
   5:             mapping.Map(t => t.TextContent).CustomTypeIs("StringClob").CustomSqlTypeIs("text");
   6:         }
   7: }


All you need to do a manual mapping of the field and set the custom type to StringClob and SqlType to text. All the other properties will still be mapped by the automapper in Fluent NHibernate. To make sure that Fluent NHibernate will load the override into the configuration you need to set it up.

   1: return AutoPersistenceModel
   2:        .MapEntitiesFromAssemblyOf<Headline>()
   3:        .Where(t => t.Namespace == "Incendi.Models")
   4:        .UseOverridesFromAssemblyOf<HeadlineOverride>();

On line #4 it tells the automapper to use the overrides from the assembly where HeadlineOverride is declared. If you drop/create the schema with this updated configuration you'll end up with a text type on the TextContent column instead of a varchar(255).