tags:

views:

376

answers:

8

Hi,

So far the most I've done with code generation tools is creating my basic CRUD layers (basic sprocs, data layer and business layer that calls the data layer), and class creation based on my database tables.

What other ways do people use code gen tools?
I've never used ORM mappers or Hibernate and quite frankly don't understand what they do.

Do tools like ORM mappers and hibernate create 'slow' code? (i.e. adding too many layers and dynamic style coding that just ends up being bloatware)

A: 

We've used My Generation in the past. Its not so much a data-access code generation tool, more a tool for creating data-access code generators (or Templates as it calls them). Still it comes with some standard templates that work pretty well.

On the point about ORM mappers being slow. By far the biggest impact on performance is going to come from the number of round trips to the database that you do. In the context of a database call, a bit of reflection or string comparison is peanuts.

d4nt
A: 

We rolled our own code generator with XML & XSLT. We had an XML file that defined all the classes and attributes for each class, then used XSLT to actually generate the .cpp/.h files to go along with them. In our project each class just had basic data storage functionality, the actual logic or rules was done with custom (hand-coded) classes.

This project was started back in 2001, and I'm sure there are better tools out there now. I just thought I'd throw this out there that you could roll your own if you needed that level of flexibility.

Rob Thomas
A: 

We made our own code generators.
We have stored procedures which create more stored rocedures. We have a tool creating our object tree out of the meta-tables we store in our database.
Why? Easy: tons of stored procedures, tons of tables, tons of objects and so few developers.
That's what generators are good for: reducing time, reducing effort.

I've never used ORM mappers and quite frankly don't understand what they do

Object Relational Mapping: storing/persisting your objects (or their relevant data) into a (relational) database.

John Smithers
A: 

We've used code generators to allow reflection in C++, generate serialization code automatically, allow programmers and game designers to write in a higher-level language, generate scripting language glue, and automate some security and data format checks. That's for a game, though, so if you're using a higher level language in the first place, you might need to generate code a bit less desperately than we do.

Joe Ludwig
A: 

We started out with CodeSmith, but as our product evolved, we ended up using less and less of it, so replaced everything CodeSmith was doing, with one routine for a parsing a template file.

I'd still recommend it though - its very flexible and allows you to write general purpose templates, as well as coming with some standard ORM type things out-of-the-box (CSLA and netTiers I think).

We use code generation for our ORM framework, for creating resource only assemblies (an Images library for example), and for string resources files, so that non-programmers can update a data file of translations, and not worry about the code.

Ch00k
+1  A: 

The number one thing to remember with any sort of code generation is that you shouldn't be modifying the generated code. In other words, treat the input to the generator as the source artifact that you put in your source code management system, not the output of the generator.

In fact, you should have your build process run the generator to create the code if possible, so you don't rely at all on on any hand-modifications of generated code.

That way as the code generator itself advances, your own projects aren't left behind; rebuilding your projects (possibly with additional options, e.g. "use new SomeGenerator 2.0 features") will pick up the bug fixes, performance improvements, and other enhancements from a new version of your code generator.

Chris Hanson
A: 

I've rolled my one using xslt on numerous occasions. I've also created them using C and Java for some tasks - the most elaborate parsed a Java file, and added ASN.1 serialisation for fields based on custom @tags in the comments (this was before Java 5 annotations), so the output and the input were the same file. You'd probably do that with an IDE plug-in now, but having the thing ant scriptable is advantageous. Recently I did some DSL generation using a plug-in for Enterprise Architect written in Python (EA has some customizable code generation, but not very flexible). But for the most I do XMI->friendly XML->general purpose language code via XSLT.

Pete Kirkham
A: 

ORM mappers and code generators do not create slow code. Sometimes they generate code for slow architectures, but that is all dependant on the developer writing the code generation templates. I think the best performing ORM for its functionality I know if is EntitySpaces. There are many other great ones out there, but often they use reflection based models which are slow. Entityspaces uses the core components of ADO.net like the DataTable to manage database updates and record state instead of reflection. It is not as fast as cursor access through a reader, but it is much faster than subsonic or nhibernate. Maybe your question is more targeted to the theory behind it. Basicallty, all of the information you need to write a data access layer is in the meta data of the database. All of that information is available to you if you look hard enough. The ORM fetches that meta data and applies it to a template to generate data access classes. Anyone that doesn't use code generation for data access these days is really going to have trouble competing.

komma8.komma1