Class name: Add DTO or Entity

Is there any preference for adding DTO

or Entity

to the class name?

Is there any standard around this?

1 The class is used by ORM (EntityFramework) and another class is used for serialization.

The reason for this is that there is no duplication of all fields, since the EntityFramework is a wrapper around the DTO class (most, but not all properties).

The DTO class is in a shared library and is separate from EF.

eg. Which one is the most common / standard approach?

// 1.
MyNamespace.Entities.MyClass
MyNamespace.Models  .MyClassDto


// 2.
MyNamespace.Entities.MyClassEntity
MyNamespace.Models  .MyClass


// 3.
MyNamespace.Entities.MyClassEntity
MyNamespace.Models  .MyClassDto

      

+3


source to share


2 answers


In my personal experience, your third example is the only implementation I have worked with and this is the one I would say about because the purpose of the object you are working with will always be clear, whereas with the other two it will become clear when viewed both objects.



This is talked about while your team comes to an agreement on which one will work.

+1


source


In my opinion, you generally don't want to put implementation details in class names, for the same reasons why you don't want to use Hungarian notation.

If there is a bit of code that needs to work with and differentiate both types, the other parameter includes aliases using assertions like this:



using entities = MyNamespace.Entities;
using dto = MyNamespace.Models;

//in code
var myClassEntity = new entities.MyClass();
var myClassDto = new dto.MyClass();

//work with both

      

My guess is that code that needs to work on both types is limited to a sandboxed library, and that client code usually works on one rather than two types.

+1


source







All Articles