FluentResults icon indicating copy to clipboard operation
FluentResults copied to clipboard

No issue: My example of generic ResultDto

Open Kingside88 opened this issue 2 years ago • 0 comments

I want to provide my example how I use the ResultDto. Most of the code, I get from here. But with mine you can optionally provide generic data. I use this code like this:

myResultObject.WithError($"Error while saving").ToResultDto(personnel);

public class ResultDto<T> : ResultDto
    {
        public T Data { get; set; }
        public ResultDto(bool isSuccess, IEnumerable<ErrorDto> errors, T data) : base(isSuccess, errors)
        {
            this.Data = data;
        }
    }

    public class ResultDto
    {
        public bool IsSuccess { get; set; }

        public IEnumerable<ErrorDto> Errors { get; set; }

        public ResultDto(bool isSuccess, IEnumerable<ErrorDto> errors)
        {
            IsSuccess = isSuccess;
            Errors = errors;
        }
    }

    public class ErrorDto
    {
        public string Message { get; set; }

        public string? Key { get; set; }

        public ErrorDto(string message, string? key)
        {
            Message = message;
            Key = key;
        }
    }

    public static class ResultDtoExtensions
    {
        public static ResultDto ToResultDto(this Result result)
        {
            if (result.IsSuccess)
                return new ResultDto(true, Enumerable.Empty<ErrorDto>());

            return new ResultDto(false, TransformErrors(result.Errors));
        }

        public static ResultDto<T> ToResultDto<T>(this Result result, T data)
        {
            if (result.IsSuccess)
                return new ResultDto<T>(true, Enumerable.Empty<ErrorDto>(), data);

            return new ResultDto<T>(false, TransformErrors(result.Errors), data);
        }

        private static IEnumerable<ErrorDto> TransformErrors(List<IError> errors)
        {
            return errors.Select(TransformError);
        }

        private static ErrorDto TransformError(IError error)
        {
            var key = TransformErrorKey(error);

            return new ErrorDto(error.Message, key);
        }

        private static string? TransformErrorKey(IError error)
        {
            if (error.Metadata.TryGetValue("Key", out var key))
                return key as string;

            return null;
        }
    }`
````    public class ResultDto<T> : ResultDto
    {
        public T Data { get; set; }
        public ResultDto(bool isSuccess, IEnumerable<ErrorDto> errors, T data) : base(isSuccess, errors)
        {
            this.Data = data;
        }
    }

    public class ResultDto
    {
        public bool IsSuccess { get; set; }

        public IEnumerable<ErrorDto> Errors { get; set; }

        public ResultDto(bool isSuccess, IEnumerable<ErrorDto> errors)
        {
            IsSuccess = isSuccess;
            Errors = errors;
        }
    }

    public class ErrorDto
    {
        public string Message { get; set; }

        public string? Key { get; set; }

        public ErrorDto(string message, string? key)
        {
            Message = message;
            Key = key;
        }
    }

    public static class ResultDtoExtensions
    {
        public static ResultDto ToResultDto(this Result result)
        {
            if (result.IsSuccess)
                return new ResultDto(true, Enumerable.Empty<ErrorDto>());

            return new ResultDto(false, TransformErrors(result.Errors));
        }

        public static ResultDto<T> ToResultDto<T>(this Result result, T data)
        {
            if (result.IsSuccess)
                return new ResultDto<T>(true, Enumerable.Empty<ErrorDto>(), data);

            return new ResultDto<T>(false, TransformErrors(result.Errors), data);
        }

        private static IEnumerable<ErrorDto> TransformErrors(List<IError> errors)
        {
            return errors.Select(TransformError);
        }

        private static ErrorDto TransformError(IError error)
        {
            var key = TransformErrorKey(error);

            return new ErrorDto(error.Message, key);
        }

        private static string? TransformErrorKey(IError error)
        {
            if (error.Metadata.TryGetValue("Key", out var key))
                return key as string;

            return null;
        }
    }

Kingside88 avatar Jun 11 '22 10:06 Kingside88