mercredi 13 juin 2018

Should a DTO/POCO have a constructor and private setters on all properties?

I know there are many discussions here about DTOs and POCOs, but I couldn't really find one about this. Is there a rule on writing DTOs without constructors vs private setters and constructors?

Example A:

public class Person
{
    public int Id { get; set; }
    public String Name { get; set; }
    public int Age { get; set; }
}

Example B:

public class Person
{
    public Person (int id, String name, int age)
    {
        Id = id;
        Name = name;
        Age = age;
     }

    public int Id { get; set; }
    public String Name { get; set; }
    public int Age { get; set; }
}

Is any of the two approaches considered anti-pattern? Why? I mean, I know one could argue in favor of immutability or boiler platting and refactoring issues, but is there a de facto approach, something official?

Aucun commentaire:

Enregistrer un commentaire