Simple.Data does not need POCOs

This is a Public Service Announcement


Simple.Data returns dynamic objects. You don’t have to use statically-typed POCOs unless you want to.

And in full for those without Attention Deficit disorders

I just caught this post on Google+, about using Simple.Data with MongoDB. (I’m really pleased that there’s a MongoDB adapter, because (a) MongoDB is awesome, and (b) it’s the poster child for the fact that Simple.Data is not exclusively an RDBMS-targeting solution. The adapter works really well, although some query operations don’t make sense in MongoDB; I’m working on some code for Core which provides polyfills for any operations which adapters might not be able to interpret meaningfully, such as grouping and aggregates and so forth.)

Anyway, back to the main point of this short post. The comments from Jak Charlton about static typing made me realise that there is potentially a fundamental misunderstanding about what Simple.Data actually does, and a belief that it requires static POCO types to work. It doesn’t, but I haven’t made this clear enough, so sorry about that, and this is me rectifying that situation.

So this misunderstanding probably arises from the fact that most of the demo apps and blog posts that have been written do use POCOs, doing things like this:

User user = db.Users.FindByEmail(email);
VerifyPassword(enteredPassword, user.Password, user.PasswordSalt);

That code works, but in terms of clarity it hides a step in the process. If I split it into two steps, it becomes clearer:

var record = db.Users.FindByEmail(email);
User user = record;
VerifyPassword(enteredPassword, user.Password, user.PasswordSalt);

The first line creates an object of type SimpleRecord, and the type inferred for var is dynamic. (SimpleRecord is very similar to the BCL ExpandoObject type, but with some data-access-specific intelligence.)

The second line performs a kind of “implicit cast” of the dynamic SimpleRecord object to the static User type. (SimpleRecord handles this by overriding the DynamicObject.TryConvert method, if you’re interested.)

The Very Important Thing that I want you to take away from reading this post is that the second line is not required. This code will work every bit as well:

var user = db.Users.FindByEmail(email);
VerifyPassword(enteredPassword, user.Password, user.PasswordSalt);

Here, user will be compiled as dynamic, created as a SimpleRecord instance, and will return values for the Password and PasswordSalt properties when asked. You just won’t get IntelliSense™ for the object.

Other things you can do with these dynamic objects is pass them to your View for rendering, or bind to them in WPF applications. If you don’t want the “statically typed noose dragging you down” (and personally I really don’t), then Simple.Data is your new best friend.

Tonight’s movie

will be Pitch Black. A group of marooned space travellers struggle for survival on a seemingly lifeless sun-scorched world.

That is all.


  1. PItch Black is a fine B-movie. Enjoy.

  2. Mark, good post. I couldn’t agree more, which is why Let Me Rate doesn’t have statically typed POCOs for simplicity.

  3. Thanks for posting this Marc, it’s a good follow up from my G+ post. I can see why some people don’t want POCO’s, and others do.

    For what I’m using it for, it makes little difference either way, but having them in there simplifies other parts (pushing out to WCF) ’cause I can’t always trust that they’ll have great dynamics support.

    Either way, Simple.Data is pretty rad 🙂

  4. 🙂 I think you were assuming my comments about POCOs and static typing were in reference to SimpleData … they were actually in reference to a frame of mind that moves to POCOs as the “simple database solution”, when you can go one step further and dump them in most cases.

    (and p.s. may well use SimpleData on my next project 🙂

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

%d bloggers like this: