Cast Hacking

Have you ever wanted to cast a base class into a subclass? Consider these two classes:

public class Animal { }
public class Dog : Animal { }

Because of the inheritance we can create an Animal from a Dog because we know that Dog has all of the necessary properties to set on Animal. Quick example:

var dog = new Dog();
var animal = (Animal) dog;

This works in .NET. But what if you wanted to create a Dog from an Animal? I mean, a dog is an animal, right? Unfortunately Dog may have some properties that Animal doesn't and so you will get an exception at runtime when trying to cast from an Animal to a Dog:

var animal = new Animal();
var dog = (Dog) animal;

Causes this runtime exception:

An unhandled exception of type 'System.InvalidCastException' occurred.
Additional information: Unable to cast object of type 'Animal' to type 'Dog'.

Getting around this casting issue means you would have to create a new Animal, then for every property on Animal set the value to the corresponding property on Dog. What pain. Who has time for that? What happens when you add a new property to Animal - will you remember to update this property-copying code as well? It wasn't intended specifically for getting around casting issues but for property copying Automapper is awesome. It does pretty much what you would expect. It maps the properties of one class to another so you don't have a boring and messy slop of code to maintain. Here is how you can create an Animal from a Dog using Automapper:

Mapper.CreateMap<Animal, Dog>();
var animal = new Animal();
var dog = Mapper.Map<Animal, Dog>(animal);

For the properties of Dog that Animal doesn't have, if the property is a value type it's value will be the type's default value and for reference types the value will be null.

Thats it. Go forth and break all of the traditional coding rules you know!

Generate an enum from SQL server database table

I have a lookup table in SQL Server and a corresponding enum in my code - two different sources that represent the same thing. The problem I was having was that one would become out of sync with the other. To solve this I created a T4 template that will generate an enum based on database values. The database becomes the master source and I just have to run the T4 template to update my code... easy!

Here is the T4 template I created if you would like to use it as well. It generates the enum in C#. The enum name is determined by the name of the .tt file so if you want an enum named LanguageType then rename the .tt file to "". Customize the variables at the top of the script with your connection string, table name, and the columns used for the enum member name and value. [2KB]

Linq.js CRUD Methods

One of my favorite JavaScript libraries is Linq.js (LINQ for JavaScript). Unfortunately the author is not responding to pull requests so I'm going to post my update here.

I've added CRUD methods to the library so that you can Add, Delete, and Update items easily. I've found this very useful pattern where I load a lot of data when the page loads, convert it to an Enumerable using Linq.js, then as the user modifies data update it locally and send updates back to the server asynchronously.

Here are examples of each method I've added:

var data = Enumerable.From([
    { "id": 1, "url": "", "title": "Amazon" },
    { "id": 2, "url": "", "title" : "Google" },
    { "id": 3, "url": "", "title" : "Wrong" }

data.Add({ "id": 4, "url": "", "title" : "MSN" });

data.Delete(function(x) { return == 1; });

data.Update(function(x) { return == 3 }, { "id": 3, "url": "", "title" : "Yahoo!" });

You can download my linq.js file here. You can also play with a fiddle I created using the example above.

Alternatively you can pass a function to Update instead of an object (makes sense if you are updating more than one item at a time):
data.Update(function(x) { return x; }, function(x) {
    x.title = "updated " +;
    return x;

Trick for debugging parallel code

Thanks to Microsoft's TPL (Task Parallel Library) writing parallel applications has become easy. In some cases it's as easy as swapping a foreach loop for a Parallel.ForEach loop. The one thing that it makes harder though is debugging! How are you supposed to keep track of which thread you are in? When stepping through code the debugger jumps around to different lines because multiple threads are running code at the same time. It's frustrating. Here is a simple solution I found by only letting one thread run at a time while debugging. Use ParallelOptions and a debug preprocessor! Example:

var items = new[] {1, 2, 3};
var options = new ParallelOptions();

options.MaxDegreeOfParallelism = 1;

Parallel.ForEach(items, options, item =>
    //do something

The best part is you don't have to change anything when you do a release build. The preprocessor takes care of not compiling line 5 and so the default MaxDegreeOfParallelism value will be used in production.

Max Tesoro Shrike H2L Laser Gaming Mouse Review

I recently bought a Max Tesoro Shrike H2L Laser Gaming Mouse. Here are my thoughts on it so far.


  1. The laser sensor is precise.
  2. I was concerned about the mouse cord before my purchase because I have been using a wireless mouse for the past 10 years and have become accustomed to the freedom. The cord is very thin, lightweight, and long. I haven't noticed it's there unless I look at it.
  3. The scroll wheel is stiff which I like. It makes it easier to use the scroll wheel as a button. I use it as a button when gaming since there isn't a lot of travel between the left click button and the scroll wheel and most games use the scroll wheel anyways (ex: for cycling through items). It's the bump/stop scroller type which I like as well, not free-wheel.
  4. The weight system is a nice option. I loaded mine up with all of the weights (35 grams). The weights come in a plastic case if you don't use all of them and want to store the rest.


  1. The amount of travel when clicking is too much for a gaming mouse. The $5 Logitech mouse I have at work has less travel. This isn't acceptable for gaming. It's only okay for daily computer activities.
  2. The top side looks like it is brushed aluminium in pictures but it is actually plastic that is painted to look like brushed aluminium. After enough use I'm sure the paint will wear off and make the mouse look raggedy.
  3. The DPI button is clear plastic and there is clear plastic leading up to it which contains an "adjustable full color LED control" under it. There are a number of things wrong with this:
    • The clear plastic is tinted so you don't get much light coming through it.
    • It seems like the light is made up of a 3 or 4 LEDs - Blue, Red, Green, and Yellow. They should have just gone with a single (or a few) adjustable-color LED because combining the 4 colors together isn't producing a nice light effect. If you look closely you can see the different LED colors. If you change the color using the software the lights recalibrate by cycling through each color.
    • The clear plastic is on top of the mouse... you know... where you hand is covering it most of the time. It would make more sense if other parts of the mouse were transparent instead.
    • Overall I think they could have removed the LED feature and reduced the cost.
  4. The upper portion of the thumb grip needs to come out a little more so that the mouse is easier to pick up. I have been using the underside of the thumb buttons to help pick up the mouse. This could lead to fatigue and accidental thumb button clicks.
  5. The packaging was not nice. It was suspended it a plastic shell inside of a cardboard box. Seems like it was made to hang on a rack at BestBuy.

Other thoughts

  1. The bump on the backside of the mouse is a little high but it hasn't bothered me much.
  2. I wish the thumb grip came out wide so I could rest my thumb on it instead of actually having to grip. This is nice for casual use. (like the Logitech Performance Mouse MX)
  3. I wish the DPI button did not say "DPI" on it and that adjusting the DPI was a software only feature. You can remap this button using the software but the button still says "DPI" on it.
  4. Mouse must be plugged into your USB host port that provide 500 mA (Milliamps) at 5 V (Volts) - this means you cannot plug it into the USB port on the Nighthawk X8 keyboard which I also bought.


I do not think this is a great gaming mouse. It works well for daily usage and may work well for designers. I'm concerned about how long it will last. I plan on keeping it for now since these things are costly, but I will be looking at alternatives not too far in the future.