Question

I have a data access layer that has separate classes for each table in the database. Each class makes objects that reference a row in the table, they have create, update, delete, and fetch functions. They all extend a DBObj class. My question is, say I want to write a query to SELECT * FROM table, and have that in a function. Where is the best place to put this? In the business layer or group all related functions in their respective data layer classes? Its in PHP if that matters, using MySQL.

Was it helpful?

Solution

Put SQL queries in the data layer.

For a query like SELECT * FROM table, whether you make that a class method of the model itself, or a method of a model manager object, is a matter of style.

But business logic should be abstracted from the arbitrary details of the database schema. The business layer should be able to just ask the data layer for all instances of a model, without caring about the specific query that is going to be used to get that data.

OTHER TIPS

I've just found your question, and I thought about bringing another point of view. You should on most applications (won't say every, but it's close to it) build a data abstraction layer, most times defined as an ORM framework.

Careful though, the Data Access Layer(DAL) should not be confused with you Domain Layer or Business Logic Layer, depending on your architectural approach. If you want to get deeper into this subject, I think that every developer should, you can seek for Martin Fowler's articles, also you can read this blog post I did a while ago, not nearly as important as M. Fowler, but I made some reflections on the matter.

Best regards, David

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top