How do I link an domain object in memory to its database records without cluttering the domain with database concerns?

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/146765

  •  04-11-2019
  •  | 
  •  

Question

Your domain model contains a set of objects. I'm here presenting a side project, but I have a much more complicated work project falling to its knees because I didn't do a good separation of the database and the domain.

In this case, the object is called a CyclingRecord.

Following Onion Architecture, the domain objects know nothing of their persistence. However, each one of those objects corresponds to an entry in a datastore (potentially a database, potentially a file). The row ID is absolutely not part of the domain object and therefore has no business being stored in the domain object.

data CyclingRecord = CyclingRecord { date :: Day,
                                     distance :: Float,
                                     time :: Integer,
                                     description :: Maybe String }

In a language that enforces strong immutability (such as Haskell and Clojure), identity and value are synonymous. Further, there is no reason why you could not record more than a single ride in a day. So, the entire record completely describes a ride.

So, if I edit a record in the Domain, when I pass the edited objects back to the Database Infrastructure, how do I go about notifying the database that it needs to update a particular record instead of deleting an existing record?

(the same idea would need to be extensible so that I can have an application potentially manipulate many records at once and then send them all back to the database. Offline operation or syncing or just avoiding excessive disk activity)

Unless the correct answer is to actually make something like a unique identifier for each ride. Perhaps a UUID. Perhaps add a "day counter" to the record. Make it intrinsically part of a bike ride and then have the database depend on it instead of a separate row ID.

No correct solution

Licensed under: CC-BY-SA with attribution
scroll top