November 4th, 2010

I spent hours today debugging an issue where a call to a finicky web service API.  All my hand-built calls in Fiddler worked fine, but my code did not.  I realized I needed to inspect the headers of the outgoing web request to determine the cause and I stumbled upon this post by Mark Needham about how to configure IIS so that web requests route through Fiddler. <!--more-->

 
September 21st, 2010

I think most web developers have mixed feelings about Javascript.  On the one hand it’s very important for a good user experience and there’s some pretty cool things you can do with it.  On the other hand, there are so many browser inconsistencies, especially historically -- not to mention the IE6 problem –- that we approach it with some trepidation.

I believe that being a good developer requires constant education and there’s tremendous importance in learning new languages and going deeper in the ones you know.  A couple weeks ago I decided to learn Javascript in depth.  I think Javascript is a good choice for my next language because it’s just so useful.  And how many server side developers know Javascript?  I mean *really *know it.  It’s probably the only language that the majority of developers who program it never spent real time learning it.  Which is not a good thing.  Javascript is syntactically similar to other C languages but uses an entirely different type system and inheritance model which require different design patterns and expertise.  And with real knowledge, it doesn’t have to be scary.

I’ve been watching the Douglas Crockford videos (all of them) from YUI Theater (http://developer.yahoo.com/yui/theater/) during my commute.  I also bought his book Javascript: The Good Parts from Amazon.  I don’t think you can watch his videos and not come away with a real appreciation for the language!  And his presentation style is great.  He’s got lots of history which I enjoy immensely and does a great job imparting information.  One thing I’m not a huge fan of is the way he presents certain opinions as fact.  This is an issue in the development community and he takes issue with the practice himself in at least one presentation so I wish he were a little more liberal with the “I think […] is the best […]”-s instead of the “obviously […] is better”-s.  But that’s a minor nitpick.  His videos are really top notch.  Go watch them.  Now.  I’m waiting…

As it turns out, Javascript is actually very powerful and, using the right conventions, can be pretty and concise too.  Especially after falling in love with the functional aspects of C#, I’m really appreciating Javascript lambdas.  I thought this was a new concept at least in the C-based languages when Microsoft introduced it into C#, but Javascript’s been doing it for years!

If you’re not yet a Javascript guru and you work in web development or just trying to figure out what language to learn next, I highly recommend diving into Javascript.  It really deserves some attention.

 
August 19th, 2010

Seems obvious in retrospect, but gave me some trouble yesterday.  I’m using hMailServer (a nifty, open source SMTP server) which supports scripting via VBScript event handlers.  Now I don’t want to code in VBScript so I wrote a .NET library and was using some COM interop to pass everything through.  However, I wanted my library to read configuration values via the ConfigurationManager class and didn’t know what config file it was looking for!

In a .NET app you typically have an App.Config that gets copied to a new name on build – YourApp.exe.config.  I was building a library though, not an executable, and my library was being called through COM, so what should the config file be named?

Well after some research (running procmon – man that’s a useful utility!) I found that the config file that ConfigurationManager was looking for used the same naming conventions as if it was called from a .NET app: “calling-executable.exe.config” – in my case “hMailServer.exe.config” in the \hMailServer\bin directory.

Good to know!

 
July 15th, 2010

If you create a new class library in VS2010 using .NET 4, the default target framework is a new package called .NET 4 Client Profile which is a subset of the full .NET 4 (more info at http://msdn.microsoft.com/en-us/library/cc656912.aspx).  The Client Profile is useful for for streamlining client application installations, but it causes some odd behavior in Visual Studio 2010 with regards to referenced assemblies. 

Basically, if you reference another assembly that makes use of references outside the .NET 4 Client Profile, you’ll get full intellisense before you build.  However, your build will fail.  And afterwards, intellisense will disappear for those unsupported assemblies and the IDE will mark them with squiggly lines indicating that they’re unknown classes.  At first it seems as though the dll was deleted or something, but it wasn’t.  It’s just that the default target framework for Class Libraries in Visual Studio 2010 is the .NET 4 Client Profile which doesn’t support something you’re doing.  I assume that the reason for the weird behavior has something to do with the build-as-you-type functionality of VS2010 not paying full attention to this.

Anyway, the solution to this is just to go to your project settings (right click on project and select properties) and under the Application tab (top tab) like so:

Capture

 
July 14th, 2010

I’ve been conducting interviews over the last couple weeks and have noticed a general lack of understanding regarding Lambda expressions (even in those candidates with senior role titles).  So here’s some information about Lamdas that if you don’t already know, you should!

First of all, Lambda expressions are just inline methods.  Unlike delegates, lambdas have access to variables that are scoped the same as the lambda.  So if you have a method with a variable v _and a lambda _l, the statements inside l will have access to _v _as in the following example:

   1: void SomeMethod(){
<!--CRLF-->

 
June 8th, 2010

Thanks to Keith (comment #1 at http://weblogs.asp.net/johnkatsiotis/archive/2010/04/28/huge-ef4-inheritance-bug.aspx#7464062) for that title, btw.

After coming from NHibernate, EF4 is an enormous pain to work with.  Unlike NHibernate, which basically lets you map any relationship you can dream of, EF4 has a lot of issues with inheritance.  Here’s a pretty big one IMO:

Take a look at this object hierarchy:

ObjectHierarchy

Before I continue, if you think we’re working on an edge case I have to disagree.  One of the tenets of OOP is the idea of encapsulation – there’s no reason a base class shouldn’t be able to encapsulate the logic of a subclass.  For instance, in this case, the LeaveWork method on Employee would be abstract, but would be implemented on SomeoneWhoActuallyWorks *to make sure they don’t leave too early and on *SomeoneWhoDoesNotWork to allow them to leave at any time.  I know I’m stretching it here with the example, but I wanted something simple that I could test & show you.  I actually am working on a project with this type of hierarchy and EF4 is making my life painful… Take a look: ##### Mapping with Entity Framework (NOT)

 

Here’s an Employee table. This should really be all we need.  Employees just have a FullName and a type.  We just want to map it to the right classes based on the EmployeeType, but that won’t actually get mapped as a property on the class.

Employee Table

So you might be naive and try mapping this relationship like so (in the edmx file):

Inheritance_NoMiddleMan

Of course you’d make sure that Sucker had a Condition “When EmployeeType = ‘Sucker’” and Manager when EmployeeType = ‘Manager’.

But that causes a runtime exception on insert:

Mapping and metadata information could not be found for EntityType 'EFTest.Manager'.

You see, the fact that Manager and Sucker both inherit from SomeoneWhoActuallyWorks is actually very important to Entity Framework 4.  I’m not sure why.  I mean if I didn’t specify a middle-man class, what’s the difference?  If it has properties, I’m obviously not trying to map them, so who cares?  Well EF4 cares…  But wait, it gets better (or worse, depending on how you want to look at it)

So, your next try might be to map middleman -- SomeoneWhoActuallyWorks

Inheritance_OneMiddleMan

You can map SomoneWhoActuallyWorks to the Employee table and set its Abstract property to True, this will compile without a problem.  And it will run too.  And inserting a Manager or a Sucker will work.

But this method explodes when you add another branch off of Employee:

FullMappingHierarchy

This mapping (SomeoneWhoDoesNotWork is abstract, btw, and Owner is mapped to the Employee table with a Condition “Where EmployeeType = ‘Owner’”) won’t even compile:

Error    1    Error 3032: Problem in mapping fragments starting at lines 47, 66:EntityTypes EFTest.Manager, EFTest.Sucker, EFTest.Owner are being mapped to the same rows in table employees. Mapping conditions can be used to distinguish the rows that these types are mapped to.

Yea…

Now you can “fix” this by taking out those middle men.  But deriving Owner, Manager and Sucker from Employee directly might have you writing a lot more code, and potentially duplicate code at that.  And it’s a hack.

 
June 1st, 2010

When you retrieve an entity from the database, Entity Framework handles lazy loading for you by creating a proxy for your model class.  There is, however, a little caveat.  Take the following code:

   1: using (MSEntities ent = new MSEntities("name=MSEntities"))
<!--CRLF-->

 
May 28th, 2010

I’m working on a new project using .NET4, MVC2 and EntityFramework.  For the last couple years I’ve been using NHibernate as my ORM of choice but there are a lot of similarities between NHibernate and EntityFramework actually so the learning curve is quite small.  There are a number of things that make me long for NHibernate, but one stands out in my mind: lazy loading.  One of my first issues with EF was trying to track down a bug: ClassA has a property reference to ClassB, but the property is always null even when ClassB exists in the database.  After debugging for what seems like hours, I tracked down the issue – the ClassB property on ClassA was not virtual.  This was never a problem with NHibernate because NHibernate threw an exception if the property was not virtual.

 
April 8th, 2010

 

UPDATE: I’ve cleaned up the code a bit and put it up on CodePlex at http://ordertolist.codeplex.com/

 

A team member recently wanted LINQ-to-SQL to perform a query with a Contains() clause and return the results in the same order.  For example, he was passed a list of People:

 
March 29th, 2010

Over the last couple weeks I’ve spoken to a few people about something I’m calling QA Based Testing, and I figured it would be a good time to write about it.

There’s been a lot of attention around Test Driven Development in the last couple years.  TDD does take some practice to get used to and it requires team participation.  If your boss tells you to “do it right now” while he looks over your shoulder waiting to alert the business that it’s ready, you might have a hard time practicing TDD ;).

But there’s another way to get your code coverage up, and it doesn’t require practice or team participation.  In fact, it’s very helpful for working with legacy systems.  The idea behind QA Based Testing is that the moment QA fails a certain part of the app, the first thing to do is write a test.  The test should fail because it should produce the issue that QA had a problem with.  Then fix your code.  Red-Green-Resubmit to QA.

At my current company I’ve used this with great success.  First of all, I don’t always do TDD.  And second of all, we’re building on top of a legacy system that constantly surprises us with new issues.  Even modules I built with TDD have failed to account for certain situations because I just could not think of all the possible (invalid) data permutations we would have to account for.  As issues arise, the code coverage increases.  And it requires no getting used to and no team participation. 

There’s really no excuse for not testing your code!