post header image for Type-Safe Extensions for Unity Type-Safe Extensions for Unity

10th May 2014

As mentioned in my previous post I have been working on a multiplayer game built in Unity for a little while now. I ummed and ahhed over the technology choice for the backend for a little while before deciding to go with's library. The reason being that it looked simple to implement (they have a Unity SDK), they take care of all the backend headaches for me and the pricing model looked fair.

The Parse Unity library is okay, not as fully fleshed as some of the other languages they support but good enough for what I needed apart from one critical point, the lack of type-safety in the queries such as:

[code language="csharp"] new ParseQuery().WhereLessThanOrEqualTo("cost", 13); [/code]

This annoyed me so much that I decided to write some extension methods that take advantage of Lambda functions to provide the type-safety. The above now becomes:

[code language="csharp"] new ParseQuery().WhereLessThanOrEqualTo(a => a.Cost, 13); [/code]

The library can also handle chains such as

[code language="csharp"] new ParseQuery().Include(p => p.Stats.Heath.Remaining); // becomes "" [/code]

The library also handles interfaces by introducing a new attribute "ParseFieldType".

[code language="csharp"][parseclassname("father")] public class Father : ParseObject, IFather { [ParseFieldName("daughter")][parsefieldtype(typeof(child))] public IChild Daughter { get; set; } }

new ParseQuery().Include(f => f.Daughter.Name); // becomes "" and works because ParseFieldType redirects the chain to Child rather than IChild [/code]

It can even handle lists

[code language="csharp"][parseclassname("father")] public class Father : ParseObject, IFather { [ParseFieldName("children")][parsefieldtype(typeof(child))] public List Children { get; set; } }

new ParseQuery().Include(f => f.Children[0].Name); // becomes "" [/code]

To get started go grab the .dll from my Github project for the library: