Ok, let’s make our DTO a bit more complex (or more real life like I would say)
so we added list of MyItems to our DTO. We also marked this list with [Include] and [Association]. It’s very important part and soon you’ll see why. Let’s test our DTO, is it really able to transfer data to/from client? Server-side is
Note: for some strange reason RIA won’t allow you to use sets/enumerations of DTO as parameter, so the following will not compile. Be aware!
the workaround is simple – just encapsulate your list inside another DTO class ;)
ok, now client side
Let’s run it… boom! Assertion failed! We expected our list be non empty but it is ;( You might ask – why? We did all good server side. Well.. not really ;) Here comes very important part – how client client side code generated by RIA handle references. It does handle it via associations. The same way as relational databases handle foreign key associations. So to make it work you have to provide valid values of properties that make association. Here is the code it uses internally (generated client side)
and the magic happens here
so, as you now see, to fix our problem we have to do this
Well, let’s give it another go. F5… same result! The sad truth is that unfortunately at the moment [Invoke] operations don't support complex composite DTOs. The only place you can use it is either [Query] methods (as a result) or in Submit based operations. Which means that we can either wait till RIA team fixes this awkward functional gap or we could use normal WCF service or this simple but ugly workaround
the idea here is pretty banal – use part of Submit processing as our custom operation.
The most painful limitation of using DTOs in WCF RIA is that you can’t use DTOs as [Query] methods parameters
will produce following compilation error
Parameter 'data' of domain operation entry 'MyQuery' must be one of the predefined serializable types.
Why??? Why RIA team has decided that providing only basic types as parameters would be enough? Take a look outside your Ivory Tower guys – there is real world outside and developers do need complex query parameters (you just can’t express everything with IQueryable<T> and simple typed parameters)
The workaround is as usual simple and ugly – you can expose your parameter as string, serialize your DTO on client and de-serialize on server
Ok, to summarize – by now RIA has very limited support for passing DTOs back and forth. We can pray and wait for this to be fixed or use dirty workarounds.
Happy RIA coding, to be continued…