LINQ to SQL Tips 8: How to (and why) create a partial class in the designer to augment generated code

The code generated for LINQ to SQL is a set of partial classes - one for your DataContext and one per entity mapped to a table or a view. That means you have the opportunity to augment the generated code with additional code in your partial class. I often get questions that have a simple answer in the following list. The code you write could be for:

  1. Adding unmapped fields or properties to an entity class (e.g. computed property - amount for an Order class based on price, quantity, freight, taxes etc.)
  2. Providing an implementation of a validation method defined as a partial method (e.g. for a mapper property called "Name", OnNameChanging(), OnNameChanged() or for an entity OnValidate() etc)
  3. Providing an implementation of CUD override methods supported by the DataContext (e.g. InsertOrder, UpdateOrder, DeleteOrder). You may choose to call a method wrapping a CUD sproc in the method body and/or do more customization.
  4. Providing an implementation of relationship load override method (e.g. LoadOrders(Customer cust), LoadCustomer(Order ord) etc.). Again, you may choose to call a method wrapping a query sproc in the method body and/or do more customization.
  5. Add a method to call a sproc returning multiple results (see this previous post for more details)
  6. Adding utility methods to entity classes or your DataContext class

That sounds like a good bunch of reasons. But I often get a followup question - is there an easy way to do this in the designer? Of course, you can add a new class to a project anytime but a somewhat stylized way is to right click on the design surface and click "View code" (don't ask me why that name was chosen for creating a new partial class). If I do that for Northwind.dbml (file opened in the designer), I get a Northwind.cs at a peer to Northwind.designer.cs.

The file already contains a little class wrapper. It is not a big deal to add that by hand using code snippets but it is another little bit we could do for you (and did in this case).

Dinesh

June 16 addendum: Steve has rightly pointed out that the behavior is different in website projects. My colleague Young Joo - PM for the designer explained the rational as follows "We disabled View Code in website project since it actually has a different meaning than other project types.  The View Code option from Dataset Designer within Windows Application project, for instance, creates the partial class file.  However, it opens .XSD file in the editor in website project."

So in website project, you will need to resort to the good old way of adding a class through the solution explorer. Thanks Steve!

Published 06 June 08 05:41 by Dinesh.Kulkarni

Comments

# Matthieu MEZIL said on June 13, 2008 10:03 AM:

J'avais blogué sur les 5 premières astuces de Dinesh Kularni . Voici les 3 suivants : LINQ to SQL Tips

# sjnaughton said on June 13, 2008 6:20 PM:

Hi Dinesh I've noticed that in a file system based website the View Code is missing and also "Configure Behavoir" is always greyed out in file system based websites.

P.S. I've noted this on Microsoft Connect, but thought it was worth mentioning here as people like me would spend a great deal of time looking for somthing that isn't there

Steve

:-D

# Dinesh.Kulkarni said on June 16, 2008 4:01 PM:

Steve, you are right about the omission. I have added a note to the post accordingly.

Thanks.

Dinesh

# Tim Hardy said on June 30, 2008 1:03 PM:

Is there a way to hook into the code generation process of LINQ?  I would love to at least be notified (some event perhaps?) of when LINQ is generating new classes, so that I can generate some partial classes of my own to respond to those changes.  As it is now, anything I do above, would have to manually be done everytime I re-generate LINQ classes.  At least that's the case if my partial class is even remotely dependent on the table (which it will be 90% of the time).  This can easily be solved by simply publishing events from within the code-generation segment and perhaps even publishing some data along with those events (table schema structure of the class being generated, etc).

This is a significant piece that is currently lacking with LINQ to SQL that would bridge the gap for us.

# Dinesh.Kulkarni said on July 1, 2008 7:29 PM:

Tim,

That is a very good suggestion. I will pass in on to the V2 team for their consideration.

In general, based on the feedback on this blog and elsewhere, it is clear that users are looking for a lot more than one-shot, black box code gen which is all we could do in V1.

Thanks,

Dinesh

New Comments to this post are disabled

About Dinesh.Kulkarni

I am a program manager in the Visual C# Product Unit of Microsoft. I am currently working on the LINQ project with specific responsibility for DLinq. Previously, I have been in a PM in SQL Server working on ObjectSpaces and DataSet. In pre-MS life, I have worked for companies ranging from startup to IBM on a wide range of software projects. Before I started working, I did M.S.E.E. and Ph.D. (CSE) from the University of Notre Dame and B.Tech. E.E. from IIT Bombay, India.

Search

This Blog

Syndication

Page view tracker