Search code examples
c#asp.net-web-apidto

Web API, AutoMapper and DTO/Model


I keep my model in a Model assembly and the DTO classes in a DTO assembly. Both are referenced in the WebAPI project and mappings are created there. The WPF client just references the DTO assembly.

My entities have an 'Id' identity property and I set the access modifier to internal so that the id is only set in the model assembly (backed by EF6). Even the Id in the DTO is set to internal.

The problem is that when the DTO reaches the client, the Id is 0. Do I need to set the accessibility to public? What if the client changes the id and saves the entity? This will cause problems.

Is there another alternative?


Solution

  • The Id field shouldn't be read only, both in the server and in the client.

    They have to be set both in the server by EF, and in the client by the serializer, so you do not want to make it inaccessible. If you don't want the user to mess with it, I'd say just mark it with the [Obsolete] attribute or some other means of warning user, but you should keep it visible and editable.