C# Dto constructor and dependency injection

后端 未结 2 847
鱼传尺愫
鱼传尺愫 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 13:00

    Value types as in your examples aren't dependencies. A dependency provides a functionality (or configuration) to the consumer. In your case they are just normal values that are assigned to your DTO. As long as the data belongs together you do not violate SRP even if you assign a lot of values in the constructor. The single responsibility in this case is to hold the data.

    Also DTO's shouldn't be created by a IoC container and have no real dependencies. You should create them manually, by your persistency framework or using auto mapping.

    If assigning the values using a constructor or properties is better depends on the usage. If they are required the constructor variant is better. If they are optional the property way is better.

提交回复
热议问题