views:

21

answers:

2

We have a service reference that points at a WCF service, this acts as a proxy to our model layer where our data access logic is being handled. Under the hood we are using Linq2Sql as the ORM to facilitate the database communication.

We use the generated classes as our data access layer, but what gets returned is actually dumb DTOs objects which are nothing more then POCOs. I would like to do two things)

1) Control what is available on the client through the service reference in terms of custom types and their associated properties. This is to reduce the size of the classes going down.

2) I know that Linq2Sql is actually decorating all of the generated classes with but I do not want these classes coming down through the service reference.

At present, if we use the class as a return type of input parameter it gets serialized. This is fine, except I would like to restrict what properties are available

Thoughts?

A: 

So I found this out. Basically when you create a service reference only the types that are used in some way are serialized down. By default, if no DataContract is present, everything is serialized.

If a DataContract is present, it will look for DataMember decorated properties and serialize those only. Tricky

xximjasonxx
Not tricky - documented! :-) That's why I advocate to **always** explicitly use [DataContract] and [DataMember] on your DTO's. Doing so is a tad more work upfront, but then you're explicit and clear about what gets serialized (and what gets skipped). That extra work will pay off later in maintenance mode - many times over!
marc_s
I agree, I just didnt realize it, actually came to answer AS I was going to the documentation :) funny how that works. And like Hal said, our generated classes are abstracted away and never used as return values
xximjasonxx
A: 

You would like DTO's to pass through your service boundaries, abstracted from your Linq to SQL objects, correct?

If that is the case, then I would recommend defining your DTO's (if you have lots of objects, write or find some good T4 templates) and then using AutoMapper to go back and forth between your DTO and Linq to SQL objects.

Hal
We already have all the DTOs defined. I have worked to abstract any usage (aside from static context) of the generated classes from Linq2Sql because they are just too heavy and cause problems with our JSON serializer
xximjasonxx