Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 247725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:17:00+00:00 2026-05-11T21:17:00+00:00

I was just wondering about some CodeWarning (ConstructorsShouldNotCallBaseClassVirtualMethods), and if there is a better

  • 0

I was just wondering about some CodeWarning (ConstructorsShouldNotCallBaseClassVirtualMethods), and if there is a better way to do it. I have a simple log collector class, and I am using NHibernate to retrieve some of the objects.

Some times I create objects by myself (of course) and add them to NHibernate for persistance. What is the best way to make sure that Lists are never NULL.

Currently I am doing this, but it does not seems “perfect”. Any idea on this topic?

public class LogRun
{
    public virtual int Id { get; private set; }
    public virtual DateTime StartTime { get; set; }
    public virtual DateTime EndTime { get; set; }
    public virtual IList<Log> LogMessages { get; set; }
    public virtual int LogMessageCount { get { return LogMessages.Count; } }

    public LogRun()
    {
        LogMessages = new List<Log>();
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:17:00+00:00Added an answer on May 11, 2026 at 9:17 pm

    Is LogMessages a persisted thing? If so, it’s best practice to never expose a public setter. NHibernate gets weird if you retreive from the database and then replace that IList with a new one:

    var myLog = session.Get<LogRun>(1);
    Assert.True(myLog.LogMessages.Count > 0);
    myLog.LogMessages = new List<Log>();
    

    If you note, NHibernate is returning a proxied object and replacing it with a generic list will cause it to go wonky when you try and save back.

    As a rule, I prefer to have a private field that I initialize and then expose only a getter to the client:

    public class LogRun
    {
        private IList<Log> logMessages = new List<Log>();
    
        public virtual int Id { get; private set; } 
        public virtual DateTime StartTime { get; set; } 
        public virtual DateTime EndTime { get; set; }
        public virtual IList<Log> LogMessages { get { return logMessages; } } 
        public virtual int LogMessageCount { get { return LogMessages.Count; } }
    
        public void AddLogMessage(Log log)
        {
            logMessages.Add(log);
        }
    }
    

    Actually, I go a step further, the client gets an IEnumerable<> and I add a helper function for the add.

    My implmentation would look like

    public class LogRun
    {
        private IList<Log> logMessages = new List<Log>();
    
        public virtual int Id { get; private set; } 
        public virtual DateTime StartTime { get; set; } 
        public virtual DateTime EndTime { get; set; }
        public virtual IEnumerable<Log> LogMessages { get { return logMessages; } } 
        public virtual int LogMessageCount { get { return LogMessages.Count(); } }
    
        public void AddLogMessage(Log log)
        {
            logMessages.Add(log);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering how people would go about this. Say I have one class that
I am just wondering if anyone have some info or feedback about the situation
I was just wondering if there was some way I could actually rename the
Just trying to canvas some opinions here. I was wondering how people go about
I was just wondering about something. I have a frame that loads pages and
I am using .Net3.5. Just wondering my understanding about the insert, update, delete are
Im just wondering how to go about using FormAuthentication in asp.net In our project
I'm wondering about http://www.woostercollective.com They have some nice responsive transitions that occurs when window
Just wondering about how to avoid Magic Strings in Querystrings? I have a lot
I was wondering about the best way to do this: I have an app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.