Aim of the article
Recently, I evaluated some of the open source code generators and ORM Frameworks in .NET. To my surprise, there were quite a few of them in various stages of development. This article tries to provide an overview and links to the resources that I found useful during this evaluation process.
Though we probably don’t realize, we have been using code generation since the day we compiled our first C (or assembly program!) program. The C compiler converted our C program into code that was understood by the machine.
Code generation can be defined as:
"Conversion of an abstract text/model into more concrete artifact that can be finally understood by the computer (probably after several more iterations of conversions)"
The main purpose here is to eliminate writing repetitive and static code that can be auto generated. With every new release of Microsoft's developer tools, some parts of mundane coding tasks are automated. The days when I used to type in 100 lines of code to create a simple "Hello World" Windows application, now seems so naive.
With the need for writing code to generate UI screens almost eliminated (thanks to Visual Studio and other such IDEs), and programming becoming more abstract, the next target is automating application development for enterprise applications. Model Driven Architecture (MDA) is a step towards this direction that focuses on domain and process models as a means of application generation. There are quite a few tools (mostly commercial) available, claiming to help you realize application generation. But due to the prohibitive cost, proprietary architecture, and complexity, their acceptability and proliferation is still low. Most of the current tools claiming to be MDA tools are advanced CASE tools, or code generation tools, but we are surely getting there.
For more details please check the following links:
With so much momentum behind MDA, is Microsoft going to be left behind? Rumor has it that it is working on something similar that is different from the current MDA approach which focuses on UML; the project is named Project Green. Though not much is being said about this "secret" project, it is sure to give custom application vendors some scare.
Check out some interesting products from IRISE and also Softrock systems that support application simulation using UI models.
Till these tools get cheaper and are easily available (so that I can get a copy and evaluate them in my spare time), let’s focus our attention on ORM and code generators which can automate the creation of data access layer and web pages at least for CRUD operations on database tables.
Why automate code generation for CRUD?
- CRUD operations are very common and are required by almost all enterprise applications.
- Code is repetitive and error prone.
- Code automation can reduce the effort required to test.
- Can lead to the enforcement of best practices and coding standards.
- Can integrate with Open Source Data Access Framework, which can lead to increased productivity and "free" enhancements (also remember the downside of choosing a bad Open Source product).
- And did I mention it can save time so that you can go home early!
In a database application, the Data Access Layer acts as a bridge between the data tables (Relational Universe) and business objects (Business Domains); and in the .NET world, more often than not, these are built as extensions to the ADO/.NET
DataSets/Data tables. There is nothing wrong in this approach. In fact, it will work very well for small database applications. But for a large enterprise application where there are associative relationships and inheritance hierarchies, this approach doesn’t work well. In this scenario, we end up writing complex class hierarchies and utility classes to do mapping, transformation, and messaging of data. These issues are best addressed by totally separating the Relational Model (the database), persistence/mapping layer and the Domain Model (Business layer). Read an interesting comment in the following article written by a well known ORM evangelist, "Fallacy of Data Access Layer".
Object Relational Mapper, as the name suggests, provides framework which maps relational data to objects (Business Objects). If you are interested in more detailed explanation on the theory behind ORM, please check the following links:
Many of the OR Frameworks generate (or require you to create manually) classes (objects) based on domain models, while their framework classes take care of mapping and formatting of data, so that it persists safely and accurately.
There is considerable amount of interest in the developer community with regards to ORM, and many perceive it as the next big thing in software industry. So, when there is so much going around, can Microsoft be far away? Absolutely not; they are working on something similar. Long back, they had announced they will have some thing on ORM, which was then termed as Object Spaces. It was supposed to be released with .NET Framework 2.0, but it was conspicuous by its absence in the beta release.
Here is the link to MSDN which currently doesn’t give much information. But there are a few links that provide you some insight into Object Spaces.
Can Microsoft folks think a lot different from the mere mortals? Well check this out: Cω (C Omega). Comega, or Cω, is described by Microsoft as a strongly typed, data-oriented programming language to bridge semi-structured hierarchical data (XML), relational data (SQL) and the .NET CTS (Common Type System). Additionally, C omega extends C# with asynchronous concurrency abstractions.
What are the ORM tools available for .NET in the market currently?
There are lots of ORM solutions available for Java (guys, it's just because it has been there for few years from now :) ); but surprisingly there are quite a few solutions available for Microsoft .NET. Here is a small list (thanks to Yves Reynhout and Sharp Tool Box):
Some other interesting frameworks:
To see the updated list, please visit Sharp Tool Box's List of OR Mappers (Sharp tool box website contains a wealth of information about various tools available for .NET, make sure to visit them frequently at SharpToolbox). Also, Microsoft is working on data mapping application block that will (was supposed to) be shipped as a part of Enterprise application block (any connection with ObjectSpaces :-?).
For more details please visit: Data Mapping Application Block Work Space.
Criteria for evaluating ORM tools
With so many of these tools/framework available, which one to choose?
Well! If the answer was simple enough, we probably would not have so many products in the market. The best advice probably will be to evaluate products against the requirements you have.
Some of the common criteria that you can use:
- Support for class hierarchies and relations.
- Support for grouping and aggregate functions.
- Support for transactions.
- Database independence (If it’s not a major concern for you, you might want to ignore it. In some cases you might be able to eek out some performance gains).
- Performance and footprint.
- Data caching, lazy load.
- Learning curve and simplicity with which it can be integrated with the code.
- Some other points that are to be considered are maturity, and available documentation, or community support.
Fabrice Marguerie has provided a detailed list of criteria that you can use to evaluate an ORM tool: Evaluation Criteria for ORM.
If you want to read some interesting debates about which ORM is the best, please check out the following discussion (have a cup of coffee ready it’s a long night!)
I will try to give you an overview of a few of these technologies in another article. I plan to start with Nhibernate. Nhibernate is very flexible and is a part of a highly popular and established Hibernate Framework for Java. It provides you with a framework for eliminating/reducing (I prefer simplifying) the amount of code that needs to be written for DAL.
Wouldn’t it be nice, if there were tools that (given a database and a table in that database) did the whole bit of magic and generated a C# solution, which will have a GUI for all the basic CRUD operations along with the DAL? I bet that would be nice!! Well, we are not alone; there are so many people who have thought of this before us. To get the list of these tools you just have to click the link! C# Code Generators.
I have used IronSpeed (it is much more than a code generator), and is good at what it does. It also makes, creating "Professional looking" database application very simple. But it’s not free and also does a lot of things "magically", without allowing me to control them (newer version allows more control though)!
Some of the free source code generators are:
MyGeneration: A highly recommended template based code generation engine, written by Mike Griffin and Justin Greenwood of MyGeneration Software. They have also committed to keep version 2.0, which is planned for release by the end of 2005 as a free product. With ASP-like syntax, flexible UI, and good community and developer support it's a product that's ready for prime time.
CodUS: Codus is a comprehensive code generation tool for object-relational mapping. It takes an existing database and automatically generates all the code for updating, deleting, inserting and selecting records. In addition, it creates web services for distributed programming, strongly-typed collections, and a full set of unit tests. I haven’t got an opportunity to test this product, but it looks very promising (hopefully I shall get an opportunity to experiment with it soon).
Olymars: SQL Server Centric .NET Code Generator (code named OlyMars) is both a flexible and powerful code generator based on database modeling. This is a free and unsupported product from Microsoft. I was not updated for quite some time, hence found it a little difficult to understand and use.
Nvelocity: based on velocity, a highly popular template engine for JAVA. This project has been abandoned for lack of community support. (Come on .NET dudes wake up let’s start building strong open source community, aren’t we fed up of taunts from these JAVA guys.)
M3rlin: is a template code generator that uses ASP.NET style syntax. It hosts the ASP.NET runtime to perform code generation. It uses XML as it is the source of meta-data.
MyGeneration so far as I have understood stands above the rest, with its flexible template based approach (in four different languages- JScript, VBScript, C#, and VB.NET), long term commitment from the developers, and an active community support.
For details please visit:
I hope you find this article interesting and useful. Till next time, Keep learning!