I’m currently working on a project for a client that has a database schema comprised of 150-200 tables. I’ve hit the limit of using the LINQ or ADO.NET Entity Framework designer feasibly so I’m looking at code generation. I used Codesmith on a project with their NetTiers framework at a previous company and it was quite elegant. They ended up using NHibernate after I left to move to Austin and they got a new technical lead, but I think it would have done the job very well.
In most of the web application’s I’ve designed over the years, I have used ORMs to generate classes that sit in front of database tables and let me set and get properties backed by fields, traverse relationships defined with foreign keys, and store and update rows. I have chosen not to go the domain driven development route however where each object exposes additional non-persistence-related methods that provide behavior for a couple of reasons.
First, most web applications seem to naturally evolve some portion of the API that serves the user interface layer (business logic layer) into a web service at some point. If I create manager/gateway classes that provide CRUD and common queries in front of my domain this makes it easy to expose as a web service since I am basically re-exposing a subset of the same API. Of course for some operations this doesn’t work, where for example you want the web service to accept an “Order” object that is very rich and contains nested complex types within it. Secondly, many of the ORM tools today inherently are tightly coupled to the database and don’t give very flexible ways to add behavior to them.
I’m looking into PLINQO, a set of templates for Codesmith that do smart non-destructive code generation and allow you to change the properties of your classes from the fields backed by them, add CLR attributes to these properties, add business rules that are validated with your class, and not have these annotations overwritten when updating the code from the database when the schema changes. PLINQO has the bonus of generating manager classes that include CRUD operations and get methods for relationships, indexes, and the primary key of each table.
I’m also looking at the LINQ-to-SQL T4 templates written by Oleg Sych. T4 was added to Visual Studio as a somewhat unknown feature and you can read alot more about it on Oleg’s blog than I could tell you here. The T4 templates are great when you want to use the LINQ-to-SQL designer to model your domain, and actually spit out both the database schema and the resulting code objects.
We have many interesting requirements. The client will take over the code base at some point so we need them to accept the tool we use. We need the tool to be affordable and provide future value on projects other than this one. We also need the tool to enable us to be agile enough when the schema and business rules change. I think one of these approaches will work out great in the end.