lundi 21 décembre 2015

How to design this function efficiently?

I have the below UpdateCustomer function. The problem I'm running into is that I want ValidCustomer function to work by itself and not require existing customer object. In other words it should be calling the database to get the existing customer and do all the complex business rules checking. If I do that I will be calling the database multiple times since CustomerDataChanged function also requires existing customer object. What is the best way to design this function? Is there a design pattern I could follow to refactor this?

public string UpdateCustomer(Customer CustomerToUpdate)
{

  Customer ExistingCustomer = GetExistingCustomerFromDataBase(CustomerToUpdate.ID);

  string ErrorMessage = ValidCustomer(CustomerToUpdate, ExistingCustomer);

    if (string.IsNullOrEmpty(ErrorMessage)) {
       bool DataChanged = CustomerDataChanged(CustomerToUpdate, ExistingCustomer);

       if (DataChanged) {
          UpdateCustomer(CustomerToUpdate);
       }
    }
  return ErrorMessage;

 }

Aucun commentaire:

Enregistrer un commentaire