Bernhard
10/10/2018, 3:19 PMdiesieben07
10/10/2018, 3:21 PMclass DbEntry<ID : UUID?>(val id: ID, val name: String)
typealias SavedDbEntry = DbEntry<UUID>
typealias UnsavedDbEntry = DbEntry<Nothing?>
UnsavedDbEntry
can only hold null
for id
, SavedDbEntry
can only hold non-null UUID
.DbEntry<*>
to refer to either.Bernhard
10/10/2018, 3:24 PMdiesieben07
10/10/2018, 3:25 PMNothing?
, not Nothing
.DbEntry
(the only thing visible from a Java standpoint), has a field (and getter) of type UUID
with @Nullable
.Bernhard
10/10/2018, 3:26 PMdiesieben07
10/10/2018, 3:26 PMBernhard
10/10/2018, 3:27 PMdiesieben07
10/10/2018, 3:27 PMUUID
, which to it's mind can be null.tddmonkey
10/10/2018, 3:56 PMid
vs no-id
issue here, it helps you be sure that adding an attribute on one (save, read etc.) path doesn’t have an unintended affect on another pathBernhard
10/11/2018, 6:57 AMdata class User(val id: String, override val name: String) : SavableUser
data class CreateUser(override val name: String) : SavableUser
interface SavableUser {
val name: String
}
diesieben07
10/11/2018, 7:36 AMtddmonkey
10/11/2018, 8:16 AMdiesieben07
10/11/2018, 8:17 AMtddmonkey
10/11/2018, 8:18 AMdiesieben07
10/11/2018, 8:19 AMtddmonkey
10/11/2018, 8:20 AMdiesieben07
10/11/2018, 8:22 AMdon’t usually find myself converting between them so muchNot sure how you are doing this. You will need to write a separate
saveToDb
function for every type you have...tddmonkey
10/11/2018, 8:26 AMGET
endpoint for a client to retrieve data, then a PUT/POST
endpoint to update data. There’s no read-update-write cycle inside my servicediesieben07
10/11/2018, 8:28 AMtddmonkey
10/11/2018, 8:29 AMhow do you feel about having different objects for writing and reading database objects? Writing an object will for instance not require an id whereas querying an object will always have one. Otherwise I have to add null checks everywhere.
. Nothing in there suggests that isn’t/can’t be a stateless RESTful servicediesieben07
10/11/2018, 8:31 AMval unsaved = UnsavedEntity(... data ...)
// unsaved.id does not exist / is null
val saved: SavedEntity = db.save(unsaved)
println(saved.id) // guaranteed to be not null
Inside db.save
you need to go from UnsavedEntity
to SavedEntity
.tddmonkey
10/11/2018, 8:38 AMdiesieben07
10/11/2018, 8:40 AMtddmonkey
10/11/2018, 8:46 AMtypealias
route instead of having a (potentially reusable) container object to represent the saved state and using composition? Say something like:
class SavedItem<T: Any>(val id: UUID, val item: T)
Then you either use User
or Saved<User>
as necessary in the right place?diesieben07
10/11/2018, 8:49 AMuser.item.name
instead of user.name
).tddmonkey
10/11/2018, 8:53 AMdiesieben07
10/11/2018, 8:53 AMdata class
😄tddmonkey
10/11/2018, 8:56 AMdiesieben07
10/11/2018, 8:56 AMtddmonkey
10/11/2018, 8:57 AM