title: Good class design by example
body: <p>I am trying to work out the best way to design a class that has its properties persisted in a database. Let's take a basic example of a <code>Person</code>. To create a new person and place it in the database, I want the <code>DateOfBirth</code> property to be optional (i.e. NULLable in the DB).</p>
  
  <p>Here's my sample code:</p>
  
  <pre><code>namespace BusinessLayer
  {
      class Person
      {
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public DateTime DateOfBirth { get; set; }
      }
  }
  </code></pre>
  
  <p>I'm unsure as to whether the fields should be public or not. Should I do it like this:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Person person1 = new Person("Kate","Middleton",null);
      }
  }
  </code></pre>
  
  <p>or like this:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Person person1 = new Person();
          person1.FirstName = "Kate";
          person1.LastName = "Middleton";
      }
  }
  </code></pre>
  
  <p>I'm also wondering how I should be dealing with the optional properties of the class. Once the fields have been populated how do I then save them to the DB? I have a DatabaseComponenet class to save the information. How do I deal with the optional when saving to the database?</p>
  
  <p>So, would I do something like this:</p>
  
  <pre><code>public int Save()
  {
      int personId;
      personId = DatabaseComponent.InsertPerson(FirstName, LastName, DateOfBirth);
      return personId;
  }
  </code></pre>
  
  <p>Thanks for any help! Some useful URLs on good class design would also be appreciated.</p>