Using partial classes to make LINQ to SQL even more useful

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.

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.

About the Author

Steve Temple, Technical Director and co-founder of Gibe

Steve is the super brain behind the development of our major projects. With over 20 years of commercial experience, Steve is an expert in .NET, Umbraco and Microsoft technologies. Steve is also an Umbraco Certified Master and Microsoft MCSD