jaduff / LabLog

Software for recording physical users of computers in a lab environment.
MIT License
0 stars 1 forks source link

Creating new events for storage from write model #31

Closed jaduff closed 6 years ago

jaduff commented 6 years ago

I've seen how to create SchoolCreatedEvents, but can't work out how to create child events. To get a RoomAddedEvent, I need a school to attach it to. But the school only has an eventhandler if its static Create method was called. This returns the event that creates the room. But if I'm adding a room to a school that already exists, I don't want to call School.Create(), because it will create a new GUID; I want to instantiate an existing school. Should I either change the constructor of School to allow it to take an event handler, thus enabling me to do School school = new School(eventhandler stuff here) and then to do school.AddRoom(room)? Or should the school.AddRoom() method itself take an eventhandler of its own? school.AddRoom(room, eventhandler stuff) ?

simonjduff commented 6 years ago

Yes, constructor with event handler, and then call the Replay with a list of events, then call your method. Alternatively, you could pass in the list of events to replay as part of that constructor so you don't have to call replay separately

jaduff commented 6 years ago

This isn't for replaying, this is for creating the event in the first place. I assumed I pass the details to the School, and it creates the event and returns it, just as School.Create does. Or should I be newing the event in the Controller instead, then passing it to the School to replay?

simonjduff commented 6 years ago

"But if I'm adding a room to a school that already exists" you must replay all the events, then do your AddRoom()

jaduff commented 6 years ago

Yes, but I can get the events from the database. For the RoomAdded event, do I New it in the controller, or do I pass Room attributes to the School class for it to return the event to me?

jaduff commented 6 years ago

Wait, the answer to that is obvious, it has to be made in the School so it can validate the event. K.

jaduff commented 6 years ago

Presumably I'd want to handle the whole thing in one action, to minimize the possibility of other events being created at the same time. So creating a School(events) and then calling School.HandleMyEvent(event) is less than ideal? How would you create the school, give it an event list, and pass it the data to create the new event in one fell swoop? You could possibly do School.AddRoom(schoolId, eventList, roomName, etc)... but that's a LOT of stuff to put into every method to handle events?

simonjduff commented 6 years ago

This is where Optimistic Locking comes in. If another thread tries to create a new event for the same entity at the same time, the first one inserts to the database, and the second one fails (Unique key violation on EntityId, Version). You handle this by replaying the new event, revalidating your action, and raising a new event.

jaduff commented 6 years ago

Ok, I'll carry on with what I'm doing then, and work on that one later.