Skip Navigation

Using partial classes to make LINQ to SQL even more useful

10 March 2010 11:14 by steve.temple

Recently I got into a discussion with an interviewee for a developer role here at Gibe, I thought it was interesting and misunderstood enough to warrant a blog post on the subject. The gist of it was a problem he was having because he wasn't able to add methods to the LINQ to SQL classes that get created by the drag and drop editor. Well he had added methods but had done it by modifying the generated source code which were overwritten if he wanted to update a table to pick up some new fields.

One of the big advantages of using LINQ to SQL is the speed of development it allows. I used to create class libraries with data objects which tended to be pretty dull code, just grouping data from tables into logical objects. LINQ to SQL and the visual studio designer does away with this by automating the creation of these data classes which is awesome and saves a bundle of time, however some of my classes would have a few methods for doing some manipulation of the data. How do you do the same with Linq to SQL without hacking the generated code.

The solution to this is using partial classes, for those who don't know, partial classes allow you to spread the code for a class over multiple files. It's how webforms manage to hide all the code that you used to have in .NET 1 projects when you added controls to your aspx pages. In .NET 2 they've tucked all that code away in a partial class so you can't fiddle with it and break things. A much more elegant solution than the previous version which had comments in the code behind file telling you not to touch some of the file. The dynamically created LINQ to SQL classes are all partial classes, which lets you perform the same trick.

As a sample imagine I've got a Product table in my database and I've just dragged this table into my dbml designer. This handily creates me a Product class in the .designer.cs file which I should leave along. But now I want to add a method to get the total stock of this product. The stock levels are spread over two fields, one in stock now and one due in. I can create a new class in the same project called Product like so:

public partial class Product
{
    public int GetTotalStock()
    {
        return this.Stock + this.DueInStock
    }
}

Note that this is a partial class with the same name as the one the visual studio designer has created for me. Because of this .NET will add the methods, properties and attributes added in this class together with all the other partial classes with the same name (and namespace) in this project. So I'll now be able to use the GetTotalStock method along with the other designer generated properties that are available in the Product class ... Simples.

Gibe and the "Joel Test"

15 July 2009 18:12 by steve.temple

