Adventures of a wannabe geek!

Ranting within

Simple.Data – a New Wave of ORM?

A while back I seen some tweets from Mark Rendle on his version of Microsoft.Data, Simple.Data, as it seemed to be causing distress for developers when it was announced earlier.

I offered to give Simple.Data a trial for Mark as I was doing a small personal project at the time that was far too simple to use a well known ORM or a horrible amount of ADO.NET code. So after getting the sample solution from Mark and reading through the integration and unit tests, my eyes were opened up to a new wave of ORM.

Lets talk about the requirements of adding this ORM to our solution:

  • .Net 4 framework
  • Connectionstring called Simple.Data
  • References to
    • Simple.Data.dll
    • Simple.DataSetExtensions.dll
       

Yes that’s it! Its that easy. I was suitably impressed straight away as I have played with a  few ORMs that require some configuration and mappings files to be created. This was very early on for Simple.Data, so there was not much documentation. In-fact the tests were the documentation :)

So I was using this to kill the need to use ADO.NET and we all know how much code we would have had to write to get that working. In fact, I’m sure I would have used Enterprise Library or something to get it going and make things easier. (Lets assume I would have used Ent. Lib for code examples.)

First off was to write some code to select data from a users table. With ADO.NET the code would have looked as follows:

public User GetUserById(int userId)
{
    var returnedUser = new User();
    try
    {
        var command = db.GetStoredProcCommand("storedprocname", userId);
	    var reader = db.ExecuteReader(command);
	    while (reader.Read())
	    {
	        returnedUser = User.GetUserDetails(reader);
	    }
    }
    catch (Exception ex)
    {
        DataAccessExceptionHandler.HandleSqlException(ex);
    }
    
    return returnedUser;
}

How does this look with Simple.Data? Well as simple as this:

public User GetUserByUserId(int userId)
{
    dynamic db = Database.Open();
    var dbUser = db.Users.FindByUserId(userId);

    if (dbUser != null)
    {
        return dbUser;
    }
    else
        return new User();
}

This makes it a LOT simpler and no need for a stored procedure. Basically it uses the dynamic keyword of C# in order to get the properties of the User table for a specified user and then puts the column names and their values in the dynamic object.

The same shortening and less complex methods are in place for getting lists of users and items from the database as well as inserting (using Named parameters) and also deleting rows by a specified column name. This was a basic introduction about how easy I found Simple.Data to use. Please read more about Simple.Data at Mark Rendle’s blog