Is having multiples properties chained togeather bad coding practice?
I've been designing some classes for my ASP.NET application and a couple of the classes have fields which are of other class types. When I was coding my save methods, I found that I was chaining quite a few properties togeather simply to get a value, for example in order to access the value for a street address I would have to type Customer.Contact.Address.StreetAddress and that seems rather convoluted so I'm wondering if this is normal or did I design my classes badly which are below.
Thanks
public abstract class Person { private string firstName; private string lastName; public Person(string firstName, string lastName) { this.firstName = firstName; this.lastName = lastName; } public string FirstName { get { return firstName; } set { firstName = value; } } public string LastName { get { return lastName; } set { lastName = value; } } }
Contact class is used in Customer to store contact info
public class Contact { private Address address; private Company company; private List<ContactDetail> contactDetails; public Contact(Address address, Company company) { this.address = address; this.company = company; this.contactDetails = new List<ContactDetail>(); } public Address Address { get { return address; } set { address = value; } } public Company Company { get { return company; } set { company = value; } } public List<ContactDetail> ContactDetails { get { return contactDetails; } } public void AddContactDetails(ContactDetail detail) { contactDetails.Add(detail); } }
Be as granular as you can be... this will lend to easier maintenance and scalability. There are ways around lengthy property traversing.
obb-taurus
ASKER
Can you give an example of what I might do to avoid the lengthy properties in this situation?
kaufmed
I personally prefer it--so long as the chain isn't too long--for readability, but that's not to say you cannot find ways to achieve the same level of readability by using local variables as well. I think "Customer.Contact.Address.StreetAddress" might be pushing it (length-wise) for my personal taste. I believe MS' recommendation is to use local variables rather than chaining. This can approach can be a bit easier to debug also.
One seemingly ironic thing, however, is that you often see method chaining used heavily in LINQ. It can be difficult, at times, to debug such chaining.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
obb-taurus
ASKER
Kaufmed, when you say to use local variables rather than chaining, do you simply mean something like the following?
public Address GetAddress(){ Address address = Contact.Address; return address;}
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
Just so I have an idea, what would the code for the method RedActStreet(r.street) look like in your example? Sorry about asking for so much detail but for some reason designing classes from the ground up seems to be a challenge for me and when I ran into this situation it threw me for a loop.
Thanks
kaufmed
Mind you this is all hypothetical code, but perhaps something along the lines of:
public string RedactStreet(string street){ string result = steet; if (street == "123 Main St.") { result = string.Empty; } return result;}