C
C#3y ago
N0b0dy

Get method for API

public Order GetById(int orderId)
{
var customerData = _orders.FirstOrDefault(x => x.Id == orderId);
StandardDelivery delivery = new StandardDelivery(orderData: customerData);
delivery.State = StandardDeliveryState.Prepare;
delivery.State = StandardDeliveryState.Dispatch;
delivery.State = StandardDeliveryState.Delivery;
customerData.DeliveryDate = delivery.deliveryDate;
delivery.Dispose();

return customerData;
}
public Order GetById(int orderId)
{
var customerData = _orders.FirstOrDefault(x => x.Id == orderId);
StandardDelivery delivery = new StandardDelivery(orderData: customerData);
delivery.State = StandardDeliveryState.Prepare;
delivery.State = StandardDeliveryState.Dispatch;
delivery.State = StandardDeliveryState.Delivery;
customerData.DeliveryDate = delivery.deliveryDate;
delivery.Dispose();

return customerData;
}
Is it an ideal approach to set properties values before getting sending the JSON?
4 Replies
Brainiac V
Brainiac V3y ago
I don't know where or why you picked up that pattern, but don't ever do it like that If you need to map a data structure to a different one, either use a library like AutoMapper or Mapster, use DTOs with LINQs select or write specific mapper classes
Scratch
Scratch3y ago
I looked at your code in code review. If you're using the setter of State to perform work, that's a terrible idea. Use a function for that at least
N0b0dy
N0b0dyOP3y ago
@Scratch function to update state?
Scratch
Scratch3y ago
Actually I have no idea what your original question is even trying to ask Move whatever logic you're doing in the setter of State into its own method so you need to call that to perform that transition
Want results from more Discord servers?
Add your server