0

In my final ASP.NET assignment, I am instructed to use a code first approach and add the following properties and Model(s) to represent the described changes.

1) A Person 'has a' Address object (This class was given, but I modified adding properties)

2) An Address object has a single property of type string for Email. (I created this class)

namespace ContosoUniversity.Models
{
    public class Address
    {
        [Required]
        [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]     {2,4}")]
        public string Email { get; set; }

        [Required]
        [Compare("Email")]
        public string EmailConfirm { get; set; }
    }
}

but now I am not sure what he means in the first instruction. I have researched composition, inheritance, and abstract classes but still don't know what I am suppose to do?

How am I suppose to create an Address object in the person class? What does that actually mean? Here is the Person class:

namespace ContosoUniversity.Models
{
    public abstract class Person
    {
        public int ID { get; set; }

        [Required]
        [StringLength(50)]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        [Required]
        [StringLength(50, ErrorMessage = "First name cannot be longer than 50     characters.")]
        [Column("FirstName")]
        [Display(Name = "First Name")]
        public string FirstMidName { get; set; }

        [Display(Name = "Full Name")]
        public string FullName
        {
            get
            {
                return LastName + ", " + FirstMidName;
            }
        }
    }
}
mason
  • 31,774
  • 10
  • 77
  • 121
Earl Switzer
  • 1
  • 1
  • 1
  • 3

1 Answers1

1

It means that there should be a one to one relationship between the person and an address. A Person has one Address.

namespace ContosoUniversity.Models
{
    public abstract class Person
    {
        public int ID { get; set; }

        public Address Address {get; set;}

        [Required]
        [StringLength(50)]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Required]
        [StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
        [Column("FirstName")]
        [Display(Name = "First Name")]
        public string FirstMidName { get; set; }

        [Display(Name = "Full Name")]
        public string FullName
        {
            get
            {
                return LastName + ", " + FirstMidName;
            }
        }
    }
}

Now you can do something like this (assuming Person wasn't an abstract class)...

Person person = new Person();
person.Address = new Address();
person.Address.Email = "john.doe@example.com";

If your teacher had said something like "a person can have multiple addresses" you could have done something like this (omitted duplicate lines for brevity):

public class Person
{
    public IEnumerable<Address> Addresses {get; set;}

    public Person()
    {
        Addresses = new List<Address>(); //initialize with an empty collection
    }
}

which would allow you to do this...

Person john = new Person();
Address home = new Address(){Email = "john.doe@example.com"}; //create a new address and set its Email property to a value in a single line
Address work = new Address(){Email = "johndoe@work.com"};
john.Addresses.Add(home); //Add the address to the Addresses collection of john
john.Addresses.Add(work);
mason
  • 31,774
  • 10
  • 77
  • 121