Ok so I have these classes to process an Event of an OrderUploadFile creation:
public class ProcessOrderXlsEvent : ProcessFileBaseEvent
{
public Guid Id { get; set; }
}
public class ProcessFileBaseEvent
{
public OrderUploadFile? Message { get; set; }
public DateTime Timestamp { get; set; }
}
I have a class OrderUploadFile that inherits from Entity (which is used by other classes)
public class OrderUploadFile : Entity
{
public string FileNameOriginal { get; private set; }
public string ContentType { get; private set; }
public long ContentLength { get; private set; }
public OrderUploadFile(
string fileNameOriginal,
string contentType,
long contentLength)
{
FileNameOriginal = fileNameOriginal;
ContentType = contentType;
ContentLength = contentLength;
}
}
Entity:
public abstract class Entity
{
public Guid Id { get; private set; }
public string CreatedBy { get; set; }
public DateTime CreatedAt { get; private set; }
public string? UpdatedBy { get; set; }
public DateTime? UpdatedAt { get; set; }
protected Entity()
{
Id = Guid.NewGuid();
}
}
A message comes with this structure:
{
"Id": "dasadsa-dasda-dasda-42fdsa-1312dswas",
"Message": {
"FileNameOriginal": "fileName-etc.xlsm",
"ContentType": "application/vnd.ms-excel.sheet.macroEnabled.12",
"ContentLength": 153121,
"Id": "432424dasdad-fdsfsd312-dsa21-312dsa-ds212",
"CreatedBy": null,
"CreatedAt": "0001-01-01T00:00:00",
"UpdatedBy": null,
"UpdatedAt": null
},
"Timestamp": "2022-10-21T09:25:16.9305165Z"
}
The message, when it arrives, actually has the correct id of the OrderUploadFile class, but when deserialized, this id is not recognized. If I comment out the Entity constructor's Id assignment line to test, the OrderUploadFile's Id is left with an empty Guid (example 0000-0000-0000-etc).
This is the way of deserialization (As MessageBody being the message above):
JsonConvert.DeserializeObject<ProcessOrderXlsEvent>(args.MessageBody);
How can I get the Id inside the message to be correctly assigned to OrderUploadFile? Is it possible to do this even inheriting from Entity?