1

I have a class defined as such:

public class LogEntry
{
  public string Number {get; internal set;}
}

On the client side when I consume the class, the Number property doesn't exist. But if I remove internal, then the property exists, but for both get and set.

I just want the client to be able to read its value, not setting it.

Ray Cheng
  • 12,230
  • 14
  • 74
  • 137
  • I used to want to do the same thing because from an "clean API design" perspective, this seems to make much sense. However I am slowly seeing that this might not be worth the effort: Services often accept and produce dumb data objects without any behaviour. All the client should do with a service response is extract information from it. If the client also wants to modify the service response (even though that doesn't appear to make much sense), why not just allow it? It won't hurt your service. – stakx - no longer contributing Apr 23 '15 at 17:12
  • @stakx, that make good sense. If I don't provide them the API to save the modified data back, they can't do any harm to protected properties. I'll keep that in mind. – Ray Cheng Apr 23 '15 at 17:17
  • If some operation of your service returns a `T` and another operation accepts a `T`, then make sure in the "accepting" method to validate the `T` object passed in. If you do that, you should be on the safe side, whatever the client does. – stakx - no longer contributing Apr 23 '15 at 17:18
  • @stakx, that's right. But in this case, there will be no update or delete api provided, only insert and read. So there's zero chance that user can modify data they are not suppose to. For insert, I'm assigning a new value to `Number` prior to saving it, so that's taken care of. The only thing is that it may confuse someone because they set a value, but when try to retrieve by it, it's not found. I'll make it super clear in docs. – Ray Cheng Apr 23 '15 at 17:28
  • If you still want to do read-only properties, I've written a detailed answer [here](http://stackoverflow.com/a/29830975/240733) that demonstrates how to do it. – stakx - no longer contributing Apr 23 '15 at 17:58

0 Answers0