Simplifying your data with

I’ve meant to write a post about Simple.Data for quite a while now. It completely blew me away the first time I used it. So clean, so elegant, so simple.

What is Simple.Data?

A bit of background

As you most certainly know, while ORMs are useful, not all projects need a full blown ORMs. You typically need to employ one if the project is large, and if it involves a lot of inserts and updates for a rather complex schema with many relationships.

However, if most of what you do is reading, you do not need to use an ORM like NHibernate or the Entity Framework. In such circumstaces, your app will be better off if you just read from database using traditional means (via sqlconnection, datareader, etc..). Just look at the table here for proof. This has led to the creation of Micro-ORMs.

Micro orms such as dapper dot net and Massive are attempts to tackle this issue. They are more focus on the extraction of data than the insertions/update, based on what I’ve seen. You typically can’t just give them an object and expect them to figure out the relationships. While you can eager load data with some of them, lazy-loading is typically not builtin as a feature. You also have to write your own sql every now and then…which isn’t such a bad good thing.

So what is Simple.Data?

To put it simply, it’s a super simple ORM-like-data-access-framework created by Mark Rendle. He swears it’s not an Orm…but if it looks like a duck, quacks duck, walks like a duck, it is a duck 😀

Simple.Data is a lightweight framework that uses the dynamic features of .NET 4.0 to provide an expressive, ORM-ish way of accessing and manipulating data without any of the code pre-generation and boilerplate required by other frameworks – Mark Rendle

This framework has made my life easier. How?

Say we have a story class which maps to a table called “Stories”. Normally, to query for data I would write a Story Repository class with various method to retrieve the items based on the fields of the Story class. With Simple.Data, it all comes for free.

The query above eager loads the project and the tasks associated with each story. When I want to lazy load them, I can just return all. Since I am keeping the dynamics, using the Project property or even the Tasks property will make it fetch the data. Pretty cool uh! But wait, there’s more…

I can essentially use “FindBy” and accompany it with any of the column on the table. This is a built in feature. Under the covers, Simple.Data creates the query. How? From what I can tell, it achieves this using the various dynamic features added to .NET 4.0, as well the Expressions features that came up with LINQ in .Net 3.0 and .Net 3.5. It breaks down the name of the method being called and its parameters, thus knowing what call to the database to make. The code base itself is quite interesting. I have only just started to dig deep into it to understand how he does things.

You don’t always have to bring back all the rows in the database. You can use a select query in which you specify which column(s) need be brought back.

Simple.Data also makes inserts easy.

Simple.Data is a nice medium between a micro-orm and a full fledged one like NHibernate. However, because it relies heavily on dynamics, it may cause subtle bugs that won’t get caught until runtime. For example, you might have mistyped a table name, which is something the compiler could have caught…but because everything is dynamic it will compile just fine and blow up once that statement is executed. Hencewhy unit testing is important when using it. But that shouldn’t discrourage you. For more information, checkout the docs and the repository on github

Simplifying your data with

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s