Search code examples
c#asp.netvirtualdto

Do we need to use Virtual keyword while writing properties in my Dto?


while declaring all properties of my main class I have used Virtual keyword. Now I am wondering if need to use virtual keyword in my dto class or not?


Solution

  • Generally the conceptual Data Transfer Object is not thought be be a nested class, because it is intended to transfer data.

    I sometimes end up with a nested structure where shared values are on the base class(es) but then they are not on the derived classes also. If imaginable like @tony wrote sure you can - But as the purpose of a DTO is to serialize in and out .net object entities and the wire it is on the verge of being against the concept imho.

    Where i would use it is in Model Classes, that which interacts with your business layers and data storage layers, but these I would not use for DTOs to maintain freedom to change things. So whilst in quick implements DTO classes end up being Model classes too, we should remember that this is a bit hacky and only defendable while the two classes would be identical.

    Though with entity framework and the likes, it is not practical to suffix a class with DTO as it is waiting to get stupid-slapped by ones colleagues when the tables are suddently auto generated with the data transfer suffix XD :).

    So, No.