Search code examples
c#nhibernatepropertiesdomain-driven-designvirtual

Use of virtual and protected keyword in property settings in c#?


In our project we are using Domain Driven Design pattern and NHibernate mapping. We created domain classes, and in that classes we create some property like this.

    public class Telephone
    {
       public virtual int    Id      { get; protected set;}
       public virtual string Number  { get; protected set;}
       public virtual string Remarks { get; set;}
    }

Here all properties are decorated with virtual keyword and some are with protected keyword. My question is what is the use of virtual and protected keyword in this class?


Solution

  • Virtual properties are used for lazy loading (here is an answer to the similar question), and the protected setters ensure that value can not be changed outside of this class except for its descendants.