SharkTooth
Programmer
We are learning how to do OOP and have a question that we have been debating. Hopefully you guys can clear this up for us.
I get customer data from the database and I want to populate the “Customer” object with the data from the database. I also want to use the “Customer” object to add customers to the database. What would be the best design? Should we have a method in the customer class that retrieves the data from the database or should we have some other class that loads the information into the object. Also should the property CountyCode (in the example below) actually validate itself by going to the database when it is set? I feel like there should be a better way.
This is what the class could look like:
Is this good design?
Thanks for your thoughts!
I get customer data from the database and I want to populate the “Customer” object with the data from the database. I also want to use the “Customer” object to add customers to the database. What would be the best design? Should we have a method in the customer class that retrieves the data from the database or should we have some other class that loads the information into the object. Also should the property CountyCode (in the example below) actually validate itself by going to the database when it is set? I feel like there should be a better way.
This is what the class could look like:
Code:
public class Customer:User, IAddress{
public Customer(string customerId){
LoadCustomer(customerId){
}
public string CountryCode{
get{return _countryCode;}
set{
if(ValidateCountryCode(value))
_countryCode = value;
}else{
throw(new Exception())
}
}
}
void LoadCustomer(string customerId){
// do database stuff
}
bool ValidateCountryCode(string countryCode){
// do database stuff to validate countrycode
}
...
}
Thanks for your thoughts!