asp.net mvc - Exception Handling in webapi MVC.net -


i got api controller , service, when invoking 1 of action in controller must apply validation, validation need check data in db validate correct.

as far can see there 2 ways of handling this

1- validate before calling "update" prevent error

public iactionresult updatecustomer(customerdto customer) {    if (!customerservice.validate(customer))    {        return send400badrequest();    }    customerservice.update(customer);     return send200ok(); } 

2- call update validate inside , throw exception.

 public iactionresult updatecustomer(customerdto customer)  {     customerservice.update(customer);         return send200ok();  } 

in customer service

  public void update(customerdto customer)   {      if (!validate(customer)         throws new customvalidationexception("customer not valid");      //apply update   } 

we have actionfilter handle customvalidationexception return badrequest.

1) pros +don't use exception use running flow

cons -controller more fat, has more decision on each case decide output

2) pros +operation more atomic, logic inside service. +easier test +every use of method validated.

cons -use exception manage flow.

which better solution?

i need arguments defend 1 or other.

if have business logic layer , service layer, prefer keep business logic rules including business logic validations in business logic layer , use service layer wrapper around business logic layer , throw exception in business methods.

when deciding whether use exception business validation rules or not, can consider:

1) it's better business methods unit of work. should perform complete task. it's better contain validation rules. way can reuse such business logic layer across different service layers or use same unit of work in different methods of same service layer. if throw business validation exception in business logic layer, not face risk of forgetting validation or using validation rule mistake , each service method / action perform single task , lightweight possible.

think when may need expose wcf service clients or example if may use asp.net mvc without using webapi or iff want use same logic in action method in same webapi.

if put business logic validations in web api controller, when creating wcf service methods or creating mvc actions or other service methods, may forget apply validations or may apply wrong validations different rules in new service layer.

2) considering first benefit, can return meaningful value methods shows success, failure, or contain suitable information failure reason in output?

i believe it's not suitable use out put of method these goals. method output method output, should data in such business application. should not status, data or times message. throwing exception solve problem.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -