I have a three tier architecture API to add and remove customers. Every customer has some employees that are responsible for that customer. Those employees are located in another collection of documents (I am using a NoSQL db) So if I am adding or updating a customer the customers gets a list of employeeIds that reference those employees. When a customer is now submitted, I have to check all those ids in a validation method:

function CheckIfAllEmployeeIdsHaveExistingEmployee(List<string> employeeIds) {...}

Would you do this in the DAL or in the BL? I am currently doing everything in the BL and only very simple CRUD operations and views are done in the DAL. But this logic was also a "database logic" in my SQL-times.

What do you think?

  • How do you handle deletion of employees? – Rik D Aug 26 '19 at 8:40
  • If an employee is deleted, the customer reference will just be removed automatically. – David Mason Aug 26 '19 at 10:34
  • But each customer also has a collection of employeeIds. When deleting an employee, how are these collections updated? – Rik D Aug 26 '19 at 12:30
  • Validation is part of Business Logic. If you add it to DAL, and replace DAL with a different db provider, your validation logic will move with it and you'll have to write it again. – bobek Aug 29 '19 at 17:56

Checking if data provided for creating or updating an object complies to some rules or constraints is typically a BL layer responsibility. For this reason, checking if EmployeeId are valid should also be in the BL.

Putting it in the BLL gives you flexibility:

  • You can decide what to do if some Ids provided are valid and some are not (should you ignore the invalid ones? or should you return an error message?).
  • You could do the validity check of data when the UI needs it (instead of letting user enter wrong data and complain only at the end).
  • You could easily modify the rules, if one day you decide that the EmployeeId must correspond to an employee assigned to the geographic area of the customer.
  • You could change the DAL technology without having to reimplement your check.

On the other side, you could argue that this check could be done automatically using some referential integrity constraints in the DB that is checked during DAL operations only. This is a valid argument. But you have to accept the consequences, i.e. that the responsibilities between the layers are no longer so clear (part of the BL responsibility is delegated to the DB) and that you'll lose in flexibility.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.