开发者

Business layer, data layer, where to put queries?

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 ques开发者_开发知识库tion 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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