iayti / CleanArchitecture

ASP.NET Core 6 Web API Clean Architecture Solution Template
MIT License
682 stars 124 forks source link

Return Task<ServiceResult> without <T> #39

Closed chuki2 closed 3 years ago

chuki2 commented 3 years ago

Based on this code

 public async Task<ServiceResult<CityDto>> Handle(UpdateCityCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Cities.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(City), request.Id);
            }
            if (!string.IsNullOrEmpty(request.Name))
                entity.Name = request.Name;
            entity.Active = request.Active;

            await _context.SaveChangesAsync(cancellationToken);

            return ServiceResult.Success(_mapper.Map<CityDto>(entity));
        }

Any I idea how I can return ServiceResult without Dto? I just want to return public async Task<ServiceResult> Handle(UpdateCityCommand request, CancellationToken cancellationToken)

In some case, I just want to tell success only

Thanks in advance.

iayti commented 3 years ago

You can use this example.

public class UpdateCityCommand : IRequestWrapper<bool>
{
    public int Id { get; set; }

    public string Name { get; set; }

    public bool Active { get; set; }
}

public class UpdateCityCommandHandler : IRequestHandlerWrapper<UpdateCityCommand, bool>
{
    private readonly IApplicationDbContext _context;
    private readonly IMapper _mapper;

    public UpdateCityCommandHandler(IApplicationDbContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    public async Task<ServiceResult<bool>> Handle(UpdateCityCommand request, CancellationToken cancellationToken)
    {
        var entity = await _context.Cities.FindAsync(request.Id);

        if (entity == null)
        {
            throw new NotFoundException(nameof(City), request.Id);
        }
        if (!string.IsNullOrEmpty(request.Name))
            entity.Name = request.Name;
        entity.Active = request.Active;

        await _context.SaveChangesAsync(cancellationToken);

        return ServiceResult.Success(true);
    }
}

Also if you don't want to return any results.

public class UpdateCityCommand : IRequest
{
    public int Id { get; set; }

    public string Name { get; set; }

    public bool Active { get; set; }
}

public class UpdateCityCommandHandler : IRequestHandler<UpdateCityCommand>
{
    private readonly IApplicationDbContext _context;
    private readonly IMapper _mapper;

    public UpdateCityCommandHandler(IApplicationDbContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    public async Task<Unit> Handle(UpdateCityCommand request, CancellationToken cancellationToken)
    {
        var entity = await _context.Cities.FindAsync(request.Id);

        if (entity == null)
        {
            throw new NotFoundException(nameof(City), request.Id);
        }
        if (!string.IsNullOrEmpty(request.Name))
            entity.Name = request.Name;
        entity.Active = request.Active;

        await _context.SaveChangesAsync(cancellationToken);

        return Unit.Value;
    }
}

Have a nice coding 😊

chuki2 commented 3 years ago

Great, thanks for that example!