|
| 1 | +module CrmDomain.Aggregate.Contact |
| 2 | + |
| 3 | +open System |
| 4 | +open Fescq |
| 5 | +open CrmDomain |
| 6 | + |
| 7 | +type Contact = { |
| 8 | + Name: PersonalName |
| 9 | + Phones: Map<Guid, PhoneNumber> |
| 10 | +} |
| 11 | + |
| 12 | + |
| 13 | +type CreateContact (name:PersonalName) = |
| 14 | + inherit Fescq.Command.CreateAggregateCommand() |
| 15 | + member val Name = name |
| 16 | + |
| 17 | +type RenameContact (aggregateId:Guid, originalVersion:int, name:PersonalName) = |
| 18 | + inherit Fescq.Command.UpdateCommand(aggregateId, originalVersion) |
| 19 | + member val Name = name |
| 20 | + |
| 21 | +type AddContactPhone (aggregateId:Guid, originalVersion:int, phone: PhoneNumber) = |
| 22 | + inherit Fescq.Command.DetailCommand(aggregateId, originalVersion) |
| 23 | + member val Phone = phone |
| 24 | + |
| 25 | +type UpdateContactPhone (aggregateId:Guid, originalVersion:int, phoneId:Guid, phone: PhoneNumber) = |
| 26 | + inherit Fescq.Command.DetailCommand(aggregateId, phoneId, originalVersion) |
| 27 | + member val Phone = phone |
| 28 | + |
| 29 | +type ContactCommand = |
| 30 | + private |
| 31 | + | Create of CreateContact |
| 32 | + | Rename of RenameContact |
| 33 | + | AddPhone of AddContactPhone |
| 34 | + | UpdatePhone of UpdateContactPhone |
| 35 | + |
| 36 | +// only public constructor for ContactCommand |
| 37 | +let validateCommand (command:Fescq.Command.ICommand) = |
| 38 | + match command with |
| 39 | + |
| 40 | + | :? CreateContact -> |
| 41 | + let cmd = command :?> CreateContact |
| 42 | + cmd.Name |
| 43 | + |> Validate.personalName |
| 44 | + |> Option.map (fun _ -> cmd |> ContactCommand.Create) |
| 45 | + |
| 46 | + | :? RenameContact -> |
| 47 | + let cmd = command :?> RenameContact |
| 48 | + cmd.Name |
| 49 | + |> Validate.personalName |
| 50 | + |> Option.map (fun _ -> cmd |> ContactCommand.Rename) |
| 51 | + |
| 52 | + | :? AddContactPhone -> |
| 53 | + let cmd = command :?> AddContactPhone |
| 54 | + cmd.Phone |
| 55 | + |> Validate.phoneNumber |
| 56 | + |> Option.map (fun _ -> cmd |> ContactCommand.AddPhone) |
| 57 | + |
| 58 | + | :? UpdateContactPhone -> |
| 59 | + let cmd = command :?> UpdateContactPhone |
| 60 | + cmd.Phone |
| 61 | + |> Validate.phoneNumber |
| 62 | + |> Option.map (fun _ -> cmd |> ContactCommand.UpdatePhone) |
| 63 | + |
| 64 | + | _ -> failwith "unexpected command" |
| 65 | + |
| 66 | + |
| 67 | +[<EventData("contact-created", 1)>] |
| 68 | +type ContactCreated (name:PersonalName) = |
| 69 | + interface IEventData |
| 70 | + member val Name = name |
| 71 | + |
| 72 | +[<EventData("contact-renamed", 1)>] |
| 73 | +type ContactRenamed (name:PersonalName) = |
| 74 | + interface IEventData |
| 75 | + member val Name = name |
| 76 | + |
| 77 | +[<EventData("contact-phone-added", 1)>] |
| 78 | +type ContactPhoneAdded (phoneId:Guid, phone:PhoneNumber) = |
| 79 | + interface IEventData |
| 80 | + member val PhoneId = phoneId |
| 81 | + member val Phone = phone |
| 82 | + |
| 83 | +[<EventData("contact-phone-updated", 1)>] |
| 84 | +type ContactPhoneUpdated (phoneId:Guid, phone:PhoneNumber) = |
| 85 | + interface IEventData |
| 86 | + member val PhoneId = phoneId |
| 87 | + member val Phone = phone |
| 88 | + |
| 89 | +type ContactEvent = |
| 90 | + | Created of ContactCreated |
| 91 | + | Renamed of ContactRenamed |
| 92 | + | PhoneAdded of ContactPhoneAdded |
| 93 | + | PhoneUpdated of ContactPhoneUpdated |
| 94 | + |
| 95 | + |
| 96 | +let validateEventData (eventData:IEventData) = |
| 97 | + match eventData with |
| 98 | + | :? ContactCreated -> eventData :?> ContactCreated |> ContactEvent.Created |
| 99 | + | :? ContactRenamed -> eventData :?> ContactRenamed |> ContactEvent.Renamed |
| 100 | + | :? ContactPhoneAdded -> eventData :?> ContactPhoneAdded |> ContactEvent.PhoneAdded |
| 101 | + | :? ContactPhoneUpdated -> eventData :?> ContactPhoneUpdated |> ContactEvent.PhoneUpdated |
| 102 | + | _ -> failwith "unsupported event type" |
| 103 | + |
| 104 | + |
| 105 | +let apply (state:(int*Contact) option) (e:Event) = |
| 106 | + |
| 107 | + let entity () = |
| 108 | + match state with |
| 109 | + | Some (_, value) -> value |
| 110 | + | None -> failwith "state was None for update" |
| 111 | + |
| 112 | + let version = |
| 113 | + match state with |
| 114 | + | Some (value, _) -> value |
| 115 | + | None -> 1 |
| 116 | + |
| 117 | + let update = |
| 118 | + match validateEventData e.EventData with |
| 119 | + |
| 120 | + | ContactEvent.Created data -> |
| 121 | + match state with |
| 122 | + | None -> { Name = data.Name; Phones = []|> Map } |
| 123 | + | Some _ -> failwith "state was Some for create" |
| 124 | + |
| 125 | + | ContactEvent.Renamed data -> |
| 126 | + { entity() with Name = data.Name } |
| 127 | + |
| 128 | + | ContactEvent.PhoneAdded data -> |
| 129 | + let prev = entity() |
| 130 | + if prev.Phones.ContainsKey(data.PhoneId) then failwith "ContactPhoneAdded: id already exists" |
| 131 | + { prev with Phones = prev.Phones.Add(data.PhoneId, data.Phone) } |
| 132 | + |
| 133 | + | ContactEvent.PhoneUpdated data -> |
| 134 | + let prev = entity() |
| 135 | + if not(prev.Phones.ContainsKey(data.PhoneId)) then failwith "ContactPhoneUpdated: id does not exist" |
| 136 | + { prev with Phones = prev.Phones.Add(data.PhoneId, data.Phone) } |
| 137 | + |
| 138 | + Some (version, update) |
| 139 | + |
| 140 | +module Handle = |
| 141 | + |
| 142 | + let aggId (command:Command.ICommand) = |
| 143 | + command.AggregateId |
| 144 | + |
| 145 | + let create utcNow metaData (command:CreateContact) = |
| 146 | + |
| 147 | + let key = |
| 148 | + { Name = "contact" |
| 149 | + Id = aggId command |
| 150 | + Version = 1 } |
| 151 | + |
| 152 | + { |
| 153 | + AggregateKey = key |
| 154 | + Timestamp = utcNow |
| 155 | + MetaData = metaData |
| 156 | + EventData = ContactCreated(command.Name) |
| 157 | + } |
| 158 | + |> Aggregate.createWithFirstEvent apply |
| 159 | + |
| 160 | + |
| 161 | + let update utcNow metaData (command:Fescq.Command.ICommand) (aggregate:Agg<Contact>) = |
| 162 | + |
| 163 | + try |
| 164 | + command |
| 165 | + |> validateCommand |
| 166 | + |> function |
| 167 | + | Some cmd -> |
| 168 | + match cmd with |
| 169 | + | Create cmd -> ContactCreated(cmd.Name) :> IEventData, cmd |> aggId |
| 170 | + | Rename cmd -> ContactRenamed(cmd.Name) :> IEventData, cmd |> aggId |
| 171 | + | AddPhone cmd -> ContactPhoneAdded(cmd.DetailId, cmd.Phone) :> IEventData, cmd |> aggId |
| 172 | + | UpdatePhone cmd -> ContactPhoneUpdated(cmd.DetailId, cmd.Phone) :> IEventData, cmd |> aggId |
| 173 | + | None -> failwith "data validation failed" |
| 174 | + |
| 175 | + |> fun (eventData, aggId) -> |
| 176 | + if aggId = aggregate.Key.Id then |
| 177 | + |
| 178 | + { AggregateKey = { aggregate.Key with Version = aggregate.Key.Version + 1 } |
| 179 | + Timestamp = utcNow |
| 180 | + MetaData = metaData |
| 181 | + EventData = eventData } |
| 182 | + |> Aggregate.createWithNextEvent apply aggregate.History |
| 183 | + |> Ok |
| 184 | + else |
| 185 | + Error "aggregate and command refer to different ids" |
| 186 | + with |
| 187 | + ex -> Error ex.Message |
| 188 | + |
| 189 | + module CSharp = |
| 190 | + |
| 191 | + let Update utcNow metaData command aggregate = |
| 192 | + update utcNow metaData command aggregate |
| 193 | + |> function |
| 194 | + | Ok agg -> struct (Some struct (fst agg, snd agg), None) |
| 195 | + | Error msg -> struct (None, Some msg) |
| 196 | + |
| 197 | +module Storage = |
| 198 | + |
| 199 | + let private factory history = |
| 200 | + try |
| 201 | + let (agg, _) = Aggregate.createFromHistory<Contact> apply history |
| 202 | + Ok agg |
| 203 | + with |
| 204 | + ex -> Error ex.Message |
| 205 | + |
| 206 | + let load (store:IEventStore) (aggId:Guid) = |
| 207 | + Repository<Contact> store |
| 208 | + :> IRepository<Contact> |
| 209 | + |> fun x -> x.Load(aggId, factory) |
| 210 | + |
| 211 | + let loadExpectedVersion (store:IEventStore) (aggId:Guid) (expectedVersion:int) = |
| 212 | + Repository<Contact> store |
| 213 | + :> IRepository<Contact> |
| 214 | + |> fun x -> x.LoadExpectedVersion(aggId, factory, expectedVersion) |
| 215 | + |
| 216 | + |
| 217 | + let save (store:IEventStore) (update:Agg<Contact> * Event list) = |
| 218 | + |
| 219 | + Repository<Contact> store |
| 220 | + :> IRepository<Contact> |
| 221 | + |> fun x -> x.Save(fst update, snd update) |
| 222 | + |
| 223 | + module CSharp = |
| 224 | + |
| 225 | + let Load (store:IEventStore, aggId:Guid) = |
| 226 | + load store aggId |
| 227 | + |> function |
| 228 | + | Ok agg -> struct (Some agg, None) |
| 229 | + | Error msg -> struct (None, Some msg) |
| 230 | + |
| 231 | + |
| 232 | + let LoadExpectedVersion (store:IEventStore, aggId:Guid, expectedVersion:int) = |
| 233 | + loadExpectedVersion store aggId expectedVersion |
| 234 | + |> function |
| 235 | + | Ok agg -> struct (Some agg, None) |
| 236 | + | Error msg -> struct (None, Some msg) |
| 237 | + |
0 commit comments