Friday, 17 February 2012

Are Micro ORM’s Ready For Every Day Use?

There is a growing trend in the use of Micro ORM’s such as Dapper, Massive and PetaPOCO, but how easy are they to use in everyday life compared to the “fuller” ORM’s such as Entity Framework and NHibernate? These micro ORM’s have been gaining a lot of attention over the last twelve months; there are a number of large websites that use these so are they ready for everyday use by the average coder? What are the advantages over the “fuller” ORM? So lets delve into the world of micro ORM’s.

Speed

The main advantage over the “fuller” ORM’s is speed. Courtesy of Dapper’s homepage, here is a comparison of the speed differences between Micro ORM’s and the more “full” ORM’s when considering the performance of SELECT mapping over 500 iterations (POCO serialization).

MethodDuration
Hand coded (using a SqlDataReader)47ms
Dapper ExecuteMapperQuery 49ms
ServiceStack.OrmLite (QueryById)50ms
PetaPoco52ms
BLToolkit80ms
SubSonic CodingHorror107ms
NHibernate SQL104ms
Linq 2 SQL ExecuteQuery181ms
Entity Framework ExecuteStoreQuery631ms

As you can see, the Micro ORM’s performance is more than half the speed of NHibernate which is the quickest “full” ORM and over six times quicker than Entity Framework.

Simplicity

So the micro ORM’s have speed on their side but what else? Personally speaking, the simplicity of using a micro ORM is a huge advantage. Taking Massive as the example, all you need to do is add the Massive.cs class to your solution. The class is very light-weight, weighing in at only 524 LOC (lines of code); compare this to Entity Framework Code First which is about 776 KB of compiled code.

All of these micro ORM’s have a simple drag and drop way of working, with little or no configuration. Once you have worked this way for a while, if you then go back the NHibernate xml files or Entity Framework Database First approach for configuring your ORM’s you will question why everything is not as simple as it could be by default.

Opinionated

One of the greatest strengths of using a micro ORM can also be one of its greatest weaknesses. Most of the micro ORM’s are view opinionated in that they expect your database to follow a convention, and your data to be structured a certain way. When this is not the case, the micro ORM’s can become stuck, or the simple approaches used to get data are no longer available and you have to start writing your SQL by hand.

If you’re considering using a micro ORM for an existing database or domain model then the rigidness of the micro ORM needs to be considered. However if you’re lucky enough to be able to start a project from scratch then a micro ORM is a very tempting option.

Who’s Using Micro ORM’s?

Micro ORM’s are already being used out there in the wild and to great success.

Dapper is being used to handle all the SQL on Stackoverflow and the Stack Exchange network of sites, Xapfest and Help desk. Massive is used to power HanselMinutes, with Scott Hanselman detailing how he did it here.

So as we can see the Micro ORM’s are quicker and are already being used in production sites. Over the next few weeks I will be detailing how you can get started using these micro ORM’s, any problems or issues that may come up and how they can be used in real applications.