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:
[sourcecode language="csharp"]User user = db.Users.FindByEmail(email); VerifyPassword(enteredPassword, user.Password, user.PasswordSalt);[/sourcecode]
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:
[sourcecode language="csharp"]var record = db.Users.FindByEmail(email);
User user = record;
VerifyPassword(enteredPassword, user.Password, user.PasswordSalt);[/sourcecode]
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:
[sourcecode language="csharp"]var user = db.Users.FindByEmail(email); VerifyPassword(enteredPassword, user.Password, user.PasswordSalt);[/sourcecode]
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.
will be Pitch Black. A group of marooned space travellers struggle for survival on a seemingly lifeless sun-scorched world.
That is all.