views:

718

answers:

2

I've got a LINQ 2 SQL generated class I'd like to expose through a webservice. There are some internal properties I don't want to be available.

Normally I'd throw [XmlIgnore] in there but because the properties are in the generated half I can't do that.

I've been looking at using MetadataType following this post which looks like it should allow me to define the property attributes in another class.

My code looks something like this:

[MetadataType(typeof(ProspectMetaData))]
public partial class Prospect : ApplicationBaseObject
{
}

public class ProspectMetaData
{
     [XmlIgnore]
     public object CreatedDateTime { get; set; }

     [XmlIgnore]
     public object AmendedDateTime { get; set; }

     [XmlIgnore]
     public object Timestamp { get; set; }
}

I'm referencing this through an ASP.NET Web Service from a Silverlight Project.

The issue is that the [XmlIgnore] attributes are being ignored, those properties are being sent through.

Does anyone have any insight into what might be going wrong here? and what might be the best way to do this?

+1  A: 

AFAIK, MetadataTypeAttribute is not supported by XmlSerializer (although it would be nice - I've simply never checked). And as you say, you can't add member attributes in a partial class.

One option may be to make the generated properties non-public (private, protected or internal) - and name it something like TimestampStorage (etc) - then re-expose them (in the partial class) on the public API:

 [XmlIgnore]
 public object Timestamp {
     get {return TimestampStorage; }
     set {TimestampStorage = value; }
 }
 // and other properties

(since XmlSerializer only looks at the public API). The biggest problem here is that LINQ-to-SQL queries (Where etc) will only work against the generated columns (TimestampStorage etc). I've used this approach before with the member as internal, allowing my DAL class to use the internal property... but it is a bit of a fudge.

Marc Gravell
It certainly seems that way and it's a real shame.I have no control over the accessors on the generated properties so it doesn't really solve it for me.I guess I'll just have to go with a proxy class rather than sending my business objects.
TreeUK
+1  A: 

I agree with Marc. The easiest thing to do is mark them internal. Optionally, you can then re-expose them in the partial class with [XmlIgnore]. BTW, You can control the accessibility of the properties in the linq2sql designer. Get at the properties dialog and you'll see a place to set them

Erik Mork
We're actually using our own code generation tools to output the generated halves, when I said LINQ2SQL, it's the closest parallel. This might be handy for others though. +1
TreeUK