c#

c#

Dynamic configuration settings in .NET 4

12 January 2012 |

We have a default implementation over our API that we can customize for clients.  In order to support different feature-sets we have a custom configuration section inside our web.config that looks similar to this: <sites> <add name="statichippo"> <hosts> <add name="blog.statichippo.com" /> </hosts> <profile enabled="true" /> </add> ... </sites> and this works well for the most part.  But we have some clients that have certain...

If the property’s computed, tell me!

08 March 2011 |

Today’s lesson learned: if a property may or may not be computed, provide a mechanism that allows consumers to check if it is computed.  Seems pretty simple when you hear it but we don’t always design like this.  And I think we probably should most of the time.  I came to this conclusion based on the following problem: My project has a lot of similarities to a shopping cart.  Imagine a product catalog with both a Product and a SubProduct (only one level deep).  Both a Product and SubProduct have a price but price is only required on a...

Mapping a computed property in EF4

26 January 2011 |

I posed a question a while ago on stackoverflow (http://stackoverflow.com/questions/4618861/ef4-map-model-defined-function-to-property) about how to map a computed property in Entity Framework 4.  I didn’t get any answers so I figured I’d post what I came up with. The Problem Just in case you’re too lazy to click the SO link and read the actual question, here’s the rundown.  Suppose you have a Product and Order class defined like so (a bit over-simplified for real ecommerce but you get the idea): 1: public class Product ...

Some EF4 quirks and annoyances

27 December 2010 |

I’ve heard a lot of people bash Entity Framework for being incomplete.  I happen to think that EF4 is pretty good for a first version.  It’s just too bad it’s a second.  Especially considering there’s plenty of mature ORMs from which Microsoft could take some more hints. I’m using EF4 code-first which should be a lot like other ORMs out there like, for example, NHibernate (which I used in many previous projects).  NHibernate supports hydration of data into fields.  EF4 can only hydrate properties.  This is more annoying than it may sound.  For example, take the following code snippet:...

How to properly set ContentType on HttpWebRequest

03 November 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. The issue turned out to be that the Content-Type header was not being set on the POST to the web service.  Weird, since this line was present: request.ContentType...

Reading config file when called through COM interop

18 August 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,...

Lambda variable scope & execution confusion

14 July 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: ...

Huge EFing Bug!

08 June 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: 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...

OrderToList: Fix for LINQ-to-SQL Order dilemma

07 April 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: 1: List<int> idsOfPeopleToDisplay = new List<int> { 578, 291, 788, 230, 45 }; and he wanted to select all People from the database with those IDs, in that order.  The following will result in a non-correctly ordered...

Object tracking != Lazy Loading!

26 March 2010 |

LINQ-to-SQL DataContexts have a boolean property called ObjectTracking which is used to determine whether the framework should exert the overhead necessary to remember previous states.  This defaults to true and is used to determine the SQL UPDATE command to run when persisting the object. We have a project with data that’s read-only and so we decided to turn ObjectTracking off (set to false).  We figured this was a good idea since, the thinking went, it would save overhead and since we’re not persisting the data anyway (read-only) we’ll have nothing to worry about.  Boy were we wrong. All...