C# Dto constructor and dependency injection

后端 未结 2 848
鱼传尺愫
鱼传尺愫 2021-01-11 12:17

I would like to know what is the best practice in designing the constructors of DTO objects.

say i have a Dto object like this:

class CustomerDto
{
          


        
2条回答
  •  青春惊慌失措
    2021-01-11 12:51

    I would suggest using immutable data structures so DTO entity would not expose any setters, obviously in this way constructor should provide ability to initialize all underlying properties of a given DTO.

    So I prefer:

    public CustomerDto(string name, string surname, string phone, ...) 
    

    DTO is a Data Transfer Object, especially to represent a set of properties to be passed across a system (distributed as well) boundaries, so do not bother too much regarding SRP violation. This is like Facade design pattern, it abstarcts a set of operations/services to simplify usage. So in this kase Keep It Simple won.

提交回复
热议问题