views:

325

answers:

5

I use Linq to Sql (although this is equally applicable in Entity Framework) for my models, and I'm finding myself creating buddy classes for my models all the time. I find this time consuming and repetitive. Is there an easy way to automatically generate these buddy classes based on the models? Perhaps a visual studio macro?

An example of a buddy class that I'd like to create:

[MetadataType(typeof(PersonMetadata))]
public partial class Person
{
}

public class PersonMetadata
{
    public object Id { get; set; }

    public object FirstName { get; set; }
}

Thanks in advance.

+1  A: 

I would take a look at Text Templates (tt) in Visual Studio 2010 (I believe you can use them in 2008, but there's not as much support for them as there is in 2010).

It will allow you to write a generic template using code which you can use to generically process your LINQ to SQL types and generate the buddy classes for.

casperOne
A: 

You might want to take a look at the T4 templates. Here's a quick intro from Oleg Sych. These work just like CodeSmith or MyGeneration code generation templates.

To get a jump start, you might also spend some time dissecting the alternate code generation templates for Entity Framework, like Self-Tracking Entities. These are T4 templates that look at the metadata files from the model .EDMX to generate alternate entities. These might get you going on how to look at your L2S model and create the buddy classes for you.

Hope this helps. Good luck!

Josh
+2  A: 

Don't generate buddy classes. Buddy classes are a bit of a kludge. Instead, write an associated metadata provider. This produces the correct hints to dynamic data at runtime. It's a little more work initially, but will pay off in the long run.

Craig Stuntz
That's a great suggestion. But if you don't use data annotations, where do you store your metadata?
JohnnyO
Mostly, I don't. I tend to favor convention over configuration, especially for anything which *could* be generated in a T4 template (why generate attributes when you can just provide the metadata directly?). If you *must* configure a type manually, write a provider for that and store wherever is appropriate. But that's a last resort. Prefer a view-specific view model over annotating entity types directly.
Craig Stuntz
A: 

I've heard a lot of good things about the T4 templates.

The following is my opinion, and is not intended to start a flame war.

I've not had great success with ORMs. The SQL they generate tends to be less-than-optimal, and you wind up jumping through hoops to get around limitations in the framework. Also, placing query code in your app instead of in the database where it, again in my opinion, belongs, violates a clear n-tier architecture, and puts more db maintenance responsibility on the developer, eliminating part of the benefit of having a dedicated DBA. Also, it makes mid-flight debugging of queries next to impossible.

I wound up writing a utility to generate db proxy classes and related stored procedures from a table definition, and have turned that into a Visual Studio plugin. It's about 1200 lines of code, not including the String Template library. I've been using this setup for about 6 months and have no regrets.

David Lively
A: 

Perhaps the new LLBLGen tool is useful to you. It is able of generating EF1, EF4 and L2S code and allows generating attributes for you. It's a commercial tool though.

Steven