Modelling SQL Server data into complex POCOs using LINQ

I recently had a task which involved modelling data from SQL Server into reasonably complex POCOs (plain old C# objects). Rather than adopt an ORM to enforce the relationship between the database and the POCOs, I decided to use LINQ to model the data into the correct form.

If we take these two data model classes below:

We notice that there is a one-to-N relationship between the Product class and the Asset class. There is an intimated many-to-many relationship between assets and products here – in the sense that a photographic asset can be linked to many products – but this is not enforced. Rather than implementing a full scale object relational mapper (ORM) solution to tying up the database to the POCO, LINQ’s GroupBy can be used in combination with a sub GroupBy query to first group by product code (there may be several entries in the table for each product code depending on how many assets are linked to the Product), and then group by asset ID a second time to pull in assets that appear across multiple products.

 

Expanding my knowledge: learning new programming languages

In an effort to expand my knowledge, I’ve decided to use some of my week nights after work to learn some other programming languages. Working as a .NET developer is sometimes a bit of a bubble – there is so much to know across the .NET ecosystem and often the underlying building blocks of the web are abstracted out and hidden under the hood of the framework itself that it’s sometimes easy to forget how the web actually works. So, with that in mind, I have decided to focus on other web-based languages – each month I’m going to focus on one particular programming language. I’ve settled on a fairly comprehensive list, which can be seen below:

  • Node.js
  • Python
  • Scala w/ Play framework
  • Java w/ Struts
  • PHP w/ Zend or maybe CakePHP

The coming posts will recap over some little experiments I’ve conducted in each language.

MB Interactive Bake-Off – 14th October 2014

So it’s that time of the year again – the MB Interactive Bake Off series continues. This time around the contest is dedicated to our graphic designer, Paul “Minnsy” Minns. Entries into the competition will be judged on the Minnsyness factor – presumed to be not quite as good as the X Factor.  As usual, I have prepared a fairly shoddy entry to the contest. I decided to design a Photoshop cake – I started with a bog standard cake mix by Betty Crocker and adorned it with the Photoshop logo. Not rocket science I’m afraid. The cake was made and looked like this undecorated:

IMG_1062

Then I went a bit crazy with the iceing and ended up with this:

IMG_1066

Overall, as someone who is not very artistic and has no discernable baking skills, I’m very happy with the result.

Here are some of the other entries:

James's White chocolate wedding cake

James’s White chocolate wedding cake

Benny's Spurs cake

Benny’s Spurs cake

 

Rocky road bites

Rocky road bites

 

Brownies

Brownies

 

Mark's amazing chocolate fudge NYC cake

Mark’s amazing chocolate fudge NYC cake

 

Until next time,

Adam

 

Handling Exceptions thrown by C# Tasks

Recently I built an interactive real-time map on top of the (fairly) new Twitter Streaming API – in order to keep the stream to the API open, I used the C# Tasks library to process the connection to Twitter. One problem I encountered was that often web exceptions would occur in the lifetime of a task dedicated to streaming. I created several custom “watcher” classes to notify me when an exception occured. What I didn’t know at that point in time is that you can handle exceptions thrown by Tasks in a try/catch block as below:

I will be applying this to something non-trivial very soon to see what the possibilities are!

Consuming your GitHub Recent Activity feed

If you have a GitHub account and actively push code to it and you also have a website then this post is for you! Oh and also it’s in C#. Deal with it.

Prerequisites over and here we go. Very quick jobbie this was although I still hate XML. Whose idea was it?!

Here is the code that you can just pop into your Controller, Model or wherever else you want (replacing my username adaam2 with your GitHub username):

And just stick this in your View:

And hey presto you have your recent commits for GitHub for all repos.