Mapping an Pivot Query in EntityFramework6

At work I had a challenging problem in the last days (in fact I like challenging problems and they’re not that rare which is why I like my job).

This time the problem was this:

  1. Let T be a table with columns C and X.
  2. Let the column E be of an enum type with a short, static defined values E1, E2, E3.
  3. Let the column X be a space separated list of tokens (values look like „ABC“, „ABC DEF“, „XUT OSH OJR“, with an arbitrary number of tokens).
  4. We look for the following information:
    For a given token x in the values of X get a tuple R = (#(E1), #(E2), #(E3)), where #(Ei) denotes the number of rows in T, where x is included in X and the row has value Ei in column E.

Mapping an Pivot Query in EntityFramework6 weiterlesen

Ozcode’s export object(s) feature, a first review (part 1)

Nearly half a year ago, in November 2015 I wrote a first article about a feature of the OzCode, an awesome debugging tool that plugs into Visual Studio and enhances it by many features in the debugging field I don’t want to miss again.

Recently Version 2.0 of OzCode has been published and today I finally found the time to test it in more depth. The whole article is based on OzCode version

For testing I wrote a very small test project with some XUnit tests. Unit testing as such is not necessary in this case, but these tests are easy to run as a single, separated piece of code, so it was the easiest way to go. The test project is published on GitHub as ozcode-export-test.

When OzCode 2.0 was announced on twitter and I asked for a test version, they finally answered (fixed the link here that was a wrong shortlink on Twitter due to missing space):

@jongleur1983 Wait is over Doesn’t address the issues you mentioned in your post yet, but we will be looking into that.

So I do not expect any issue mentioned in my first post to be addressed, but nevertheless the feature might be quite useful, so let’s give it a try.

As this article get’s lengthy again and it’s past midnight, I decided to split it, so this post is about single-object exports only for now, I’ll try to write a next one about exporting object graphs assembled from more than one object. Ozcode’s export object(s) feature, a first review (part 1) weiterlesen

Wrapping Arbitrary HTML to Assign a common Universal Attribute

A few days ago I checked the HTML validity of my own blog. I have to admit that I didn’t do that after I wrote the jugglingPostLang plugin (wrote about that in my blog post Simple Language-Defined Posts in WordPress) for WordPress. I realized that my plugin produced invalid html in many cases, so that was an issue:

The issue is, that <span> elements forbid block level content, while <div> elements are not allowed as a child of any element that only accepts phrasing content (which is roughly running text and what’s inside).

To solve that I read through the html specifications to find a solution but unfortunately there isn’t a simple solution on the horizon.

In the following I’m going to summarize the html specification details on the topic and give a rough sketch on the algorithm to determine what’s a valid wrapping node. Wrapping Arbitrary HTML to Assign a common Universal Attribute weiterlesen

Strange behaviour of String Interpolation Refactorings in VisualStudio 2015/Roslyn/Resharper

We switched to VisualStudio 2015 at work a few days ago, using Resharper inside as before. VisualStudio has some great new features in this version, a few bugs, and some strange behaviours I don’t understand.

In some articles I’m going to talk about some strange refactoring suggestions I discovered, starting with string interpolations here.

Just to ask beforehand: Is there any way to identify where a refactoring comes from (is it a suggestion and/or implementation of Roslyn or Resharper?) Strange behaviour of String Interpolation Refactorings in VisualStudio 2015/Roslyn/Resharper weiterlesen

Open Question: How to always run a custom tool in Visual Studio on Project Build?

In a project at work we have some T4 templates running in a Visual Studio (2015) environment. Those templates should run whenever some dependent files have changed. As it’s kind of a compilation step, a best fit, I thought, would be to run the templates on every (re-)build of the project, but how to achieve that? Open Question: How to always run a custom tool in Visual Studio on Project Build? weiterlesen

Simple Language-Defined posts in WordPress

For different audiences I write blog posts in different languages. Developers usually understand English and often even talk in English, it’s their lingua franca of the programming domain.
On the other hand other articles are about more local topics, so their target audience is mainly Germans. Here I prefer writing in my mother tongue. Unfortunately there seems to be no easy and simple solution to define the language per article in WordPress, so I considered to write one myself.

The plugin is available for download on github now.

Read further for more details on What it does, How it is done, Why I did it and What could be added. Simple Language-Defined posts in WordPress weiterlesen

IEnumerable, ICollection, IReadOnlyCollection – an API analysis on .NET and XUnit

Today for the first time since I use XUnit I wrote a theory where the individual test parameters could not be easily defined as compile time constants.

In the next chapter I’m going to explain the motivation and the use case that arose here, before the next chapter will try to give a summary over the .NET API that’s behind that issue and what’s wong with it. IEnumerable, ICollection, IReadOnlyCollection – an API analysis on .NET and XUnit weiterlesen

Ozcode upcoming feature tweet: serializing objects for debugging

Currently oz_code asks about what developers want to see in future versions of their Visual Studio extensions to simplify and enhance debugging experience.

We use oz_code at work for complex .NET/WPF desktop applications and it is an invaluable tool for various use cases while debugging.

Today I read this tweet from @oz_code:

Would you like to be able to serialize objects from your debugging session and then use those objects in a Unit Test? (feature planning)

And I didn’t have to think twice about it: yes! But it didn’t take a minute thinking about what would be possible – and was a developer myself: What might be challenges of such a feature (even though I don’t have to implement it myself).

Ozcode upcoming feature tweet: serializing objects for debugging weiterlesen

Customized WordPress-Template for Category-Subtree

For the Advent calendar categories I wanted to have a custom template that applies to all categories below the Adventskalender category (that one included).

WordPress has a great Template system, using inheritance between templates, as shown in the Codex. Utilizing this system it is possible to customize all category pages or one category page in particular – but there seems to be no way to customize a set of categories based on the category hierarchy.

On the other hand WordPress contains a function to get a Breadcrumb trail from the root to the current category. For this function, get_category_parents() no non-grapical equivalent seems to exist unfortunately.

On the other hand the code is quite simple: Start at the category of your choice and recursively get the parent category up to the root.

My current solution is based on a fixed „root“ category, as that is what I want to have (more ideas on how the template system could be extended nicely see below).

My solution utilizes the action hook  ‚category_template‘ which is not documented unfortunately in the codex (although listed in the filter reference). The following is the code listing of the function:

function get_advent_category_template( $archive_template )
  // get the category:
  $catId = get_query_var('cat');
  $found = false;

  $yourcat = get_category($catId);
  do {
    if ($yourcat->slug == 'adventskalender')
      return dirname(__FILE__).'/category-adventskalender.php';
    $yourcat = get_category($yourcat->parent);
  } while (true);

  // not found in the parent hierarchy, return the default template:
  return $archive_template;

The parameter of this function is the default template that would be applied without the hook implemented here. We have to detect if we’re in a matching category and if so, overwrite that template by returning our customized one. Therefore the loop from the current category up to the root category and search for our anchor category which is the root for the categories we want to apply the custom template for.

If found, we return the custom template file, otherwise we return the default one.

Now this function has to be registered, and that’s it:

add_filter( 'category_template', 'get_advent_category_template' ) ;

How the WordPress Template system could be extended

When I stumbled over this issue I asked in the WordPress IRC channel after not seeing a proper solution, where I was told that it is a quite common problem, and more people asked for a solution in the past.

In fact it would be quite easy to extend the WordPress template system by another template „class“ in the second line of the graphical overview. Inserting another variant between category-$id.php and category.php would fit nicely from a logical point of view.

A Category archive that is defined for a single category is preferred (left of the new one), but the tree-based category template (the new one) is preferred over the generic category template.

Test database instances

OSM is a big database. Postgresql/Postgis is an excellent database management system, but for local tests on my notebook I have a problem: I have only one slow harddisk that is shared by postgresql tablespace, tomcat, linux swap partition and firefox.

Even with 4 GB RAM that leads to a problem, if the database tables become too big to fit into memory. Especially for the mapnik database schema some actions require full table scans on big tables (that’s one reason why I would prefer a different database schema).

A worldwide osm database is to big for my harddisk (or at least I don’t want to give that much disk space for testing currently here), so I first tried a germany extract database (built out of the Geofabrik’s pbf Germany extract), but even that’s too big for fast processing as at least one query of the exploration page needs a full sequential table scan through the ways table. As a result the calculation of any exploration page takes several minutes, which isn’t acceptable for debugging (as it would not be acceptable for a live environment, of course).

After all I had to restrict myself to an even smaller part of the world.

To apply that I first thought about cutting a boundingbox around Paderborn by osm2pgsql directly:

osm2pgsql -s -C 768 -c -p paderborn -d lalm -U lalm -W -H -P 5432 
          -b 8.6,51.65,8.9,51.8 --hstore --style ./ germany.osm.pbf

The problem here is, that osm2pgsql ignores the bounding box (parameter -b) to write the slim tables. These are written containing every node of the import file, which takes quite long and uses lot’s of disk space.

The second attempt is better: I first used osmosis to get the bounding box extract and then imported the whole resulting file using osm2pgsql. The corresponding osmosis command is

osmosis --read-pbf file="germany.osm.pbf" 
        --bounding-box top=51.8 left=8.6 bottom=51.65 right=8.9 
        --write-pbf file="paderborn.osm.pbf" compress=none

I decided not to compress the paderborn.osm.pbf file as that speeds up reading the file for osm2pgsql afterwards. The osm2pgsql import then is again the same as above except the -b parameter as now the bbox corresponds to the whole file already.