Похожие чаты

How do you guys transfer codes and stuff via diff

layers of the projects?

i have made this model to pass data from business to controller public class APIResponse<T>(int success, string message, T data, List<string> errors = null)
{
public int Success { get; set; } = success;
public string Message { get; set; } = message;
public List<string> Errors { get; set; } = errors ?? [];
public T Data { get; set; } = data;
}

calling it like
[HttpPost]
public async Task<IActionResult> Something(ContactRequest contactRequest)
{
APIResponse<ContactRequest> Response = await _user.SaveMessage(contactRequest);
return StatusCode(Response.Success, Response);
}
and business be like
if (string.IsNullOrEmpty(contactRequest.Name)) Response.Errors.Add("Invalid Name");

if (Response.Errors.Count == 0)
{
Response.Success = (int)StatCode.Success;
Response.Message = "Saved successfully";
}
else
{
Response.Success = (int)StatCode.Bad_Request;
Response.Message = "Error saving name";
}


suggest better ways or let me know if this ones gonna work fine StatCode is an enum for all status code numbers (200,404,401 etc)

8 ответов

42 просмотра

this is good idea i think wait for someone pro that give you correct answer if my answer is wrong.

I use something like this: public class Result<T> { public bool IsSuccess { get; private init; } public T? Value { get; private init; } public string[]? Errors { get; private init; } public static Result<T> Success(T value) => new() { IsSuccess = true, Value = value }; public static Result<T> Failure(string[] errors) => new() { IsSuccess = false, Errors = errors }; } var chat = await _unitOfWork.ChatRepository.FindAsync(x => x.Id == request.ChatId, false); return chat == null ? Result<ChatDto>.Failure([$"Chat with id {request.ChatId} not found"]) : Result<ChatDto>.Success(_mapper.Map(chat));

Jass- Автор вопроса
Іван Карпов
I use something like this: public class Result<T> ...

Glad to see even C# folks start to get into using monads

Jass- Автор вопроса
Jass
is it about c# also going functional thing?

that depends on what will be the outcome if discriminated unions and type unions issues that are being worked on but i have my doubts

Jass
is it about c# also going functional thing?

I don't like it being put in the "functional" thing, but more in the type safety and type-assisted safety

Похожие вопросы

Обсуждают сегодня

Карта сайта