Skip to content

Instantly share code, notes, and snippets.

@RealDotNetDave
Last active August 27, 2019 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RealDotNetDave/d635ce8eab4b20ac1b8a4cededb0fb46 to your computer and use it in GitHub Desktop.
Save RealDotNetDave/d635ce8eab4b20ac1b8a4cededb0fb46 to your computer and use it in GitHub Desktop.
Improve Your Model Classes with OOP
// ***********************************************************************
// Assembly : dotNetTips.OOP.Design
// Author : David McCarter
// Created : 07-24-2019
//
// Last Modified By : David McCarter
// Last Modified On : 08-18-2019
// ***********************************************************************
// <copyright file="Person.cs" company="dotNetTips.OOP.Design">
// Copyright (c) McCarter Consulting. All rights reserved.
// </copyright>
// <summary>Person OOP Design for Article 2</summary>
// ***********************************************************************
using System;
using System.ComponentModel;
using System.Diagnostics;
namespace dotNetTips.OOP.Design.Models.Article2
{
/// <summary>
/// Person class for my Improve Your Model Classes with OOP articles.
/// Implements the <see cref="System.IEquatable{dotNetTips.OOP.Design.Models.Article2.Person}" />
/// Implements the <see cref="System.IComparable" />
/// </summary>
/// <seealso cref="System.IEquatable{dotNetTips.OOP.Design.Models.Article2.Person}" />
/// <seealso cref="System.IComparable" />
[DebuggerDisplay("{Email}")]
public class Person : IComparable, IComparable<Person>
{
/// <summary>
/// The address1
/// </summary>
private string _address1;
/// <summary>
/// The address2
/// </summary>
private string _address2;
/// <summary>
/// The born on
/// </summary>
private DateTimeOffset _bornOn;
/// <summary>
/// The cell phone number
/// </summary>
private string _cellPhone;
/// <summary>
/// The city
/// </summary>
private string _city;
/// <summary>
/// The country
/// </summary>
private string _country = "USA";
/// <summary>
/// The email
/// </summary>
private string _email;
/// <summary>
/// The first name
/// </summary>
private string _firstName;
/// <summary>
/// The home phone number
/// </summary>
private string _homePhone;
/// <summary>
/// The unique identifier
/// </summary>
private string _id;
/// <summary>
/// The last name
/// </summary>
private string _lastName;
/// <summary>
/// The postal code
/// </summary>
private string _postalCode;
/// <summary>
/// Initializes a new instance of the
/// <see cref="T:Article2.Person" /> class.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Person()
{
}
/// <summary>
/// Initializes a new instance of the
/// <see cref="T:Article2.Person" /> class.
/// </summary>
/// <param name="id">The unique identifier.</param>
/// <param name="email">The email address.</param>
public Person(string id, string email)
{
Email = email;
Id = id;
}
/// <summary>
/// Gets or sets the Address1.
/// </summary>
/// <value>The Address1.</value>
/// <exception cref="ArgumentNullException">Address1 - Value for address cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Address1 - Address must be between 10 - 256 characters.</exception>
public string Address1
{
get
{
return this._address1;
}
set
{
if (this._address1 == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(Address1), "Value for address cannot be null or empty.");
}
this._address1 = (value.Length < 10 || value.Length > 256) ? throw new ArgumentOutOfRangeException(nameof(Address1), "Address must be between 10 - 256 characters.") : value;
}
}
/// <summary>
/// Gets or sets the Address2.
/// </summary>
/// <value>The Address2.</value>
/// <exception cref="ArgumentNullException">Address2 - Value for address cannot be null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Address1 - Address cannot be more than 256 characters.</exception>
public string Address2
{
get
{
return this._address2;
}
set
{
if (this._address2 == value)
{
return;
}
if (value == null)
{
throw new ArgumentNullException(nameof(Address2), "Value for address cannot be null.");
}
this._address2 = (value.Length > 256) ? throw new ArgumentOutOfRangeException(nameof(Address1), "Address cannot be more than 256 characters.") : value;
}
}
/// <summary>
/// Gets or sets the born on date.
/// </summary>
/// <value>The born on date.</value>
/// <exception cref="ArgumentOutOfRangeException">BornOn - Person cannot be born in the future.</exception>
public DateTimeOffset BornOn
{
get
{
return this._bornOn;
}
set
{
if (this._bornOn == value)
{
return;
}
this._bornOn = value.ToUniversalTime() > DateTimeOffset.UtcNow ? throw new ArgumentOutOfRangeException(nameof(BornOn), "Person cannot be born in the future.") : value;
}
}
/// <summary>
/// Gets or sets the cell phone number.
/// </summary>
/// <value>The cell phone number.</value>
/// <exception cref="ArgumentNullException">CellPhone - Value for phone number cannot be null.</exception>
/// <exception cref="ArgumentOutOfRangeException">CellPhone - Phone number is limited to 50 characters.</exception>
public string CellPhone
{
get
{
return this._cellPhone;
}
set
{
if (this._cellPhone == value)
{
return;
}
if (value == null)
{
throw new ArgumentNullException(nameof(CellPhone), "Value for phone number cannot be null.");
}
this._cellPhone = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(CellPhone), "Phone number is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the city.
/// </summary>
/// <value>The city name.</value>
/// <exception cref="ArgumentNullException">City - Value for City cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">City - City length is limited to 100 characters.</exception>
public string City
{
get
{
return this._city;
}
set
{
if (this._city == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(City), "Value for City cannot be null or empty.");
}
this._city = value.Length > 100 ? throw new ArgumentOutOfRangeException(nameof(City), "City length is limited to 100 characters.") : value;
}
}
/// <summary>
/// Gets or sets the country.
/// </summary>
/// <value>The country name.</value>
/// <exception cref="ArgumentNullException">Country - Value for Country cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Country - Country length is limited to 50 characters.</exception>
public string Country
{
get
{
return this._country;
}
set
{
if (this._country == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(Country), "Value for Country cannot be null or empty.");
}
this._country = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(Country), "Country length is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the email address.
/// </summary>
/// <value>The email address.</value>
/// <exception cref="ArgumentNullException">Email - Value for Email cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Email - Email length is limited to 75 characters.</exception>
public string Email
{
get
{
return this._email;
}
set
{
if (this._email == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(Email), "Value for Email cannot be null or empty.");
}
this._email = value.Length > 75 ? throw new ArgumentOutOfRangeException(nameof(Email), "Email length is limited to 75 characters.") : value;
}
}
/// <summary>
/// Gets or sets the first name.
/// </summary>
/// <value>The first name.</value>
/// <exception cref="ArgumentNullException">FirstName - Value for name cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Email - First name length is limited to 50 characters.</exception>
public string FirstName
{
get
{
return this._firstName;
}
set
{
if (this._firstName == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(FirstName), "Value for name cannot be null or empty.");
}
this._firstName = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(Email), "First name length is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the home phone number.
/// </summary>
/// <value>The home phone.</value>
/// <exception cref="ArgumentNullException">HomePhone - Value for phone number cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">HomePhone - Home phone length is limited to 50 characters.</exception>
public string HomePhone
{
get
{
return this._homePhone;
}
set
{
if (this._homePhone == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(HomePhone), "Value for phone number cannot be null or empty.");
}
this._homePhone = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(this.HomePhone), "Home phone length is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the unique identifier.
/// </summary>
/// <value>The unique identifier.</value>
/// <exception cref="ArgumentNullException">Id - Value for Id cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Id - Id length is limited to 256 characters.</exception>
public string Id
{
get
{
return this._id;
}
set
{
if (this._id == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(Id), "Value for Id cannot be null or empty.");
}
this._id = value.Length > 256 ? throw new ArgumentOutOfRangeException(nameof(this.Id), "Id length is limited to 256 characters.") : value;
}
}
/// <summary>
/// Gets or sets the last name.
/// </summary>
/// <value>The last name.</value>
/// <exception cref="ArgumentNullException">LastName - Value for name cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">LastName - Last name length is limited to 50 characters.</exception>
public string LastName
{
get
{
return this._lastName;
}
set
{
if (this._lastName == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(LastName), "Value for name cannot be null or empty.");
}
this._lastName = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(this.LastName), "Last name length is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the postal code.
/// </summary>
/// <value>The postal code.</value>
/// <exception cref="ArgumentNullException">PostalCode - Value for postal code cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">PostalCode - Postal code length is limited to 20 characters.</exception>
public string PostalCode
{
get
{
return this._postalCode;
}
set
{
if (this._postalCode == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(PostalCode), "Value for postal code cannot be null or empty.");
}
this._postalCode = value.Length > 20 ? throw new ArgumentOutOfRangeException(nameof(this.PostalCode), "Postal code length is limited to 20 characters.") : value;
}
}
/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
/// </summary>
/// <param name="obj">An object to compare with this instance.</param>
/// <returns>A value that indicates the relative order of the objects being compared. The return value has these meanings:
/// Value
/// Meaning
/// Less than zero
/// This instance precedes <paramref name="obj">obj</paramref> in the sort order.
/// Zero
/// This instance occurs in the same position in the sort order as <paramref name="obj">obj</paramref>.
/// Greater than zero
/// This instance follows <paramref name="obj">obj</paramref> in the sort order.</returns>
/// <exception cref="ArgumentException">obj</exception>
public int CompareTo(object obj)
{
if (obj == null)
{
return 1;
}
Person other = obj as Person;
if (other == null)
{
throw new ArgumentException(nameof(obj) + " is not a " + nameof(Person));
}
return CompareTo(other);
}
/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
/// </summary>
/// <param name="other">An object to compare with this instance.</param>
/// <returns>A value that indicates the relative order of the objects being compared. The return value has these meanings:
/// Value
/// Meaning
/// Less than zero
/// This instance precedes <paramref name="other">other</paramref> in the sort order.
/// Zero
/// This instance occurs in the same position in the sort order as <paramref name="other">other</paramref>.
/// Greater than zero
/// This instance follows <paramref name="other">other</paramref> in the sort order.</returns>
public int CompareTo(Person other)
{
if (other == null)
{
return 1;
}
int result = 0;
result = _address1.CompareTo(other._address1);
if (result != 0)
{
return result;
}
result = _address2.CompareTo(other._address2);
if (result != 0)
{
return result;
}
result = _bornOn.CompareTo(other._bornOn);
if (result != 0)
{
return result;
}
result = _cellPhone.CompareTo(other._cellPhone);
if (result != 0)
{
return result;
}
result = _city.CompareTo(other._city);
if (result != 0)
{
return result;
}
result = _country.CompareTo(other._country);
if (result != 0)
{
return result;
}
result = _email.CompareTo(other._email);
if (result != 0)
{
return result;
}
result = _firstName.CompareTo(other._firstName);
if (result != 0)
{
return result;
}
result = _homePhone.CompareTo(other._homePhone);
if (result != 0)
{
return result;
}
result = _id.CompareTo(other._id);
if (result != 0)
{
return result;
}
result = _lastName.CompareTo(other._lastName);
if (result != 0)
{
return result;
}
result = _postalCode.CompareTo(other._postalCode);
if (result != 0)
{
return result;
}
return result;
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
public override int GetHashCode()
{
return HashCode.Combine(Email, Id);
}
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
public override string ToString()
{
return $"{Id} - {Email}";
}
}
}
// ***********************************************************************
// Assembly : dotNetTips.OOP.Design
// Author : David McCarter
// Created : 07-24-2019
//
// Last Modified By : David McCarter
// Last Modified On : 08-27-2019
// ***********************************************************************
// <copyright file="Person.cs" company="dotNetTips.OOP.Design">
// Copyright (c) McCarter Consulting. All rights reserved.
// </copyright>
// <summary>Person OOP Design for Article 3</summary>
// ***********************************************************************
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace dotNetTips.OOP.Design.Models.Article3
{
/// <summary>
/// Person class for my Improve Your Model Classes with OOP articles.
/// Implements the <see cref="System.IComparable" />
/// Implements the <see cref="System.IComparable{dotNetTips.OOP.Design.Models.Article3.Person}" />
/// </summary>
/// <seealso cref="System.IComparable{dotNetTips.OOP.Design.Models.Article3.Person}" />
/// <seealso cref="System.IComparable" />
[DebuggerDisplay("{Email}")]
[DataContract(Name = "person")]
[Serializable]
[XmlRoot(ElementName = "Person")]
public class Person : IComparable, IComparable<Person>
{
/// <summary>
/// The address1
/// </summary>
[NonSerialized]
private string _address1;
/// <summary>
/// The address2
/// </summary>
[NonSerialized]
private string _address2;
/// <summary>
/// The born on
/// </summary>
[NonSerialized]
private DateTimeOffset _bornOn;
/// <summary>
/// The cell phone number
/// </summary>
[NonSerialized]
private string _cellPhone;
/// <summary>
/// The city
/// </summary>
[NonSerialized]
private string _city;
/// <summary>
/// The country
/// </summary>
[NonSerialized]
private string _country = "USA";
/// <summary>
/// The email
/// </summary>
[NonSerialized]
private string _email;
/// <summary>
/// The first name
/// </summary>
[NonSerialized]
private string _firstName;
/// <summary>
/// The home phone number
/// </summary>
[NonSerialized]
private string _homePhone;
/// <summary>
/// The unique identifier
/// </summary>
[NonSerialized]
private string _id;
/// <summary>
/// The last name
/// </summary>
[NonSerialized]
private string _lastName;
/// <summary>
/// The postal code
/// </summary>
[NonSerialized]
private string _postalCode;
/// <summary>
/// Initializes a new instance of the <see cref="Person"/> class.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Person()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Person"/> class.
/// </summary>
/// <param name="id">The unique identifier.</param>
/// <param name="email">The email address.</param>
public Person(string id, string email)
{
Email = email;
Id = id;
}
/// <summary>
/// Gets or sets the Address1.
/// </summary>
/// <value>The Address1.</value>
/// <exception cref="ArgumentNullException">Address1 - Value for address cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Address1 - Address must be between 10 - 256 characters.</exception>
[DataMember(Name = "address1")]
[XmlElement]
public string Address1
{
get
{
return this._address1;
}
set
{
if (this._address1 == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(Address1), "Value for address cannot be null or empty.");
}
this._address1 = (value.Length < 10 || value.Length > 256) ? throw new ArgumentOutOfRangeException(nameof(Address1), "Address must be between 10 - 256 characters.") : value;
}
}
/// <summary>
/// Gets or sets the Address2.
/// </summary>
/// <value>The Address2.</value>
/// <exception cref="ArgumentNullException">Address2 - Value for address cannot be null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Address1 - Address cannot be more than 256 characters.</exception>
[DataMember(Name = "address2")]
[XmlElement]
public string Address2
{
get
{
return this._address2;
}
set
{
if (this._address2 == value)
{
return;
}
if (value == null)
{
throw new ArgumentNullException(nameof(Address2), "Value for address cannot be null.");
}
this._address2 = (value.Length > 256) ? throw new ArgumentOutOfRangeException(nameof(Address1), "Address cannot be more than 256 characters.") : value;
}
}
/// <summary>
/// Gets or sets the born on date.
/// </summary>
/// <value>The born on date.</value>
/// <exception cref="ArgumentOutOfRangeException">BornOn - Person cannot be born in the future.</exception>
[DataMember(Name = "bornOn")]
[XmlIgnore]
public DateTimeOffset BornOn
{
get
{
return this._bornOn;
}
set
{
if (this._bornOn == value)
{
return;
}
this._bornOn = value.ToUniversalTime() > DateTimeOffset.UtcNow ? throw new ArgumentOutOfRangeException(nameof(BornOn), "Person cannot be born in the future.") : value;
}
}
/// <summary>
/// Gets or sets the born on for XML serialization.
/// </summary>
/// <value>The born on for XML.</value>
[EditorBrowsable(EditorBrowsableState.Never)]
[IgnoreDataMember]
[XmlElement("BornOn")]
public string BornOnForXml
{
get
{
return BornOn.ToString("o", CultureInfo.CurrentCulture);
}
set
{
BornOn = DateTimeOffset.Parse(value, CultureInfo.CurrentCulture);
}
}
/// <summary>
/// Gets or sets the cell phone number.
/// </summary>
/// <value>The cell phone number.</value>
/// <exception cref="ArgumentNullException">CellPhone - Value for phone number cannot be null.</exception>
/// <exception cref="ArgumentOutOfRangeException">CellPhone - Phone number is limited to 50 characters.</exception>
[DataMember(Name = "cellPhone")]
[XmlElement]
public string CellPhone
{
get
{
return this._cellPhone;
}
set
{
if (this._cellPhone == value)
{
return;
}
if (value == null)
{
throw new ArgumentNullException(nameof(CellPhone), "Value for phone number cannot be null.");
}
this._cellPhone = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(CellPhone), "Phone number is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the city.
/// </summary>
/// <value>The city name.</value>
/// <exception cref="ArgumentNullException">City - Value for City cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">City - City length is limited to 100 characters.</exception>
[DataMember(Name = "city")]
[XmlElement]
public string City
{
get
{
return this._city;
}
set
{
if (this._city == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(City), "Value for City cannot be null or empty.");
}
this._city = value.Length > 100 ? throw new ArgumentOutOfRangeException(nameof(City), "City length is limited to 100 characters.") : value;
}
}
/// <summary>
/// Gets or sets the country.
/// </summary>
/// <value>The country name.</value>
/// <exception cref="ArgumentNullException">Country - Value for Country cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Country - Country length is limited to 50 characters.</exception>
[DataMember(Name = "country")]
[XmlElement]
public string Country
{
get
{
return this._country;
}
set
{
if (this._country == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(Country), "Value for Country cannot be null or empty.");
}
this._country = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(Country), "Country length is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the email address.
/// </summary>
/// <value>The email address.</value>
/// <exception cref="ArgumentNullException">Email - Value for Email cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Email - Email length is limited to 75 characters.</exception>
[DataMember(Name = "email", IsRequired = true)]
[XmlElement(IsNullable = false)]
public string Email
{
get
{
return this._email;
}
set
{
if (this._email == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(Email), "Value for Email cannot be null or empty.");
}
this._email = value.Length > 75 ? throw new ArgumentOutOfRangeException(nameof(Email), "Email length is limited to 75 characters.") : value;
}
}
/// <summary>
/// Gets or sets the first name.
/// </summary>
/// <value>The first name.</value>
/// <exception cref="ArgumentNullException">FirstName - Value for name cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Email - First name length is limited to 50 characters.</exception>
[DataMember(Name = "firstName")]
[XmlElement]
public string FirstName
{
get
{
return this._firstName;
}
set
{
if (this._firstName == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(FirstName), "Value for name cannot be null or empty.");
}
this._firstName = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(Email), "First name length is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the home phone number.
/// </summary>
/// <value>The home phone.</value>
/// <exception cref="ArgumentNullException">HomePhone - Value for phone number cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">HomePhone - Home phone length is limited to 50 characters.</exception>
[DataMember(Name = "homePhone")]
[XmlElement]
public string HomePhone
{
get
{
return this._homePhone;
}
set
{
if (this._homePhone == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(HomePhone), "Value for phone number cannot be null or empty.");
}
this._homePhone = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(this.HomePhone), "Home phone length is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the unique identifier.
/// </summary>
/// <value>The unique identifier.</value>
/// <exception cref="ArgumentNullException">Id - Value for Id cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Id - Id length is limited to 256 characters.</exception>
[DataMember(Name = "id", IsRequired = true)]
[XmlElement(IsNullable = false)]
public string Id
{
get
{
return this._id;
}
set
{
if (this._id == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(Id), "Value for Id cannot be null or empty.");
}
this._id = value.Length > 256 ? throw new ArgumentOutOfRangeException(nameof(this.Id), "Id length is limited to 256 characters.") : value;
}
}
/// <summary>
/// Gets or sets the last name.
/// </summary>
/// <value>The last name.</value>
/// <exception cref="ArgumentNullException">LastName - Value for name cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">LastName - Last name length is limited to 50 characters.</exception>
[DataMember(Name = "lastName")]
[XmlElement]
public string LastName
{
get
{
return this._lastName;
}
set
{
if (this._lastName == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(LastName), "Value for name cannot be null or empty.");
}
this._lastName = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(this.LastName), "Last name length is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the postal code.
/// </summary>
/// <value>The postal code.</value>
/// <exception cref="ArgumentNullException">PostalCode - Value for postal code cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">PostalCode - Postal code length is limited to 20 characters.</exception>
[DataMember(Name = "postalCode")]
[XmlElement]
public string PostalCode
{
get
{
return this._postalCode;
}
set
{
if (this._postalCode == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(PostalCode), "Value for postal code cannot be null or empty.");
}
this._postalCode = value.Length > 20 ? throw new ArgumentOutOfRangeException(nameof(this.PostalCode), "Postal code length is limited to 20 characters.") : value;
}
}
/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
/// </summary>
/// <param name="obj">An object to compare with this instance.</param>
/// <returns>A value that indicates the relative order of the objects being compared. The return value has these meanings:
/// Value
/// Meaning
/// Less than zero
/// This instance precedes <paramref name="obj">obj</paramref> in the sort order.
/// Zero
/// This instance occurs in the same position in the sort order as <paramref name="obj">obj</paramref>.
/// Greater than zero
/// This instance follows <paramref name="obj">obj</paramref> in the sort order.</returns>
/// <exception cref="ArgumentException">obj</exception>
public int CompareTo(object obj)
{
if (obj == null)
{
return 1;
}
Person other = obj as Person;
if (other == null)
{
throw new ArgumentException(nameof(obj) + " is not a " + nameof(Person));
}
return CompareTo(other);
}
/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
/// </summary>
/// <param name="other">An object to compare with this instance.</param>
/// <returns>A value that indicates the relative order of the objects being compared. The return value has these meanings:
/// Value
/// Meaning
/// Less than zero
/// This instance precedes <paramref name="other">other</paramref> in the sort order.
/// Zero
/// This instance occurs in the same position in the sort order as <paramref name="other">other</paramref>.
/// Greater than zero
/// This instance follows <paramref name="other">other</paramref> in the sort order.</returns>
public int CompareTo(Person other)
{
if (other == null)
{
return 1;
}
int result = 0;
result = _address1.CompareTo(other._address1);
if (result != 0)
{
return result;
}
result = _address2.CompareTo(other._address2);
if (result != 0)
{
return result;
}
result = _bornOn.CompareTo(other._bornOn);
if (result != 0)
{
return result;
}
result = _cellPhone.CompareTo(other._cellPhone);
if (result != 0)
{
return result;
}
result = _city.CompareTo(other._city);
if (result != 0)
{
return result;
}
result = _country.CompareTo(other._country);
if (result != 0)
{
return result;
}
result = _email.CompareTo(other._email);
if (result != 0)
{
return result;
}
result = _firstName.CompareTo(other._firstName);
if (result != 0)
{
return result;
}
result = _homePhone.CompareTo(other._homePhone);
if (result != 0)
{
return result;
}
result = _id.CompareTo(other._id);
if (result != 0)
{
return result;
}
result = _lastName.CompareTo(other._lastName);
if (result != 0)
{
return result;
}
result = _postalCode.CompareTo(other._postalCode);
if (result != 0)
{
return result;
}
return result;
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
public override int GetHashCode()
{
return HashCode.Combine(Email, Id);
}
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
public override string ToString()
{
return $"{Id} - {Email}";
}
}
}
// ***********************************************************************
// Assembly : dotNetTips.OOP.Design
// Author : David McCarter
// Created : 07-24-2019
//
// Last Modified By : David McCarter
// Last Modified On : 08-18-2019
// ***********************************************************************
// <copyright file="Person.cs" company="dotNetTips.OOP.Design">
// Copyright (c) McCarter Consulting. All rights reserved.
// </copyright>
// <summary>Person OOP Design for Article 2</summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
namespace dotNetTips.OOP.Design.Models.Article2
{
/// <summary>
/// Person class for my Improve Your Model Classes with OOP articles.
/// Implements the <see cref="System.IEquatable{dotNetTips.OOP.Design.Models.Article2.Person}" />
/// Implements the <see cref="System.IComparable" />
/// </summary>
/// <seealso cref="System.IEquatable{dotNetTips.OOP.Design.Models.Article2.Person}" />
/// <seealso cref="System.IComparable" />
[DebuggerDisplay("{Email}")]
public class Person : IEquatable<Person>, IComparable, IComparable<Person>
{
/// <summary>
/// The address1
/// </summary>
private string _address1;
/// <summary>
/// The address2
/// </summary>
private string _address2;
/// <summary>
/// The born on
/// </summary>
private DateTimeOffset _bornOn;
/// <summary>
/// The cell phone number
/// </summary>
private string _cellPhone;
/// <summary>
/// The city
/// </summary>
private string _city;
/// <summary>
/// The country
/// </summary>
private string _country = "USA";
/// <summary>
/// The email
/// </summary>
private string _email;
/// <summary>
/// The first name
/// </summary>
private string _firstName;
/// <summary>
/// The home phone number
/// </summary>
private string _homePhone;
/// <summary>
/// The unique identifier
/// </summary>
private string _id;
/// <summary>
/// The last name
/// </summary>
private string _lastName;
/// <summary>
/// The postal code
/// </summary>
private string _postalCode;
/// <summary>
/// Initializes a new instance of the
/// <see cref="T:Article2.Person" /> class.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Person()
{
}
/// <summary>
/// Initializes a new instance of the
/// <see cref="T:Article2.Person" /> class.
/// </summary>
/// <param name="id">The unique identifier.</param>
/// <param name="email">The email address.</param>
public Person(string id, string email)
{
Email = email;
Id = id;
}
/// <summary>
/// Gets or sets the Address1.
/// </summary>
/// <value>The Address1.</value>
/// <exception cref="ArgumentNullException">Address1 - Value for address cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Address1 - Address must be between 10 - 256 characters.</exception>
public string Address1
{
get
{
return this._address1;
}
set
{
if (this._address1 == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(Address1), "Value for address cannot be null or empty.");
}
this._address1 = (value.Length < 10 || value.Length > 256) ? throw new ArgumentOutOfRangeException(nameof(Address1), "Address must be between 10 - 256 characters.") : value;
}
}
/// <summary>
/// Gets or sets the Address2.
/// </summary>
/// <value>The Address2.</value>
/// <exception cref="ArgumentNullException">Address2 - Value for address cannot be null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Address1 - Address cannot be more than 256 characters.</exception>
public string Address2
{
get
{
return this._address2;
}
set
{
if (this._address2 == value)
{
return;
}
if (value == null)
{
throw new ArgumentNullException(nameof(Address2), "Value for address cannot be null.");
}
this._address2 = (value.Length > 256) ? throw new ArgumentOutOfRangeException(nameof(Address1), "Address cannot be more than 256 characters.") : value;
}
}
/// <summary>
/// Gets or sets the born on date.
/// </summary>
/// <value>The born on date.</value>
/// <exception cref="ArgumentOutOfRangeException">BornOn - Person cannot be born in the future.</exception>
public DateTimeOffset BornOn
{
get
{
return this._bornOn;
}
set
{
if (this._bornOn == value)
{
return;
}
this._bornOn = value.ToUniversalTime() > DateTimeOffset.UtcNow ? throw new ArgumentOutOfRangeException(nameof(BornOn), "Person cannot be born in the future.") : value;
}
}
/// <summary>
/// Gets or sets the cell phone number.
/// </summary>
/// <value>The cell phone number.</value>
/// <exception cref="ArgumentNullException">CellPhone - Value for phone number cannot be null.</exception>
/// <exception cref="ArgumentOutOfRangeException">CellPhone - Phone number is limited to 50 characters.</exception>
public string CellPhone
{
get
{
return this._cellPhone;
}
set
{
if (this._cellPhone == value)
{
return;
}
if (value == null)
{
throw new ArgumentNullException(nameof(CellPhone), "Value for phone number cannot be null.");
}
this._cellPhone = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(CellPhone), "Phone number is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the city.
/// </summary>
/// <value>The city name.</value>
/// <exception cref="ArgumentNullException">City - Value for City cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">City - City length is limited to 100 characters.</exception>
public string City
{
get
{
return this._city;
}
set
{
if (this._city == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(City), "Value for City cannot be null or empty.");
}
this._city = value.Length > 100 ? throw new ArgumentOutOfRangeException(nameof(City), "City length is limited to 100 characters.") : value;
}
}
/// <summary>
/// Gets or sets the country.
/// </summary>
/// <value>The country name.</value>
/// <exception cref="ArgumentNullException">Country - Value for Country cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Country - Country length is limited to 50 characters.</exception>
public string Country
{
get
{
return this._country;
}
set
{
if (this._country == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(Country), "Value for Country cannot be null or empty.");
}
this._country = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(Country), "Country length is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the email address.
/// </summary>
/// <value>The email address.</value>
/// <exception cref="ArgumentNullException">Email - Value for Email cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Email - Email length is limited to 75 characters.</exception>
public string Email
{
get
{
return this._email;
}
set
{
if (this._email == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(Email), "Value for Email cannot be null or empty.");
}
this._email = value.Length > 75 ? throw new ArgumentOutOfRangeException(nameof(Email), "Email length is limited to 75 characters.") : value;
}
}
/// <summary>
/// Gets or sets the first name.
/// </summary>
/// <value>The first name.</value>
/// <exception cref="ArgumentNullException">FirstName - Value for name cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Email - First name length is limited to 50 characters.</exception>
public string FirstName
{
get
{
return this._firstName;
}
set
{
if (this._firstName == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(FirstName), "Value for name cannot be null or empty.");
}
this._firstName = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(Email), "First name length is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the home phone number.
/// </summary>
/// <value>The home phone.</value>
/// <exception cref="ArgumentNullException">HomePhone - Value for phone number cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">HomePhone - Home phone length is limited to 50 characters.</exception>
public string HomePhone
{
get
{
return this._homePhone;
}
set
{
if (this._homePhone == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(HomePhone), "Value for phone number cannot be null or empty.");
}
this._homePhone = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(this.HomePhone), "Home phone length is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the unique identifier.
/// </summary>
/// <value>The unique identifier.</value>
/// <exception cref="ArgumentNullException">Id - Value for Id cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Id - Id length is limited to 256 characters.</exception>
public string Id
{
get
{
return this._id;
}
set
{
if (this._id == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(Id), "Value for Id cannot be null or empty.");
}
this._id = value.Length > 256 ? throw new ArgumentOutOfRangeException(nameof(this.Id), "Id length is limited to 256 characters.") : value;
}
}
/// <summary>
/// Gets or sets the last name.
/// </summary>
/// <value>The last name.</value>
/// <exception cref="ArgumentNullException">LastName - Value for name cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">LastName - Last name length is limited to 50 characters.</exception>
public string LastName
{
get
{
return this._lastName;
}
set
{
if (this._lastName == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(LastName), "Value for name cannot be null or empty.");
}
this._lastName = value.Length > 50 ? throw new ArgumentOutOfRangeException(nameof(this.LastName), "Last name length is limited to 50 characters.") : value;
}
}
/// <summary>
/// Gets or sets the postal code.
/// </summary>
/// <value>The postal code.</value>
/// <exception cref="ArgumentNullException">PostalCode - Value for postal code cannot be null or empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">PostalCode - Postal code length is limited to 20 characters.</exception>
public string PostalCode
{
get
{
return this._postalCode;
}
set
{
if (this._postalCode == value)
{
return;
}
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(nameof(PostalCode), "Value for postal code cannot be null or empty.");
}
this._postalCode = value.Length > 20 ? throw new ArgumentOutOfRangeException(nameof(this.PostalCode), "Postal code length is limited to 20 characters.") : value;
}
}
/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
/// </summary>
/// <param name="obj">An object to compare with this instance.</param>
/// <returns>A value that indicates the relative order of the objects being compared. The return value has these meanings:
/// Value
/// Meaning
/// Less than zero
/// This instance precedes <paramref name="obj">obj</paramref> in the sort order.
/// Zero
/// This instance occurs in the same position in the sort order as <paramref name="obj">obj</paramref>.
/// Greater than zero
/// This instance follows <paramref name="obj">obj</paramref> in the sort order.</returns>
/// <exception cref="ArgumentException">obj</exception>
public int CompareTo(object obj)
{
if (obj == null)
{
return 1;
}
Person other = obj as Person;
if (other == null)
{
throw new ArgumentException(nameof(obj) + " is not a " + nameof(Person));
}
return CompareTo(other);
}
/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
/// </summary>
/// <param name="other">An object to compare with this instance.</param>
/// <returns>A value that indicates the relative order of the objects being compared. The return value has these meanings:
/// Value
/// Meaning
/// Less than zero
/// This instance precedes <paramref name="other">other</paramref> in the sort order.
/// Zero
/// This instance occurs in the same position in the sort order as <paramref name="other">other</paramref>.
/// Greater than zero
/// This instance follows <paramref name="other">other</paramref> in the sort order.</returns>
public int CompareTo(Person other)
{
if (other == null)
{
return 1;
}
int result = 0;
result = _address1.CompareTo(other._address1);
if (result != 0)
{
return result;
}
result = _address2.CompareTo(other._address2);
if (result != 0)
{
return result;
}
result = _bornOn.CompareTo(other._bornOn);
if (result != 0)
{
return result;
}
result = _cellPhone.CompareTo(other._cellPhone);
if (result != 0)
{
return result;
}
result = _city.CompareTo(other._city);
if (result != 0)
{
return result;
}
result = _country.CompareTo(other._country);
if (result != 0)
{
return result;
}
result = _email.CompareTo(other._email);
if (result != 0)
{
return result;
}
result = _firstName.CompareTo(other._firstName);
if (result != 0)
{
return result;
}
result = _homePhone.CompareTo(other._homePhone);
if (result != 0)
{
return result;
}
result = _id.CompareTo(other._id);
if (result != 0)
{
return result;
}
result = _lastName.CompareTo(other._lastName);
if (result != 0)
{
return result;
}
result = _postalCode.CompareTo(other._postalCode);
if (result != 0)
{
return result;
}
return result;
}
/// <summary>
/// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
/// </summary>
/// <param name="obj">The object to compare with the current object.</param>
/// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
public override bool Equals(object obj)
{
return Equals(obj as Person);
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.</returns>
public bool Equals(Person other)
{
return other != null &&
Email == other.Email &&
Id == other.Id;
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
public override int GetHashCode()
{
return HashCode.Combine(Email, Id);
}
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
public override string ToString()
{
return $"{Id} - {Email}";
}
/// <summary>
/// Implements the == operator.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(Person left, Person right)
{
return EqualityComparer<Person>.Default.Equals(left, right);
}
/// <summary>
/// Implements the != operator.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(Person left, Person right)
{
return !(left == right);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment