Implementing request models

Now, let's look at the implementation of some DTOs in practice. The Order entity describes some actions that accept List<string> as input. Let's suppose that the implementation is getting more complex and our service needs to store additional information about our orders:

using System;
using System.Collections.Generic;

namespace SampleAPI.Models
{
public class Order
{
public Guid Id { get; set; }
public IEnumerable<string> ItemsIds { get; set; }
public string Currency { get; set; }
}
}

The domain model is a critical part of our application. It is essential to keep it separated from the request/response model. Therefore, we should avoid having the domain model tightly coupled with the request and the response models. Let's start by implementing the OrderRequest class, which will be a representation of the create request model. It will be used by the Post action method to create a new entity:

using System.Collections.Generic;

namespace SampleAPI.Requests
{
public class OrderRequest
{
public IEnumerable<string> ItemsIds { get; set; }

public string Currency { get; set; }
}
}

The OrderRequest class contains the same fields as the Order domain model, apart from the Id field. This is because the client of the API should not insert the Id information. The request model makes our filesystem in a separate folder. In this case, as you can see from the namespace, OrderRequest is stored in the Requests folder. Our OrderController can use OrderRequest as follows:

     [Route("api/order")]
[ApiController]
public class OrderController : ControllerBase
{

...

[HttpPost]
public IActionResult Post(OrderRequest request)
{
var order = Map(request);

_orderRepository.Add(order);

return CreatedAtAction(nameof(GetById), new { id = order.Id },
null);
}

...

private Order Map(OrderRequest request)
{

return new Order
{
Id = Guid.NewGuid(),
ItemsIds = request.ItemsIds,
Currency = request.Currency
};
}
...

The Post action method accepts a parameter of the OrderRequest type, which is the representation of the order request that we want to create. To link incoming data with the domain model, we should create a Map method that initializes a new domain model that's populated with the request model. Furthermore, we should also create a new instance of the Order object with the requested data and combine it with IOrderRepository. The same concept can be applied to the Put action method of our controller:

         ...

[HttpPut("{id:guid}")]
public IActionResult Put(Guid id, OrderRequest request)
{
var order = _orderRepository.Get(id);

if (order == null)
{
return NotFound(new { Message = $"Item with id {id} not exist." });
}

order = Map(request, order);

_orderRepository.Update(id, order);

return Ok();
}

private Order Map(OrderRequest request, Order order)
{
order.ItemsIds = request.ItemsIds;
order.Currency = request.Currency;

return order;
}

...

In this case, we create a new Map method, which takes two parameters as input: the OrderRequest and the Order, and we can proceed by assigning each property of the request object to the order. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.42.168