tags:

views:

103

answers:

3

So I have an application thats main purpose is to manage customers. My issue is that I'm not sure how to tie everything related to the customer together? For the sake of this post, let's pretend that a customer can have an unlimited number of emails. Below is what I was envisioning:

    class Customer {
            private int id;
            private String name;
            private List<Email> emails = new List<Email>();

            public Customer(id, name) {
                        this.id = id;
                        this.name = name;
            }

            public addEmail(Email email) {
                        emails.Add(email);
            }

            public getEmails() {
                        return emails;
            }
    }

    class Email {
            string email;

            public Email(email) {
                        this.email = email;
            }
    }

Customer newCustomer = new Customer(123, "Dummy Customer");
newCustomer.addEmail(new Email("[email protected]"));

I'm aiming towards this design because this way, say I need to make customers associated to a company representatives I could simply add another list member. Also, I tried Googling but I'm not really sure what this problem is called.

Here are some of the things I am unsure of:

  • How rock solid is this design?
  • As I add new entities to the customer class, wouldn't it get a bit large in terms of what it's responsible for?

Thank you.

A: 

Your design looks fine.

If customers need multiple email addresses, than making the Customer class contain a list of Email-objects makes complete sense.

The only issue is that it does not enforce uniqueness, in that multiple customers should not share a single email address. (or maybe they could? Can Bob, Mike and Susan all have the email [email protected]??)

abelenky
The uniqueness of emails is handled by the submit form before entering the database.
lengtche
A: 

I'd agree with ablensky. More information about how you're going to develop this class and the problems you are encountering would help to get a more concrete answer.

FYI: The OO feature you are using is composition if that is any help.

mdresser
A: 

Your naming is questionable. Is "Email" actually an email address, or is it the text of an email message? From your usage, it is obviously an address, so you might want to right-click->refactor->rename that.

Why do you bother with an "add emails" method or a "get emails" method? Just expose a public getter property.

Here's how I'd probably do it.

class Customer
{
    int id_;
    string name_;
    public IList<EmailAddress> EmailAddresses{get; private set;}

    public Customer(int id, string name)
    {
        id_ = id;
        name_ = name;
        EmailAddresses = new List<EmailAddress>();
    }
}

var newCustomer = new Customer(123, "Dummy Customer");
newCustomer.EmailAddresses.Add(new EmailAddress("[email protected]"));
dss539
btw, if all you're ever storing in an "EmailAddress" object is a string, just use the string class. Don't create a new type simply to hold a string. Do you anticipate that people will misuse the data if it's not explicitly an 'Email'? Be sure to name your variable/properties clearly and that should not happen.
dss539
I only included the email address for the sake of the question. There are 3-4 other details included with each email address.
lengtche