concept representation in category .net

appears as: representation, The representation
Microservices in .NET Core, Second Edition

This is an excerpt from Manning's book Microservices in .NET Core, Second Edition.

  • An HTTP GET endpoint at URLs of the form /users/{userId} that responds with a representation of the user. This endpoint implements both queries in figure 5.3
  • The Loyalty Program microservice is made up of the same set of standard components you’ve seen before, as shown in figure 5.4. The endpoints are implemented in the HTTP API component.

    Figure 5.4. The endpoints exposed by the Loyalty Program microservice are implemented in the HTTP API component.
    ch05 fig 4 horsdal2

    The other sides of these collaborations are microservices that most likely follow the same standard structure, with the addition of a LoyaltyProgramClient component. For instance, the Invoice microservice might be structured as shown in figure 5.5.

    In the API Gateway microservice, you’ll create a class called LoyaltyProgramClient that’s responsible for dealing with communication with the Loyalty Program microservice. That class encapsulates building and sending HTTP requests to the Loyalty Program microservice.

    The code for sending the register-user command takes a user name as input, creates an HTTP POST with a user object in the body, and sends that to the Loyalty Program microservice. After the response comes back from the Loyalty Program the we will just return the whole thing as an HttpResponseMessage. That’s an easy solution for the current needs, but often a client class like this would also check the status code of the response, deserialize the body if the status code is as expected and possibly even perform error handling if the status code indicates an error. The following listing shows the implementation.

    Listing 5.6. The API Gateway microservice registering new users
    language="sql" linenumbering="unnumbered">  using System.Net.Http;
      using System.Text;
      using System.Threading.Tasks;
      using Newtonsoft.Json;
    
      public class LoyaltyProgramClient
      {
        private readonly HttpClient httpClient;
    
        public LoyaltyProgramClient(HttpClient httpClient)
        {
          this.httpClient = httpClient;
        }
    
        public async Task<HttpResponseMessage> RegisterUser(string name)
        {
          var user = new {name, Settings = new { }};
          return await this.httpClient.PostAsync("/users/",       #1
            CreateBody(user));
        }
    
        private static StringContent CreateBody(object user)
        {
          return new StringContent(                              #2
            JsonConvert.SerializeObject(user),
            Encoding.UTF8,
            "application/json");                                 #3
        }
      }