I have a subscriber on a bus to NewDeriative messages, which is flat and primitive DTO style, for example sake has three strings ModelName, ManufacturerName, DerivativeName.
Inside the system there is a domain model of Manufacturer, Model and Derivative, a derivative has a Model, and a model has a Manufacturer. E.g. BMW --> X5 --> Sport
As part of the mapping from the message to the domain model. It goes like follows
Lookup manufacturer repository by query where name is that of manufacturerName, if not create one instance.
On that manufacturer instance look through its Model collection where name is that of ModelName, if not create one. (manufacturer.AddModel(string modelName))
Now we have a model instance (which in term has manufacturer). Now create the derivative.
Persist.
This works great in a single threaded environment, but yesterday I turned on 10 worker threads and that where the problem hit me. In the process of looking up Manufacturer another thread has started to process another NewDerivative message, so until the other one has persisted two manufacturers are being created and persisted.
I'm interested in how transactions could help in this situation, rather reworking the approach. I pushed it back to 1 worker thread for the timebing and carrying on with other parts of the implementation