-1

Suppose i have User entity. I want to check if user id exists on database. I know how to check, but in which layer ? What is the best and more understandable layer for that ?

In my opinion, it should be Controller layer. But i don't think it is gonna be smooth. I've never seen extra methods for checking in a User Controller class on sample projects. Maybe i'm missing something, what do you think ?

Xnart
  • 1
  • 1

2 Answers2

1

It should be on the Model side. Controller should just control say the flow from model to view.

It should be your service once you get the data from your repository, it should check whether you got the data or not and react accordingly.

SMA
  • 33,915
  • 6
  • 43
  • 65
  • Do you mean there is should be boolean method in DAO layer that makes all job and Service layer just returns that ? Or, DAO gets data, service layer checks if data exists and returns true or false ? – Xnart Jan 03 '16 at 09:35
  • It's the implementation detail how you want to deal with empty data. If you are expecting list of employee's and if you get empty list you could either return boolean true/false or throw an exception based on way of your implementation. – SMA Jan 03 '16 at 10:02
0

In my opinion, it depends on what you wish to do with the result. For example if you just want to check that the user exists then I would place a method on the DAO.

However if you was to then say I have a user, now I'm going to do something. I would suggest just loading the user and check for null instead. This is so you're not going to the DB twice. Not really an issue, if your caching is set up correctly. I have made an assumption that no caching has been enabled.

I would always suggest a layer between your Controller and Data Layer, just to provide Domain Specific Logic. Leaving your Controller free for routing and your Data Layer clean for just Data Access.