Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why to have private setter in entity

Still getting used to Entity framework but I have seen code like below where they have private setter for id in Entity.

public int Id { get; private set; }
public string FirstName { get; set; }
public string LastName { get; set; }

Why should some have private setter. This Id field is anyway auto-generated in database and is that reason its set to private?

Also why do we need private constructor and public constructor in entity like below?

private Emp() { }

public Emp(string name, string lastname)
{
    FirstName = firstname;
    LastName = lastname;       
}
like image 304
James Avatar asked Mar 03 '17 07:03

James


3 Answers

You don't ever need to set primary column value yourself, exactly because it's autogenerated by database, so why allow to do things that do not make sense? Hence you make Id setter private. EF can still set this property when materializing object, even though it is private.

Same story with constructor. EF requires your entity to have parameterless constructor, but it can be private. But you don't want (in your example) for entity to be created by user without providing first and last names, because most likely those names are required and you want unavoidably express this intention. So you have one constructor for you to create entity (with both names set) and one for EF to materialize object received from database (parameterless one).

Note that both private setter and this configuration of constructors are in no way required by EF. All this is done for developer convenience to prevent undesired behavior (setting Id field or crearing Emp entity without providing names).

like image 76
Evk Avatar answered Sep 30 '22 06:09

Evk


Private setter is useful to provide a read only property to the user , means it wont allow you to modify it. Because some properties like ID you dont want it to modified or ,if you want to add some validations or setting the property on the class level(from with in the class). In that case we use private setter like .

public int Id { get; private set; }

or some times like

private int Id ;
public int Id 
{
    get { return Id ; }
}
like image 41
Viplock Avatar answered Sep 30 '22 04:09

Viplock


In addition to the answers provided, with the introduction of C# 6.0, you no longer need private setters to set the value of a property.

You can use the following code instead of private setters:

public class Appointment
{
  public DateTime TimeStamp { get; } = DateTime.UtcNow;
  public string User { get; } =
    System.Security.Principal.WindowsPrincipal.Current.Identity.Name;
  public string Subject{ get; } = "New Subject"
}

You can check here for more information

As for private constructor: private constructors are used you don't want a class to be created by code outside of the class. Singletons, factories, static method objects are examples of where it is useful to restrict the constructor.

like image 20
S.Dav Avatar answered Sep 30 '22 05:09

S.Dav