views:

41

answers:

4

Hi

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 selected 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

A: 

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.

Paddy
do u mean checking in the presentation layer?
@user98454 - really you should have a business logic layer between the presentation and data layers that can handle this kind of thing.
ck
It should be worth nothing that this is not foolproof, and depends on volumes in your system, but it is just possible for someone to add an order between your validate and your delete call. Just something to bear in mind.
Paddy
A: 

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.

ck
It's not really an exception, it can occurs if the condition are normal http://en.wikipedia.org/wiki/Exception_handling
remi bourgarel
That's why it should be handled from a business layer, but if an **error** occurs in the data layer, this is a good way to propogate it.
ck
thanks for that ck
is there any good example implementing this type of exceptions
A: 

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.

The King
A: 

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 ....

remi bourgarel