Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/harvic3/httpclientapp
Simple project to learn how to use HttpClient and learn tips about OOP.
https://github.com/harvic3/httpclientapp
Last synced: about 1 month ago
JSON representation
Simple project to learn how to use HttpClient and learn tips about OOP.
- Host: GitHub
- URL: https://github.com/harvic3/httpclientapp
- Owner: harvic3
- License: mit
- Created: 2024-02-04T20:41:15.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-02-08T04:33:33.000Z (9 months ago)
- Last Synced: 2024-02-08T05:29:42.481Z (9 months ago)
- Language: C#
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# HttpClientApp
This project is a simple example about how use HttpClient in a .NET Core console application.
This project is a simple console application that uses the mock API to get and send data.
Also this project was builded using good practices about OOP.## How to run
1. The easier way is open the project in Visual Studio and run it from there.
1. The console application will run and display the questions and answers from the mocked API.If you want to run it from the command line, follow the instructions below.
## How to run from the command line
1. Clone the repository and run the following commands in the root directory of the project
1. `dotnet restore`
1. `dotnet run`
1. The console application will run and display the questions and answers from the mocked API.## Summary of the keys to serialization
1. The `System.Text.Json.Serialization` namespace is used to serialize and deserialize JSON.
1. Use the `JsonSerializer` class to serialize and deserialize JSON.
1. It is important to use the `JsonPropertyName` attribute to map the properties of the DTO to the properties of the domain object.
1. It's also a good practice to use the `DTO pattern` to map the domain object to the provider object and vice versa.```csharp
internal class AnimalDto
{
[JsonPropertyName( "name" )]
public string Name { get; set; }[JsonPropertyName( "color" )]
public string Color { get; set; }[JsonPropertyName( "sound" )]
public string Sound { get; set; }[JsonPropertyName( "image" )]
public Uri Image { get; set; }public static AnimalDto FromDomain( Animal animal )
{
return new AnimalDto
{
Name = animal.Name,
Color = animal.Color,
Sound = animal.Sound,
Image = animal.Image
};
}
}
```