I am using Entity framework core repository pattern with unit of work (C#) I want to know the effective way of update the object
Model Class
public class Item : IEntity
{
public string Label { get; set; }
public double Quantity { get; set; }
public string Description { get; set; }
public int DisplayPriority { get; set; }
public ItemStatus Status { get; set; }
public string ShopperComment { get; set; }
}
DTOclass
public class ItemDto
{
public string Label { get; set; }
public double Quantity { get; set; }
public string Description { get; set; }
public int DisplayPriority { get; set; }
public string ShopperComment { get; set; }
}
Controller code
[HttpPut("{itemId}")]
public async Task<IActionResult> Update(Guid itemId,[FromBody]ItemDto itemDto)
{
try
{
if (!_permissionsManager.UserHasAnAllowedRole(User, new List<string>() { "SuperAdmin", "Administrator", "Item.Update" }))
{
return Unauthorized(new ErrorDto("Method not allowed"));
}
var item = await _unitOfWork.Items.Get(itemId);
if (item == null)
{
return BadRequest(new ErrorDto("Item does not exist"));
}
item.Label = itemDto.Label;
item.Status = itemDto.Status;
item.ShopperComment = itemDto.ShopperComment;
item.Description = itemDto.Description;
item.Quantity = itemDto.Quantity;
item.DisplayPriority = itemDto.DisplayPriority;
await _unitOfWork.SaveChangesAsync();
return Ok(item.toItemDto());
}
catch (Exception x)
{
_log.Error("[Update] Failed", x);
return StatusCode(500, new ErrorDto(x.Message));
}
}
I don't want the mapping from ItemDto to Item for update Item Object in the controller how can I do that with good flexible way through loose coupling as I don't want to add object dependencies in the controller