views:

43

answers:

3

Hi All,

I want to have an class like this,

public class Apple
{
    public string Size { get; set;}
    public string Colour { get; set;}
    public string Shape { get; set;}
    public int appleId { get; set;}
}

I want to expose that over web services using some web methods like this,

public void AddApple(Apple apple)
{
}

public Apple GetApple(int appleId)
{
}

So when I add a service reference in visual studio to that webservice I get client proxy objects generated for me allowing me to create an Apple on the client side and send that through to the webservice.

Is there a way for me to make one of those properties read only on the client side? I need the setters there so that I can set the values on the server side, but I want to control which data they can update on the client side.

Any ideas?

What I could do is pass in some of the data in the constructor, and only expose getters on the ones I want to be read only, but I want to use an object mapper on the server side. That means ideally I would want to leave the setters there.

+2  A: 

In general, you cannot assume control over proxies generated at client side. So correct way would be to ignore values sent by client (or raise exception if he changes those values). The service documentation has to be explicitly mention such things.

Edit: Yet another work-around would be to divide your data class into two classes - one non-editable by client (say Apple1) and another editable - say Apple2. So now service update method can only accept Apple2 so that client can know looking at generated proxy code what he can change. On server side, you can have Apple1 inherited from Apple2 to have complete data but I believe that proxy generated at client will/can anyway have two different unrelated classes. Perhaps better way in such case would be to have composite full AppleFull containing Apple1 and Apple2.

VinayC
Another way of achieving this is having two classes. One would be returned from a "getter" method and would have all properties. The other one would be accepted by a "setter" or "adder" method and would have only those properties that can be explicitly set by client.
qbeuek
@qbeuek, you are absolutely right! I was editing my answer (to indicate similar solution) when your comment popped up. Answer is now edited to include the same.
VinayC
A: 

Another way to achieve that would be to share the assembly containing the Apple class (but no server side component) between server and client. Make the setters internal and mark the server side assemblies as friends using the InternalsVisibleTo attribute.

This will allow the server to use the setters but not the client.

Johann Blais
+1  A: 

Please refer to the following question and its answers, I just skimmed through your question but I believe that it is a simillar problem as I faced -

http://stackoverflow.com/questions/3545106/wcf-serialization-and-value-object-pattern-in-domain-driven-design

Unmesh Kondolikar