Home > Software design >  Wouldn't it be against the use of an optional object for validation?
Wouldn't it be against the use of an optional object for validation?

Time:06-28

I have a CRUD job.

Read -> Retrieves one object. Most have a value, but if an incorrect ID is entered, there may be no return value.

So we wrap it in an Optional object and return it.

Create -> Let's pass.

Update -> Update the object mentioned in Read.

Delete -> Deletes the object mentioned in Read.

Here, there is an ID that specifies the object to be updated or deleted (data row in DB).

The problem is I'm not sure if an object with this id exists in the DB or not.

I want to perform update or delete only when there is a value with ID in the DB.

The method I thought of at this time is to execute the first-mentioned Read method, receive an object wrapped in Optional, and pass if it is through orElseThrow().

If not, I used a method that raises a Throw exception.

But something seems wrong with this.

  1. Optional returned after inquiry is destroyed after only checking whether there is a value or not.

  2. But other than this, I can't think of a way to know if the DB really has a value.

  3. Wouldn't it be better to write if == null instead?

Below is the code I implemented.

Please suggest a good way.

public Optional<CallCounselEntity> getCallCounselByUserId(UUID userId, UUID counselId) {
        return return callCounselRepository.findByUserId(userId, counselId);
    }



public CallCounselEntity updateCallCounsel(CallCounselEntity callCounsel) {
        **getCallCounselByUserId(callCounsel.getUserId(),callCounsel.getCounselId()).orElseThrow(() -> new NoSuchElementException("No Search Data"));**
        callCounselRepository.save(callCounsel));

        return callCounsel;
}

CodePudding user response:

What do you mean by 'optional gets destroyed'?

You want CallCounselEntity entity = getCallCounselByUserId(...).orElseThrow(...). If the code doesn't throw, then the method continues with entity now being assigned a non-optional value which you can use to make the update. If the code throws, then, well, any remaining code in your method referencing entity will never be reached. That's the whole point.

So, to sum up, the following is perfectly valid code for your scenario:

CallCounselEntity entity = getCallCounselByUserId(...).orElseThrow(...)
/* perform your update here */

Alternatively, you can use:

getCallCounselByUserId(...).ifPresent(existingCounsel -> { 
    /* perform your update here */ 
})

or

getCallCounselByUserId(...).ifPresentOrElse(existingCounsel -> { 
    /* perform your update here */ 
}, () -> throw new RuntimeException(...))

if you still want to throw the exception for non-existent entities.

  • Related