Is it OK to call virtual properties from the constructor of a NHibernate entity?

前端 未结 6 613
有刺的猬
有刺的猬 2020-12-11 01:02

take a look at this example code:

public class Comment
{
    private Comment()
    { }

    public Comment(string text, DateTime creationDate, string authorE         


        
6条回答
  •  没有蜡笔的小新
    2020-12-11 01:17

    IMHO the best-practice is to use properties with backing fields:

    public class Comment
    {
        private DateTime _creationDate;
        private string _text;
        private string _authorEmail;
        private Comment() { }
        public Comment(string text, DateTime creationDate, string authorEmail)
        {
            _text = text;
            _creationDate = creationDate;
            _authorEmail = authorEmail;
        }
        public virtual string Text
        {
            get { return _text; }
            private set { _text = value; }
        }
        public virtual string AuthorEmail
        {
            get { return _authorEmail; }
            private set { _authorEmail = value; }
        }
        public virtual DateTime CreationDate
        {
            get { return _creationDate; }
            set { _creationDate = value; }
        }
    }
    

    So you can avoid problems on child classes and you don't see any warning anymore

提交回复
热议问题