开发者

error handling in asp.net

开发者 https://www.devze.com 2023-01-01 21:09 出处:网络
How can i pass the different types of errors from Data access layer to presentation layer? suppose if we take the northwind database

How can i pass the different types of errors from Data access layer to presentation layer?

suppose if we take the northwind database

scenario

I want to delete the customer, so i se开发者_Python百科lected one customer in ui and clicked the "delete" button.It internally calls the "delete" in data access layer.

The prerequisite for deleting the customer is that the customer doesn't have any orders.So in data access layer we wil check whether that customer has any orders.If the customer has orders how can we pass the message from dal to presentation layer that the customer has orders and we don't delete.

Am i doing right?is there any other ways to deal with this type?

Thanks in advance


The other answers tell you how you should be implementing this particular scenario, however to answer your original question, the answer is to define your own exceptions.

You can have a core DataLayerException as the base for all of you data exceptions (inheriting from ApplicationException or similar) then have sub exceptions based on the scenario, e.g.:

  • ConnectionClosedException
  • TImeoutException

etc.


For me personally, it would be better to call a separate "ValidateDeletion" method prior to attempting a delete. This would first check to see if that customer has orders before removing them from the database.


Particulary, if you want to raise different kinds of exception from database... I use to raise an error from SP like this.

if (@invalidCount <> 1)
Begin
    Raiserror('[Duplicate] Record Already Posted In System ', 20, 1)
End

Catch the error in the DAL, and analyse the exception type through the exception message (here the keyword for me is the "[Duplicate]") and throw the different kind of exception appropriately.

Of course this will be very cumboresum if you have more than 2/3 types of exceptions.


For me the best way is to raise an event TryToDeleteCustomerWithOrders.

The validation part is also fine, but it's about data, so the data layer should do the whole work. If you put the validation outside, there is chance that you call the deletion function without validation ....

0

精彩评论

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

关注公众号