One of the things I really want to make sure we do at Gibe is make sure we're writing great code. I don't want any buggy as hell, written by developers a couple of weeks before go live who are doing 14 hour days, barely tested, held together by blind luck and duct tape, we'll fix in the mythical phase 2 code. A quick indicator of the likely quality of code a team is putting together is where that team's company scores on the Joel Test (if you've never read anything on that site have a browse, he writes some great stuff). Over the next few months I plan on keeping a log of where we are on the test with an aim of getting 10 points covered pretty quickly, and then working on the next couple as we grow. Our main objective is to come up with a great place to work, and an environment that is condusive to getting things done. Something that is hard to actually achieve based on previous places I've worked. Basically trying to be the opposite of Initech in Office Space.

This may seem silly as our development team consists of me and a loose knit group of freelancer/contractors. That might be true but we plan on adding at least one developer to the team fairly soon. I think as the team grows it'll be easier if most of these things are in place so that's just the way it is rather than letting them get used to one way of working and gradually switching it over the next couple of months.

So where are we on the test currently? Lets have a run through

1. Do you use source control?

Definately one of the most important items on the test, if you are writing software you must have source control. We use Subversion, and I've used CVS it's not as good but works reliably. I've also heard good things about Mercurial.

So that's 1 point.

2. Can you make a build in one step?

I'm going to put Finalbuilder and the Finalbuilder server in place to do this. Finalbuilder is great, it comes with tons of built in taskts support for doings stuff like checking out of source control, copying, uploading, testing, pretty much anything you can think of. And the server lets you trigger these builds through a website. The plan is to be able to deploy any site we've developed in pretty much one click and it'll make sure the right web.config goes up etc. Basically it'll eliminate most of the chances of someone forgetting a step when uploading and breaking a live site.

Not yet, but is coming, 0 points.

3. Do you make daily builds?

Nope, 0 points.

4. Do you have a bug database?

Yes, we're using Jira. I've tried Fogbugz and Bugzilla and always come back to Jira, it's got a nice interface and does everything we need. Our clients get access to Jira so they can report issues straight in and then see what's happening with their issues.

1 point.

5. Do you fix bugs before writing new code?

Yes we definately make sure we're getting the bugs out of way before we start adding new features, seems crazy to build on top of known buggy code.

1 point.

6. Do you have an up-to-date schedule?

Yes, definately, 1 point

7. Do you have a spec?

Yes, We write specs for pretty much everything we do, even if it's just being used internally.

1 Point

8. Do programmers have quiet working conditions?

I'd say yes, we keep the office mostly quiet with just a little background music. 

1 Point

9. Do you use the best tools money can buy?

This one is tricky, we definately get the best kit we can afford, but it's not quite the best money can buy. I think quad core, 8Gb of RAM and dual widescreen monitors is pretty close to the best you can buy. 

The plan is to get the best we can afford, we're a startup and not made of money. That said, on Joel's point about not torturing your employees, I've worked for fairly large agency where there was a fortnightly email that went round complaining about the quantity of milk being consumed, and that someone is using the office milk for their cereal. This was never going to aid morale or give anyone that feeling of being valued, when you get moaned at about an additional £5 of milk each week.This was particularly demoralising when the suspected cause of the additional milk usage was that half the employees had been donating an additional 3 hours plus each night for the last couple of weeks to get a new site out of the door!

1 Point

10. Do you have testers?

Not full time, we swap code internally and test as thoroughly as we can, but we don't have dedicated testers yet.

0 Points.

11. Do new candidates write code during their interview?

Yes candidates will have to write code in their interviews, I've also been looking at doing an online test to send to potential candidates to screen them initially. Think I'd have to make the time quite tight for the test as they'll have access to Google.

1 Point 

12. Do you do hallway usability testing?

At the minute I'd say no, although we do work with Usability experts for some of our sites. It should be as simple as mailing round a link and getting everyones feedback so this is something we'll definately implement pretty quickly.

0 Points.

So that's 8 points so far, not bad, and I think we should be able to add a couple of points on pretty quickly.

Got my MCPD and MCTS certifications

14 July 2009 08:22 by steve.temple

I passed the 70-564 Microsoft exam on Saturday and the 70-536 exam on the previous Thursday. That gives me a MCTS: NET Framework 3.5, ASP.NET Applications and a MCPD: Designing and Developing ASP.NET Applications using Microsoft .NET Framework 3.5. So now I get to officially use another logo:

The last exam was actually the easiest despite it being for the more advanced qualification. The MCPD focuses more on what is the best fit for a particular scenario, which is a pretty close fit to what I do every day. The MCTS, on the other hand, focuses on having fairly intimate knowledge of the .NET framework.

There are two reasons I've been doing these, firstly I'd like to get Gibe to the Microsoft Certified Partner level in the Microsoft Partner Network. Secondly I thought it would be interesting and I might learn something which turned out to be the case as I've had to learn some of the newer parts of the framework which I hadn't encountered before.

Passed my first Microsoft exam (70-562)

26 June 2009 12:42 by steve.temple

As part of getting Gibe accredited as a Microsoft Certified Partner I've set myself the task of becoming a Microsoft Certified Solutions Developer, and after a few weeks of sporadic reading and a bit of playing around in Visual Studio I passed my first Microsoft exam yesterday with a reasonable score, I'm now one exam away from a MCTS (Microsoft Certified Technical Specialist) in MS .NET Framework 3.5, ASP.NET Application Development.

Microsoft Certified Technical Specialist

It's been something I've been meaning to do for quite some time and just never got round to. How hard could it be having been writing ASP.NET applications since version .NET 1? Turned out to be slightly trickier than I had first thought. I'd bought the official Microsoft Press Training Kit for the exam and run through that. I hadn't used much of the official ASP.NET AJAX, Linq, WCF services or Microsoft Mobile technologies so had to study those in quite a bit of detail and I'd found that I'd got stuck in a bit of a working like it's .NET 1.1 rut, I tend to follow the rule: when in doubt, go with what you know, and I hadn't picked up some of the newer controls and methods of doing things. The Training Kit had a practise exam, which was useful for checking what areas I needed to work on, although both the book and the exam had a few errors which had me second guessing my own judgement, this question had the most obvious error.

The actual exam wasn't too bad after a few tricky early questions that had me worried. They said to allow 3 1/2 hours for the exam but I was in and out in about 1 1/2. The exam software was a bit frustrating, there were some questions where you have to essentially figure out which code block is the correct one by comparing several. They put all of this in a fairly small scrolling region which makes it harder work than it needs to be. You get an instant pass fail and a summary print out on the way out showing how you did in each of the seven different areas covered by the exam. 

Now it's just two more exams to get my MCPD!

Web Optimisation

1 May 2009 17:11 by steve.temple

One of the things I've been looking into recently is how to tweak sites to maximise the conversion rate of visitors. As an example let's look at the birthday brain, this is a site I've developed as a bit of a side project. 

The key area of the homepage is the login/register box, the key objective of this page is to get readers to understand the service and register. The secondary objective is to make it clear what the service is about to search engines.

The original design of the login register block looked liked this:

Login/Register Box version 1

While there is nothing wrong with this design it does hide the register box away under the login box. User's who have already registered will be taken straight to their reminders if they check the remember box and can easily spot the login boxes if not. So making the register button a bit more obvious by swapping the two around seems like an obvious change. This is what we end up with:

Login/Register box version 2

A small change but how do we know how big a change it would make if any? I setup the page so a querystring would change which version you were looking at so ?ver=2 would display the new version then setup this in Google's Web Optimizer. This service directs users to one or the other version from google, it then tracks the conversions on the site and shows a graph for each showing the conversion rate. The old version already had a very good conversion rate of 9.28%. The new version however managed to convert 10.7% a very decent 1.42% improvement for an hours work!