Search code examples
c#.netconstructormappingdto

Instantiate dto object by id where object received as payload C#


There is a Task controller function which received payload in DTO JSON. Now whether to insert or update is based on the id received in payload data. How to instantiate the object with id value.

The task controller:

async Task<Response<RequestDto>> UpdateRequest([FromBody]RequestDto req)

I have to update/insert based on the req objects id value.

Should I use a constructor to RequestDto or by null checking assign id values

EDIT

The DTO structure here:

namespace MyWorkspace.ProjectA.Domain
{
    /// <summary>
    /// Request DTO
    /// </summary>
    public class RequestDto
    {
        /// <summary>
        /// Request Id
        /// </summary>
        [RegularExpression(Constants.GuidRegex)]
        public string RequestId { get; set; }
        [JsonConverter(typeof(StringEnumConverter))]
        public RequestDetails RequestDetails { get; set; }
        public DateTime OpenDate { get; set; }
        **public RequestDto()
        {
            RequestId = Guid.NewGuid().ToString("N");
        }**
    }
}

Now I have to update/insert the request based on ID, where 2 types of request payload can come in below ways:

1. {
  "reqId": "",
  "requestDetails": {
    "basicDetails": {
      "firstName": "Something",
      "lastName": "Something",
      "emailAddress": "Something",
    },
        "addressDetails": {
      "addLine1": "Something",
      "addLine2": "Something",
      "city": "Something",
      "state": "Something",
      "country": "Something",
    }
  },
  "openDate": "2019-07-05T09:59:18.601Z",
}
2. {
  "reqId": "0b5c7dd3931944b28f693ef8bf6fa2ad",
  "requestDetails": {
    "basicDetails": {
      "firstName": "Something",
      "lastName": "Something",
      "emailAddress": "Something",
    },
        "addressDetails": {
      "addLine1": "Something",
      "addLine2": "Something",
      "city": "Something",
      "state": "Something",
      "country": "Something",
    }
  },
  "openDate": "2019-07-05T09:59:18.601Z",
}

Question : Do I need a constructor in DTO. How do I assign ID to DTO object / or I should assign a new id in case blank as :

public async Task<Response<RequestDto>> UpdateRequest([FromBody]RequestDto req)
{
    req.RequestId = (req.RequestId == null || req.RequestId == "") ? Guid.NewGuid().ToString("N") : req.RequestId;
    _validatorObj.ValidateAndThrow(req, ruleSet: "RequestValidation");
    var result = await _repository.UpsertRequest(req);

    return Response<RequestDto>.Ok(result, "Success");
}

Code in repo:

public async Task<RequestDto> UpsertRequest(RequestDto req)
{
    var jsonReqDet = JsonConvert.SerializeObject(req.requestDetails);
    var entity = _mapper.Map<RequestEntity>(req);
    entity.RowKey = RequestEntity.GetRowKey(req.RequestId);
    entity.PartitionKey = RequestEntity.GetPartKey(req.RequestId);
    entity.RequestDetails = jsonReqDet;
    entity.OpenDate = DateTime.UtcNow;
    await AzureTableAdapter.Upsert(entity, RequestTableName);

    return req;
}

Note:

  1. I am getting whole DTO object in payload
  2. I am using Azure storage and "InsertOrReplace" operation
  3. I have request table with requestDetils field as string, to keep the json value

What should I do?


Solution


  • Finally

    I found what I was doing wrong. The payload contains reqId="". So even I am having a constructor:

    public RequestDto()
    {
        RequestId = Guid.NewGuid().ToString("N");
    }
    

    after the moment it got initialized, due to controller signature:

    public async Task<Response<RequestDto>> UpdateRequest([FromBody]RequestDto req)
    

    the id initialized with null. So I removed the reqId from payload only(during insert). Final payload I am accepting now:

    1. {
      "requestDetails": {
        "basicDetails": {
          "firstName": "Something",
          "lastName": "Something",
          "emailAddress": "Something",
        },
            "addressDetails": {
          "addLine1": "Something",
          "addLine2": "Something",
          "city": "Something",
          "state": "Something",
          "country": "Something",
        }
      },
      "openDate": "2019-07-05T09:59:18.601Z",
    }
    2. {
      "reqId": "0b5c7dd3931944b28f693ef8bf6fa2ad",
      "requestDetails": {
        "basicDetails": {
          "firstName": "Something",
          "lastName": "Something",
          "emailAddress": "Something",
        },
            "addressDetails": {
          "addLine1": "Something",
          "addLine2": "Something",
          "city": "Something",
          "state": "Something",
          "country": "Something",
        }
      },
      "openDate": "2019-07-05T09:59:18.601Z",
    }