Common Reusable Business Entities Part 1

I noticed that several of my projects both for work and hobby tend to reuse certain common business entities. A Name structure is pretty much always the same (A name is a name is a name… maybe that’s a horse – nevermind the sidetrack.) In most cases, these entities were needed without change or very little change and therefore made for a great base entity to build off of.

Here are a few common entities that you may like to begin building a library with. You never know, it may save some time and build more consistency throughout several of your applications.

PersonName Structure: What’s in a Name?

The PersonName structure specifies a person’s name, including first, middle, and last as well as common prefix and suffix values. It has built in formatting options that you may like to expand on. Overall, all of the entities to be demonstrated in this short series are meant to be thin, without a lot of functionality or business logic.

public struct PersonName
{
    #region "ENUMERATIONS"
    /// <summary>
    /// The prefix to a persons name.
    /// </summary>
    public enum PrefixType
    {
        [Description("~")]
        NONE,
        [Description("Dr.")]
        DR,
        [Description("Father")]
        FATHER,
        [Description("Miss")]
        MISS,
        [Description("Mr.")]
        MR,
        [Description("Mrs.")]
        MRS,
        [Description("Ms.")]
        MS,
        [Description("Prof.")]
        PROF,
        [Description("Rev.")]
        REV
    }

    /// <summary>
    /// The suffix to a persons name.
    /// </summary>
    public enum SuffixType
    {
        [Description("~")]
        NONE,
        [Description("I")]
        I,
        [Description("II")]
        II,
        [Description("III")]
        III,
        [Description("IV")]
        IV,
        [Description("Jr.")]
        JR,
        [Description("Sr.")]
        SR,
        [Description("1st")]
        FIRST,
        [Description("2nd")]
        SECOND,
        [Description("3rd")]
        THIRD,
        [Description("4th")]
        FOURTH,
        [Description("D.D.S.")]
        DDS,
        [Description("M.D.")]
        MD,
        [Description("Ph.D.")]
        PHD
    }

    /// <summary>
    /// The display format for a persons name.
    /// </summary>
    public enum DisplayMode
    {
        //TODO: add any additional formats needed
        LastFirstMiddle,
        LastFirstMiddleInit,
        LastFirst,
        FirstMiddleLast,
        FirstMiddleInitLast,
        FirstLast
    }
    #endregion

    #region "PROPERTIES"
    /// <summary>
    /// Persons first name.
    /// </summary>
    public string First { get; set; }

    /// <summary>
    /// Persons middle name.
    /// </summary>
    public string Middle { get; set; }

    /// <summary>
    /// Persons last name.
    /// </summary>
    public string Last { get; set; }

    private string _MiddleInit;
    /// <summary>
    /// Persons middle initial.
    /// </summary>
    public string MiddleInit
    {
        get
        {
            if (string.IsNullOrEmpty(this._MiddleInit) == true)
            {
                if (string.IsNullOrEmpty(this.Middle) == false)
                {
                    return this.Middle.Substring(0, 1);
                }
                else
                {
                    return string.Empty;
                }
            }
            else
            {
                return this._MiddleInit;
            }
        }
        set
        {
            _MiddleInit = value.Substring(0, 1);
        }
    }


    /// <summary>
    /// Persons name prefix (Mr., Mrs., Ms., etc.)
    /// </summary>
    public PrefixType? Prefix { get; set; }

    /// <summary>
    /// Persons name suffix (Jr., Sr., etc.)
    /// </summary>
    public SuffixType? Suffix { get; set; }

    #endregion

    #region "CONSTRUCTORS"

    /// <summary>
    /// PersonName constructor.
    /// </summary>
    public PersonName(string first, string last)
        : this()
    {
        First = first;
        Last = last;
    }

    public PersonName(PrefixType? prefix, string first, string middle, string last, SuffixType? suffix)
        : this()
    {
        Prefix = prefix;
        First = first;
        Middle = middle;
        Last = last;
        Suffix = suffix;
    }

    #endregion

    #region "METHODS"
    /// <summary>
    /// Overrides ToString() to display default name format (Last, First Middle).
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
        return String.Format("{0}, {1} {2}", this.Last, this.First, this.Middle);
    }

    /// <summary>
    /// Additional ToString() to display name by specified format.
    /// </summary>
    /// <param name="dm"></param>
    /// <returns></returns>
    public string ToString(DisplayMode dm)
    {
        //TODO: add all the rest of the formats needed here
        switch (dm)
        {
            case DisplayMode.LastFirstMiddleInit:
                return string.Format("{0}, {1} {2}", this.Last, this.First, this.MiddleInit);
                break;
            case DisplayMode.LastFirst:
                return string.Format("{0}, {1}", this.Last, this.First);
                break;
            case DisplayMode.FirstMiddleLast:
                return string.Format("{0} {1} {2}", this.First, this.Middle, this.Last);
                break;
            case DisplayMode.FirstMiddleInitLast:
                return string.Format("{0} {1} {2}", this.First, this.MiddleInit, this.Last);
                break;
            case DisplayMode.FirstLast:
                return string.Format("{0} {1}", this.First, this.Last);
                break;
            default:
                return ToString();
                break;
        }
    }

    #endregion
}

What’s Next?

I’ll cover an Address structure in the next part, so feel free to come back and check it out. Also, please feel free to add any requests for specific common entities or add your own in the comments below.

One thought on “Common Reusable Business Entities Part 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s