harvic3 / HttpClientApp

Simple project to learn how to use HttpClient and learn tips about OOP.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.
  2. 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
  2. dotnet restore
  3. dotnet run
  4. 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.
  2. Use the JsonSerializer class to serialize and deserialize JSON.
  3. It is important to use the JsonPropertyName attribute to map the properties of the DTO to the properties of the domain object.
  4. It's also a good practice to use the DTO pattern to map the domain object to the provider object and vice versa.
  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
      };
    }
  }

About

Simple project to learn how to use HttpClient and learn tips about OOP.

License:MIT License


Languages

Language:C# 100.0%