views:

1719

answers:

1

Is it possible to pass generic objects (by generic I mean any with DataContract, not C# generic collection) without inheriting from one root? At the moment I have something like this:

[OperationContract]
void Save(GenericObject o);

[DataContract]
[KnownType(typeof(ClassA))]
[KnownType(typeof(ClassB))]
class GenericObject {....

[DataContract]
class ClassA: GenericObject {....


[DataContract]
class ClassB: GenericObject {....

Everything was OK, but the project has got bigger and I cannot really afford to derive all those contracts from one class.

I don't want to add specialized methods to the service. As a workaround I can manually serialized classes to string or byte[] via DataContractSerializer and deserialized them on the server side: void Save(byte[] serializedObject) but that's a bit of a hack.

I've noticed that I can easily return generic objects e.g. object Load(string id) works just fine, but void Save(object o) does not.

I'm using Silverlight as a client.

+1  A: 

Yes, you can use a generic Message type for WCF projects, which can be anything, really. However, this imposes a lot of work on you, both on the client side creating the message object (you basically have to manually create the XML that makes up the generic message), as well as the server side, since you can't rely on the nice XML serialization/deserialization anymore. You're dealing with basic, raw angle-bracket soup (straight XML).

It works and it could be a solution - but maybe there are others, more elegant and useful solutions? Is that inheritance really needed at all? Think about your systems design.

Marc

marc_s
No, the inheritance is absolutely unnecessary - its only purpose was to provide workaround for passing generic objects to the storage service. But since the project got bigger we cannot really afford to inherit most of our DataContract objects from the single root.
Karol Kolenda
marc_s is correct. Receiving a Message type would allow you to use any type, regardless of inheritance. I also agree that you might think about why you are using a WCF service here at all. If you are just moving your data access layer to a WCF service you are missing the point.
Anderson Imes