In one of our applications, we had the following code:
public abstract class Event | |
{ | |
public Event(Guid operationId) | |
{ | |
OperationId=operationId; | |
} | |
public Guid OperationId { get; } | |
} |
public class ProductCreatedEvent: Event | |
{ | |
public ProductCreatedEvent(Guid operationdId):base(operationdId) | |
{ | |
} | |
} |
We were using events to share information between different modules in our Modulith. These same events were also stored in an Event Log as a source for possible projections into a read model.
However in case of the code above, the āoperationIdā for was always {00000000-0000-0000-0000-000000000000} when deserializing the ProductCreated event.
Do you notice what is wrong?
Important to notice here is that our āOperationIdā is a readonly property that is set through the constructor. You may already wonder why the JSON serializer is smart enough to fill in this property if only a getter exists. This is because it has a neat trick where it looks at the constructor of the object and if it can find a corresponding parameter, it will call the constructor and pass on the parameter value.
Now that you know this, take a second look at the code above. The reason it fails is because there is a typo in the constructor of the ProductCreatedEvent class. It spells āoperationdIdā instead of āoperationdIdā. This no longer matches with the serialized data and causes the ProductCreatedEvent to be constructed with a default guid value.
The fix is easy, just remove the typo:
public class ProductCreatedEvent: Event | |
{ | |
public ProductCreatedEvent(Guid operationId):base(operationId) | |
{ | |
} | |
} |