Похожие чаты

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 ответов

43 просмотра

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

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

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

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта