Question

My class inherits an interface so I need to have the emailaddress property in the Person class.

My question is what is the best way to get the property and set the property

public class Contact : IPerson, ILocation
{
  ...
  [MaxLength(256)]
  public string EmailAddress { 
  get{
    return this.Emails.First().ToString();
  } 
  set{ ????  }
  }
  ....

  public virtual ICollection<Emails> Emails { get; set; }
}

Essentially, I'm trying to get the class to allow more than one email.

For full disclosure, I'm pretty new to this and I may not be asking the correct questions, but I've searched for a day and half and haven't seen anything like this (not that I think it's unusual) and could use the insight.

Email class properties:

[Key]
public int Id { get; set; }

[MaxLength(256)]
    public string EmailAddress { get; set; }
Was it helpful?

Solution

Do you have control over the design of the IPerson interface that is forcing you to implement EmailAddress? If so, I would recommend rethinking the design to avoid needing both a singular property and a list of email addresses on the same object.

You may also want to consider making the Emails property setter protected, to prevent outside code from changing your object's data.

If you must implement to this interface, and you wish to have the EmailAddress property always refer to the first email in the collection, you can try this code.

public class Contact : IPerson, ILocation
{
  public Contact()
  {
    // Initialize the emails list
    this.Emails = new List<Emails>();
  }

  [MaxLength(256)]
  public string EmailAddress
  { 
    get
    {
      // You'll need to decide what to return if the first email has not been set.
      // Replace string.Empty with the appropriate value.
      return this.Emails.Count == 0 ? string.Empty : this.Emails[0].ToString();
    } 
    set
    {
      if (this.Emails.Count == 0)
      {
        this.Emails.Add(new Emails());
      }
      this.Emails[0].EmailAddress = value;
    }
  }

  public virtual IList<Emails> Emails { get; set; }
}

OTHER TIPS

EDIT: I didn't understand the question at first.

In this case, I'm not sure what you are trying to do makes sense. You are taking the first email out of a collection of emails on the getter; so what behavior do you want for the setter? It could (a) update or insert the set value into the first position in the collection or (b) just simply add the email to the collection when set. I'm not sure I would allow setting of that at all, just make a get and leave it at that.

I would also rename the property FirstEmail or something so it is obvious it is coming out of a collection of possible emails.

  public string FirstEmailAddress { 
      get{
         return this.Emails.Count > 0 ? this.Emails.First().ToString() : string.Empty;
      } 
  }

Usually, when you want to "show" an internal collection to the world, what you do is make it a property and create only a getter. This way you can not set the entire collection like Contact.Emails = new ICollection<Email>(), which is a bad practice. But you can access the collection through Contacts.Emails and iterate through it or add items, etc.

You can also add an indexer to this property if you like: http://msdn.microsoft.com/en-us/library/vstudio/6x16t2tx(v=vs.100).aspx

From your comment,

I have a one to many between Contacts and their emails (hence the collection). Because I'm inheriting an interface, I can't exclude the EmailAddress property (from the interface). So I'm not sure where to take it from here. Can I ignore the set and just do it through the relationship?

You should not allow public setter. Make it protected as you have marked virtual.

public virtual ICollection<Emails> Emails { get; protected set; }

For your EmailAddress setter you can use following

[MaxLength(256)]
public string EmailAddress 
{ 
    get
    {
        return this.Emails.DefaultIfEmpty(new EMail()).First().EmailAddress;
    } 
    set
    {
        if (Emails == null)
        {
            Emails = new Collection<Email>(); 
        }
        Emails.Items.Insert(0, new EMail {EmailAddress = value}); 
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top