lundi 18 janvier 2016

Is injecting dependencies in validation methods bad practice?

In the project I work on, most of the methods are structured like 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) {
            UpdateCustomerToDatabase(CustomerToUpdate);

        }
    }


    return ErrorMessage;
}

The validate customer method requires an existing customer to do some validation and also the DataChange method. I don't like the idea of injecting dependency to validate method. If I don't inject the dependency, I will be calling the database twice to get customer since data changed method also needs the existing customer object?

Is it a bad practice to inject dependencies like this? If so is there a cleaner solution to combat this problem? Other than switching to ddd pattern?

Aucun commentaire:

Enregistrer un commentaire