What is the best practice when implementing a data transfer facility or POCO? What should your POCO look like?

Which of the following is the best way to implement poco?

Option 1:

public class PocoOption1
{
  public PocoOption1(int val1,string val2)
  {
    Val1=val1; Val2=val2;
  }

  public int Val1{get;private set;}
  public int Val2{get;private set;}
}

Option 2:

public class PocoOption2
{           
  public int Val1{get;set;}
  public int Val2{get;set;}
}

What are the potential benefits of each approach? Does it matter? Some people say that DTO should be installed only once. Is this a rule or just an opinion?

+3
source share
2 answers

The options are slightly different. Option 1 allows you to install Val1, Val2 only once. Option 2 allows you to set and reset those values.

, . API . " ". :

  • ", , ". . ( ). .
  • - . , .
  • . , .
  • . , API. ( , InvalidOperationException, .)

, 2 - .

+3

#, , :

Customer customer =  new Customer{ Id = 1, 
     Name="Dave",                                             
     City = "Sarasota" };

2.

, .

+3

Source: https://habr.com/ru/post/1704607/


All Articles