views:

26

answers:

1

Hi, I have an existing web application that uses EF and POCO objects. I want to improve the client experience by exposing some of my objects through WCF(JSON). I have this working fine but where I am unsure is how to handle derived objects(not sure if that is the correct term) or IEnumerable anonymous objects if you will.

Let's say I have 3 tables structured like so:

Templates

ID
Template

Groups

ID
Group

Instances

ID
TemplateID
GroupID

This is obviously a one-to-many type relationship. I have my navigation properties setup correctly and getting strongly typed object properties works great. However, how do I send serialized anonymous type object(s) over the wire. Like an object that sends all instances that are equal to groupid=1 and include the names of the template and the object.

Am I missing something or do I have to create another class object for WCF that would look like this:

WCF Object

InstanceID
TemplateID
TemplateName
GroupID
GroupName

I guess I could alter my tables to account for this but that seems wrong too. I know that IEnumerable objects can't be serialized and I know that throw away objects are probably not the way to go either. I want to do this the right way but I am not sure how to go about it.

Your suggestions are appreciated. Regards

+1  A: 

Based on what you're doing, I'd suggest looking at OData with WCF Data Services. You state that you want to be able to send all instances where the groupid=1 - OData is great at this type of filtering.

If you're want to stick with your current approach and not use OData, then my first question is why are you sending back anonymous types at all? You can do what you are seeking (all instances with a groupid=1) without sending back an anonymous type. In your select clause you just create new instances of your concrete objects rather than newing up anonymous types. If your query is really just filtering and not executing any meaningful projection with the selct to anonymous type, then I don't see any reason to send back your anonymous type at all.

Steve Michelotti
I thought I was familiar with OData but I took your advise and spent some more time going over the spec and found it to be what I was looking for. I guess I thought best practices where to only expose data tables as they exist in the db as objects. I wasn't sure how to go about exposing collections of various object properties. odata.org gives some great examples. Thanks Steve.