I am wondering where should the DB queries need to be in the three tier architecture.
The first association that think is in the DAL. But then if the BL want to search for an object, the DAL need to publish methods for each possible search (Unrealistic - a lot of work).

The second option, is in the BL. But then the BL need to know how the DB designed... (Encapsulation is not good)

The third option, is that the DAL will publish some object like "QueryBuilder" or something similar, and the BL build the query with weak association (but still needed) to the DB.

What is the best option to allow BL to do a search?

  • Why would you need a method of each possible search combination anywhere in your application? – JeffO Dec 5 '13 at 21:35
  • I am not, but if i choose option 1 and create method of each search for the BL, its makes the DAL to know what algorithm exits in the BL, and that's not good. So theoretically the DAL should implements all combinations of search type... – nrofis Dec 5 '13 at 22:10

If your data access layer doesn't provide the methods to access data that you need, why would you have it at all?

The point of a DAL is not just to provide generic methods for accessing whatever data the caller wants. The database driver already provides those, and if you want to be independent of the particular database, you want a wrapper or adapter, but not an entire layer of architecture.

No, in my view the data access layer has exactly this job to do: to provide methods for those queries that you actually use in your application (rather than the near-infinite number of queries that SQL would theoretically allow).

  • The third option not good enough? That the BL will get an object to select what it want the the DAL will phrase that to SQL? Why the DAL needs to know what queries the BL needs? – nrofis Dec 5 '13 at 18:05
  • My point is that if your DAL is simply a query generator, then it will essentially consist of one class publishing one method. That may be a good design in your case, but I certainly wouldn't call it a "layer" - it would hardly show up on your architecture diagram. – Kilian Foth Dec 5 '13 at 18:41
  • My DAL handles DB and files, and know how to save and load objects from theme. I have problem only with the DB search. If the DAL publish method for each search of the BL, its doesn't destroy the separation, because DAL knows how the BL works? I agree that option 1 is the best in theory, but its impossible in real life. I'm a little confused. I think in real life option 3 is the best, because the BL ask for what it wants and the DAL phrase it to SQL query.. Isn't it? – nrofis Dec 5 '13 at 19:00

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.