2

I'm creating a website using Entity Framework (4.1), which exposes WCF services, and I have a Silverlight client communicating with the server over WCF. I want to send (and possibly receive) some Entities over the WCF service, without creating a proxy/wrapper etc. class. For example, I have a User entity type with Username, Password, Email fields (actually many more but shortened just to keep this simple) and I want to send this object to the Silverlight client. While sending Username and Email, I (obviously) don't want to send the Password property to the client. Now, I'm writing a wrapper with attribute [Serializable] and [DataContract] attributes, with fields having the [DataMember] attribute, which includes the fields only that I need, create a new instance, copy the values from the entity object, and send that proxy object. It works, but it's inconvenient and extremely painful. What I need is a mechanism where I can directly set the properties I want to be sent, and simply don't expose anything else. I can't change the code of the entity model (to add/edit attributes) as it gets auto-regenerated each time I touch the EDMX. Is there a way to accomplish exposing only the selected members to the clients?

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
  • 2
    Good question! Exposing your own custom POCO is the only way I would know how to do it. – Jay Aug 10 '11 at 14:22
  • 1
    I agree with Jay. You should create a DTO class decorated with the [DataContract] attribute containing only the [DataMembers] you need. You are going to need to map from your EF generated entity to the DTO. – Nick Ryan Aug 10 '11 at 14:28
  • well, ok then, i'm continuing the same way I'm going as it's what i'm doing.. i just wanted to be sure that it was the best way.. i could accept that answer, if that was submitted as answer anyway :) – Can Poyrazoğlu Aug 10 '11 at 14:57

1 Answers1

2

To avoid the PROXY entites on the client, you can reuse the same entities (client and server side) by making sure that your client already references the server Entity assembly (assuming that you have mapped POCO's in EF and separated them out into their own assembly). Then check the advanced options in the server reference and ensure "Reuse Types in referenced Assemblies" is checked.

This will create a proxy Interface and client, but reuse the server Entities.

You can avoid the proxied interface as well by using the ClientBase<> generic instead of the service reference, although you will now need to strip out your Service side Interfaces (Service Contract interfaces) into a separate assembly and reference this on both Client and Server.

And as per your observation, if you don't mark a Property on your Entity as [DataMember], it won't be serialized. Since you've got the same entity both sides, fields like will come out as their default value (0, null etc) on the client.

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • Despite it being a nice workaround, it's not a good programming practice to have default/null values for non-exposed members, but i'll take a look into it.. – Can Poyrazoğlu Aug 10 '11 at 14:58