Hello,

I would like to have a class like this : - persisted : so inherits from Entity (that implements ISerializable ) - with [DataContract] attribute, because this class is used by a WCF service.

So I have the following issue : a class could not be ISerializable and have attribute [DataContract] at the same time.

Question : why your class Entity is ISerializable?

thanks. Maurice.

nb : I use version 4.3.5

asked Dec 03 '10 at 10:39

Maurice's gravatar image

Maurice
15112

edited Dec 07 '10 at 03:48

Alex%20Yakunin's gravatar image

Alex Yakunin
29714412


2 Answers:

Have you look at installed ADO.NET Data Services (Astoria) Sample Solution ? If you use ADO.NET Data Services you can use your model against WCF. In fact client makes proxy classes to communicate through WCF with server side.

But if you need pure WCF service with your own methods, you must work in another way. One solution is to create DTO classes and map DO entity classes with these DTO classes using O2O mapper (using MappingBuilder class). And on client side, you use these DTO classes, on server side you execute normal DO4 linq query, and result transform into DTO classes using Mapper.Transform.

answered Dec 06 '10 at 04:54

Peter%20%C5%A0ulek's gravatar image

Peter Šulek
492313236

edited Dec 06 '10 at 04:56

1

I create diagram to one solution which could be made to satisfied some WCF requirements. I also have some hard-coded working sample of this, but it is strongly applicable only on my own project (strong project references and other things), not general reusable. But can help if you need help.

(Dec 06 '10 at 05:03) Peter Šulek Peter%20%C5%A0ulek's gravatar image

Great diagram. Yes, that's another good alternative.

(Dec 07 '10 at 04:22) Alex Yakunin Alex%20Yakunin's gravatar image

We use ISerializable because Entity really can be serialized as part of graph (currently - only as reference to actually existing entity).

That's intended usage of this feature:

var graph = ...; // anything, containing references
                 // to your Entities, EntitySets, etc.
var serializer = 
  new NetDataContractSerializer();

using (var stream = new MemoryStream()) {
  // Serliaizing
  var serialiaztionContext = 
    new SerializationContext(SerializationKind.ByReference);
  using (serialiaztionContext.Activate()) {
    serializer.Serialize(stream, graph);
  }

  // Deserializing
  stream.Position = 0;
  var graphClone = serializer.Deserialize(stream);
}

Note that if another Session was active during graphClone deserialization, deserialized graph will reference entities from this Session instead of original (Session isn't serializable itself, so we can't store reference to it in serialized graph).

SerializationContext is necessary to specify how to serialize the entities in graph:

  • SerializationKind.ByReference indicates they must be serialized as references. This implies we'll try to "connect" existing entities to the graph on deserialization instead of creating new ones.
  • SerializationKind.ByValue indicates they must be serialized by value (although some of them still can be serialized as references). Currently this mode isn't implemented, but this is one of our near-time goals. Deserialization of such graph implies that clone of each entity serialized by value will be created in the database as well. This serialization mode must be ideal for operations like import-export and copy-paste.

So you can't send Entities via WCF directly. The fact they're marked as serializable does not enable this - the purpose of this is different.

To send them via WCF, you can:

  • Use WCF data services (see AstoriaSample sample from Sandbox). But note that in this case entities on other side actually won't be our entities any more - WCF will provide client-side proxy classes for them.
  • Use DisconnectedState, MovableResult and MovableOperationLog (see WCF Sample). It relies on this serialization feature and serializability of DisconnectedState to send graphs of entities between client and server with their state, as well as send log of applied changes back to the server.

Question : why your class Entity is ISerializable?

Hopefully, I just provided full answer to this question.

answered Dec 07 '10 at 04:21

Alex%20Yakunin's gravatar image

Alex Yakunin
29714412

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA