One-to-many "flattened" projection? #2575
Replies: 1 comment
-
I don't see a way around this except through a custom |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Imagine I rent vehicles. I'm writing a software system to keep track of the vehicles I'm renting. As well, I want to be able to keep some informative information about the types of vehicles that I rent, both for my internal purposes as well as for display on my website. I would like to use Marten for my system. Also it should be noted that I am an event-sourcing newbie so go easy with me if I'm doing something really wrong.
I'm starting out with two aggregates:
VehicleType
, representing the information about a type of vehicle like Make, Model, and YearRentalVehicle
, representing an actual physical car out on my lot that I rent out to peopleIf my fleet of rental vehicles comprises ten 2022 Ford Mustangs and eleven 2021 Nissan Sentras, I will have two
VehicleType
and twenty-oneRentalVehicle
aggregates in my system.I would like to represent
RentalVehicle
in an event-sourced manner, with events such as:VehicleAcquired
, which is the "creation" event for theRentalVehicle
and carries theId
of the respectiveVehicleType
, the Vehicle Identification Number (VIN), and the acquisition dateVehicleRented
VehicleSold
VehicleLost
The
VehicleType
aggregate does not really need to be event-sourced and can be stored in a document. However, for the purposes of my problem here, imagine it is event sourced and only has one event:VehicleTypeCreated
functions as the "creation" event forVehicleType
, carrying values for Make, Model, and YearMy problem arises when I want to have a projection that combines the information from these aggregates. Let's assume I want a projection
VehicleInformation
(ignoring how horrible this name is). The documents in this projection need to carry the information for eachRentalVehicle
as well as the associatedVehicleType
. If I were using an RDBMS, I'd just have tablesRentalVehicle
andVehicleType
with a materialized viewVehicleInformation
likeOf course that's not possible here since we're not looking at this with tables and relations and views. I know my Marten projection will need to be multi-streamed but I don't see a way to accomplish this with the provided
MultiStreamProjection
, basically because:VehicleType
event occurs before any associatedRentalVehicle
aggregates even exist so there's nothing to do in that case anywayVehicleType
events likeVehicleMakeUpdated
, then there's the challenge of "broadcasting" that one event to all the existingRentalVehicle
aggregatesRentalVehicle
events (I'm assuming) will need to "look up" information on the associatedVehicleType
in order to apply that to the associatedVehicleInformation
documentI think I can accomplish what I've set out to do here with a custom
IProjection
but the implementation seems rather complex in my head. And this kind of scenario seems like it would be pretty common (essentially a flattened one-to-many view/projection) that there has to be a better way.Any better recommendations? Maybe I'm looking at the problem the wrong way and there is some better and more elegant way to model this that will solve the problem?
Beta Was this translation helpful? Give feedback.
All reactions