1

Consider a Web API project with lots of controllers and methods. Said API uses the data transfer object pattern to get the data from the client and to return the results. Those DTO objects are an implementation detail from the point of view of the api, so they are marked as internal to avoid external code being able to see or call them. How does one avoid having to put suppression attributes on the get and set of properties in the dtos for the CA1811 warning?

Right now I'm having to suppress the warnings for each property depending on if the class is a "read" dto, a "write" dto or both. In those cases, the properties need to exist because the json serializer depends on them.

I don't want to open up the classes by marking them public because then I'm exposing something that only the api project should know about, and static tools will not be able to tell if some classes or fields are not being used, because someone else could be using them from the outside.

I could also migrate the DTOs to a separate project and disable the warnings there, but then I'd either need to mark them public again or add a InternalsVisibleTo so that the api project can see them, which negates the benefits again because of this problem.

Community
  • 1
  • 1
julealgon
  • 7,072
  • 3
  • 32
  • 77

1 Answers1

0

You can probably make the setters "protected internal" which would make them accessible to classes that inherit those DTOs (theoretically) in other assemblies. Or just disable those warnings through directives or globally for the project.

Dmitry S.
  • 8,373
  • 2
  • 39
  • 49
  • The DTO classes are all sealed and internal, since they are not supposed to be seen from other projects and there is no inheritance chain using them.`protected` wouldn't make sense and would very likely trigger another warning. I wanted to avoid having to disable them via individual suppressions because there are a lot of them. Disabling it globally is also not ideal because I'll then stop receiving the warnings for other valid reasons. – julealgon Mar 30 '15 at 13:16