I have a repository pattern employed in my .NET Core 2.1 project. While implementing the edit/update scenario, I have a doubt.

My domain:

public class Component
    public int Id { get; set; }
    public string Description { get; set; }
    public decimal Amount { get; set; }
    public string Narration { get; set; }

Here, the repository will have a method:

public async Task<ComponentViewModel> Edit(ComponentEditDTO dto)
    var fcDb = await _context.FeeComponentCollection.FirstOrDefaultAsync(f => f.Id == dto.Id);

    if (fcDb == null)
         //what to do here?        

I have thought of two alternatives.

  1. Have a method in the repo that checks if the Id exists, then use this method to reject edit requests at the API level
  2. Let the repo handle edit case entirely. I can think of raising an exception (maybe RecordNotFoundException) here. Once the exception is raised, the API can then deal with the error by emitting BadRequest.

What is the standard procedure in such cases?

  • 3. Make it "idempotent". Like SQL update statements. It doesn't raises an error when no register is found/updated. Forget standards, rather ask yourself which one suits better my requirements? And, as usual, keep it simple. – Laiv Sep 5 '18 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.