C
C#3mo ago
jlim

✅ wpf json CRUD(mvvm)

Could you tutorial how to use json config file ? I will do CRUD in the mvvm pattern through Json file.
42 Replies
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
leowest
leowest3mo ago
Imagine you have a User DTO (which represents the JSON of one of your endpoints)
public class UserDto
{
public Guid Id {get;set;}
public string Name {get;set;}
public int Age {get;set;}
}
public class UserDto
{
public Guid Id {get;set;}
public string Name {get;set;}
public int Age {get;set;}
}
And your API returns a list of Users for example... So you would be using HttpClient to communicate with your API via a service. As very barebone example imagine something like:
public class UserService
{
private readonly HttpClient _httpClient;
public UserService(HttpClient httpClient)
{
_httpClient = httpClient;
}

public async Task<List<UserDto>> GetUsers()
{
return await _httpClient.GetFromJsonAsync<UserDto>("your api endpoint");
}
}
public class UserService
{
private readonly HttpClient _httpClient;
public UserService(HttpClient httpClient)
{
_httpClient = httpClient;
}

public async Task<List<UserDto>> GetUsers()
{
return await _httpClient.GetFromJsonAsync<UserDto>("your api endpoint");
}
}
So in your mvvm you would have an instance of that service and u would call GetUsers to populate a ObservableCollection which would in turn populate your View. This is very barebone simplistic explanation just to give u an idea.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
leowest
leowest3mo ago
I don't think its a file but could be
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
leowest
leowest3mo ago
could be poor wording and language barrier
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
leowest
leowest3mo ago
so I gave an example using an API and if that is not what he wants im sure he will say 😉 regardless from httpclient to readfile is very little changes
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
jlim
jlim3mo ago
Thanks your kindness reply, but I don't need Http client. Json file is located into my pc local I can't use DBMS like mysql, postgre. And factory's environment is can't use internet. So I wanna use configure file like json, xml.. And I wanna implemented CRUD in the json file. And yea' I think IOC is clear. If give me a guideline or solution then I will step up than before Thanks your kindness coding! But my intent is not using http
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
jlim
jlim3mo ago
I see Oh I found FileUserservice!, I try it thanks!
leowest
leowest3mo ago
then I would suggest you use SQLite which is local and can be used with efcore
SleepWellPupper
SleepWellPupper3mo ago
SqLite uses a file also and provides more facilities suited to CRUD oh yeah me too slow
Want results from more Discord servers?
Add your server