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 5937891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:36:23+00:00 2026-05-22T15:36:23+00:00

I am trying to work out the best way to design a class that

  • 0

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 Person. To create a new person and place it in the database, I want the DateOfBirth property to be optional (i.e. NULLable in the DB).

Here’s my sample code:

namespace BusinessLayer
{
    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DateOfBirth { get; set; }
    }
}

I’m unsure as to whether the fields should be public or not. Should I do it like this:

class Program
{
    static void Main(string[] args)
    {
        Person person1 = new Person("Kate","Middleton",null);
    }
}

or like this:

class Program
{
    static void Main(string[] args)
    {
        Person person1 = new Person();
        person1.FirstName = "Kate";
        person1.LastName = "Middleton";
    }
}

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?

So, would I do something like this:

public int Save()
{
    int personId;
    personId = DatabaseComponent.InsertPerson(FirstName, LastName, DateOfBirth);
    return personId;
}

Thanks for any help! Some useful URLs on good class design would also be appreciated.

  • 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-22T15:36:24+00:00Added an answer on May 22, 2026 at 3:36 pm

    First, I’d put two distinct public constructor to Person:

    namespace BusinessLayer
    {
        class Person
        {
            public Person(string firstName, string lastName): this(firstName, lastName, DateTime.Now)
            {}
    
            public Person(string firstName, string lastName, DateTime birthDate)
            {
                FirstName = firstName;
                LastName = lastName;
                DateOfBirth = birthDate;
            }
    
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public DateTime DateOfBirth { get; set; }
        }
    }
    

    this allows you to write both

    var p = new Person("Marilyin", "Manson");
    var p2 = new Person("Alice", "Cooper", new DateTime(...));
    

    and

    var p = new Person { FirstName="Marilyn", LastName="Manson" };
    

    I can’t see why you should limit to only one form.

    As for the DatabaseComponent I’d strongly suggest to write a method that allows you to save a Person instead of the signature you are implicitly declaring.

    That’s because, should one day change the way a Person is defined, you’d probably have to change the code in each point you invoke Save() method. By saving just a Person, you only have to change the Save() implementation.

    Don’t you plan to use an ORM by the way?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to work out the best way to upgrade jQuery. Let's say I
I am trying to work out the best way of doing this but not
I've been trying to work out the 'best practices' way to manage file uploads
I'm trying to work out the best way to assemble the navigation menu of
I am trying to work out the best way to stop double 'booking' in
I'm just trying to work out the best way to replicate a pretty useful
Im trying to work out the best way to approach the following problem with
I'm trying to work out the best way to setup our multi-module Apache Maven
I am trying to work out the best way of using a viewmodel in
I'm currently trying to work out the best way to store a business' hours

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.