0

I need some guidance on how to send error responses to client from WebAPI controller for an update operation. I need to check if data is changed and if it has duplicate data. I have service class that does database operations.

Below is psuedo code

Approach 1

CustomerController.cs

public Task<IActionResult> UpdateCustomer(int id, Customer request)
{
     var customer = service.GetCustomer(id);

     if(customer is null)
       return NotFound()

     var isChanged = service.IsChanged(request, customer);

     if(!isChanged)
       return BadRequest();


     var isDuplicate = service.IsDuplicate(request);

     if(!isDuplicate)
       return BadRequest(); //500 error

     await service.Update(request)
}

Approach 2

CustomerController.cs

public Task<IActionResult> UpdateCustomer(int id, Customer request)
   {
     var customer = service.GetCustomer(id);

     if(customer is null)
       return NotFound()

     await service.Update(request, customer);
}

CustomerService.cs

public void Update(Customer request, Customer customer)
{
  if(!HasChanged(request, customer))
    throw NoChangesException();

  if(IsDuplicate(request))
   throw DuplicateException();

   db.Update(request);
}

Catch exceptions in Global exception Middleware and send the BadRequest or Internal error based on exception type.

Edit

I need to add that duplicates and no changes will be rejected by data layer. Data layer is another common OData service. I feel its expensive to call the data service for known exceptions.

1 Answer 1

2

Approach 0

Controller

public Task UpdateCustomer(Customer request)
{
     await service.Update(request)
}

Service

public void Update(Customer request)
{
   db.Update(request);
}

If there has been no change, update anyway

If the customer already exists, update it

If the customer doesn't exist, add it.

If there is a db error, throw it and convert automatically to a 500 in a global error handler

10
  • I need to clarify that duplicates and no changes will be rejected by data layer. Its the functionality across common database service. So we know that exceptions occur if we submit the request.
    – Sunny
    Commented Jan 27 at 17:51
  • Also, Data layer is a OData web service and I feel it can get expensive to submit the update for known exceptions.
    – Sunny
    Commented Jan 27 at 18:13
  • 1
    1. im unclear on what you mean by duplicate here, you are updatign right? so the record must exist. Also I think odata v4 supports Upserts? so it should be fine with dupes if its the create thats causing issues.. But you use of OData behind another rest api seems questionable in of itself to me.
    – Ewan
    Commented Jan 28 at 10:41
  • 1
    2. re: performance. You would have to measure the performance of requesting the existing record first for every update request vs sending some update that will fail. Obviously is depends on the volumes of the different types of request and whether you can cache known dupes or something clever, but, I think you will find the "just try it and throw" approach faster. You can see without the request less data is going over the wire
    – Ewan
    Commented Jan 28 at 10:43
  • 1
    you should be able to do a patch request without retrieving the row first and you should be able to use a patch request to do an upsert : docs.oasis-open.org/odata/odata/v4.01/…
    – Ewan
    Commented Jan 28 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.