C
C#14mo ago
Camster

❔ NSwag and API help

I'm learning how to build an APIs and connect to it from a Blazor front-end. I'm using NSwag to build the http service client. I suppose this might be an NSwag question more than C#, but perhaps I'm just doing it completely wrong. In my API, all my function calls are wrapped in a custom Result class:
public class Result
{
public bool IsSuccess { get; set; }
public string Message { get; set; }
public string Error { get; set; }
}

public class Result<T> : Result
{
public T Obj { get; set; }
}
public class Result
{
public bool IsSuccess { get; set; }
public string Message { get; set; }
public string Error { get; set; }
}

public class Result<T> : Result
{
public T Obj { get; set; }
}
So an example call in the API would be:
public interface IAPICaller
{
Task<Result<AccountEntity>> Account_Create(AccountCreateDTO create);
Task<Result<List<AccountEntity>>> Account_Get(bool showInactive);
Task<Result<AccountEntity>> Account_Update(AccountUpdateDTO update);
}
public interface IAPICaller
{
Task<Result<AccountEntity>> Account_Create(AccountCreateDTO create);
Task<Result<List<AccountEntity>>> Account_Get(bool showInactive);
Task<Result<AccountEntity>> Account_Update(AccountUpdateDTO update);
}
The problem is that NSwag flattens the call into one class:
public partial interface IClient
{
System.Threading.Tasks.Task<AccountEntityListResult> AccountsGetAsync(bool showInactive)
}

public partial class AccountEntityListResult
{
public bool IsSuccess { get; set; }
public string Message { get; set; }
public string Error { get; set; }
public System.Collections.Generic.ICollection<AccountEntity> Obj { get; set; }
}
public partial interface IClient
{
System.Threading.Tasks.Task<AccountEntityListResult> AccountsGetAsync(bool showInactive)
}

public partial class AccountEntityListResult
{
public bool IsSuccess { get; set; }
public string Message { get; set; }
public string Error { get; set; }
public System.Collections.Generic.ICollection<AccountEntity> Obj { get; set; }
}
How can I get NSwag to build the classes to be separated like they are in the API? My plan is to build generic functions in the front-end to depend on the Result class, with different logic based on the IsSuccess, Message, and Error properties. Thank you for your help!
2 Replies
Camster
CamsterOP14mo ago
I'll probably have to write my own wrapper within blazor to convert the classes to what I need 😕
Accord
Accord14mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server