Base solution for your next web application
Open Closed

UpdateAsync example #839


User avatar
0
acrigney created

Would you be able to give me an UpdateAsync example

I have this code.

public async Task<PatientBodyImage> PostSetPatientBodyImageStateAsync(PatientBodyImageInput input) {

        PatientBodyImage patientBodyImage = _patientBodyImageRepository.Get(input.Id);

        Logger.Debug("Setting  a new image state async: " + input.Name);
        
        var update = await _patientBodyImageRepository.UpdateAsync(input.Id, async (x) =>
        {
            await Task.Run(() => UpdateImageState(patientBodyImage, input));                
        });

        return (update);
    }

Havent tested it uet Love your work!!


2 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I assume that this method is in an application service. If it is, you should not return your Entity to client. Instead you should use a dto object like PatientBodyImageDto.

    An example update might be like:

    public async Task<PatientBodyImageDto> PostSetPatientBodyImageStateAsync(IdInput input)
    {
        PatientBodyImage patientBodyImage = await _patientBodyImageRepository.GetAsync(input.Id);
    
        Logger.Debug("Setting a new image state async: " + input.Name);
        
        patientBodyImage.State = PatientBodyImageStates.Approved;
        
        var update = await _patientBodyImageRepository.UpdateAsync(patientBodyImage);
    
        return update.MapTo<PatientBodyImageDto>();
    }
    

    You have to create PatientBodyImageDto class and put an attribute on it for AutoMapping.

    [AutoMapFrom(typeof(PatientBodyImage))]
    

    I hope this helps.

  • User Avatar
    0
    acrigney created

    Thats great about returning the DTO I forgot about that!

    And also your code is much simpler.

    I didn't want to use an automapper for the map from the DTO to the domain object so I was using a function as not all the fields would be set in this update case but you don't need to with your solution.

    Awesome thank you!