1

I'm working on a .Net WebApi project in which I've got Api controllers making calls to a service layer.

Let's say I want to set up an end-point to delete a resource if it exists. If nothing is found, return an error message to that effect.

I need some clarification on where best to place the logic for checking for whether an item exists before a deletion is performed.

Specifically, do I perform the item exists check on the controller level or within the service layer?

For clarification, I could:

i. have two methods in the service layer - GetItem and DeleteItem and from the controller first call GetItem and if Item exists then call DeleteItem. If Item does not exist, I return an error.

ii. alternatively, I could include the GetItem check within the DeleteItem method in the service layer and return that back up as an error to the controller.

Which of the above is the best place to do this logic check?

Talha
  • 373
  • 3
  • 9
  • You are likely better off handling this in the service layer to deal with concurrent requests, but it depends on the semantics that are exposed by DeleteItem, and to an extent, the semantics you want to expose to callers or your API. In particular, what would you expect to happen if there were two racing requests to delete the item? Let's say that both get past the GetItem check before either calls DeleteItem. How would you expect execution to proceed for the two requests? Would you want both requests to report success? One succeeds and one fails? – DavidS May 15 '16 at 20:14

0 Answers0